Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cloud Error Reporting Trace for ASP.NET Core #700

Merged
merged 21 commits into from Jan 23, 2017

Conversation

iantalarico
Copy link
Contributor

Allows for automated tracing of incoming requests to ASP.NET Core applications. Trace data is sent to the Stackdriver Trace API.

This PR includes:

  • Middleware for Tracing
  • Unit tests
  • Documentation
  • Snippets and examples

Coming soon:

  • Integration Tests
  • Delegate Handler for Tracing outgoing requests.

# Conflicts:
#	apis/Google.Cloud.Diagnostics.AspNet/Google.Cloud.Diagnostics.AspNet/Trace/Labels.cs
# Conflicts:
#	apis/Google.Cloud.Diagnostics.AspNet/Google.Cloud.Diagnostics.AspNet/Trace/CloudTrace.cs
#	apis/Google.Cloud.Diagnostics.AspNet/Google.Cloud.Diagnostics.AspNet/Trace/TraceHeaderContextUtils.cs
#	apis/Google.Cloud.Diagnostics.AspNet/Google.Cloud.Diagnostics.AspNet/Trace/TraceHeaderTraceOptionsFactory.cs
#	apis/Google.Cloud.Diagnostics.Common/Google.Cloud.Diagnostics.Common.Tests/AssemblyInfo.cs
#	apis/Google.Cloud.Diagnostics.Common/Google.Cloud.Diagnostics.Common.Tests/Trace/LabelsTest.cs
#	apis/Google.Cloud.Diagnostics.Common/Google.Cloud.Diagnostics.Common.Tests/Trace/SimpleManagedTracerTest.cs
#	apis/Google.Cloud.Diagnostics.Common/Google.Cloud.Diagnostics.Common/Trace/Labels.cs
#	apis/Google.Cloud.Diagnostics.Common/Google.Cloud.Diagnostics.Common/Trace/TraceHeaderContext.cs
/// </summary>
/// <param name="projectId">The Google Cloud Platform project ID. Cannot be null.</param>
/// <param name="config">Optional trace configuration, if unset the default will be used.</param>
/// <param name="clientTask">Optional a task which produces the Trace client, if

This comment was marked as spam.

This comment was marked as spam.

{
// If the trace header says to trace or if the rate limiter allows tracing continue.
var headerContext = provider.GetService<TraceHeaderContext>();
if (!headerContext.ShouldTrace)

This comment was marked as spam.

This comment was marked as spam.

// Trace the delegate and annotate it with information from the current
// http context.
tracer.StartSpan(httpContext.Request.Path);
await _next(httpContext);

This comment was marked as spam.

This comment was marked as spam.

}

/// <summary>
/// Creates a <see cref="IManagedTracer"/> based on the <see cref="TraceHeaderContext"/> and

This comment was marked as spam.

This comment was marked as spam.

@jskeet
Copy link
Collaborator

jskeet commented Jan 20, 2017

As Mike seems to be on the case here, I'll wait until he's done - I wouldn't be reviewing it today anyway, as I'm off soon and this is a big thing to start last minute on Friday :)

Copy link
Contributor Author

@iantalarico iantalarico left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Mike PTAL

/// </summary>
/// <param name="projectId">The Google Cloud Platform project ID. Cannot be null.</param>
/// <param name="config">Optional trace configuration, if unset the default will be used.</param>
/// <param name="clientTask">Optional a task which produces the Trace client, if

This comment was marked as spam.

}

/// <summary>
/// Creates a <see cref="IManagedTracer"/> based on the <see cref="TraceHeaderContext"/> and

This comment was marked as spam.

{
// If the trace header says to trace or if the rate limiter allows tracing continue.
var headerContext = provider.GetService<TraceHeaderContext>();
if (!headerContext.ShouldTrace)

This comment was marked as spam.

// Trace the delegate and annotate it with information from the current
// http context.
tracer.StartSpan(httpContext.Request.Path);
await _next(httpContext);

This comment was marked as spam.

Copy link
Contributor

@evildour evildour left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just 2 additional comments. Everything else looks good.

// End sample

// Sample: UseTracer
/// <param name="tracer">Populated by dependency injection.</param>

This comment was marked as spam.

using System;
using Xunit;

using ProjectId = Google.Cloud.Diagnostics.AspNetCore.CloudTraceExtension.ProjectId;

This comment was marked as spam.

@iantalarico
Copy link
Contributor Author

Thanks! PTAL

Copy link
Contributor

@evildour evildour left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Collaborator

@jskeet jskeet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't done a thorough review of the tests, but I love how easy it is to set up. Will see if I can use it from Azure easily, once I've honed my "getting Google credentials from Azure Key Vault" code...

{
public class CloudTraceExtensionTest
{
private const string projectId = "pid";

This comment was marked as spam.

This comment was marked as spam.

TraceHeaderContext context, bool rateLimiterShouldTrace)
{
Mock<IConsumer<TraceProto>> mockConsumer = new Mock<IConsumer<TraceProto>>();
Mock<IServiceProvider> mockProvider = new Mock<IServiceProvider>();

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

/// finely grained manual tracing.
/// </summary>
///
/// <example>

This comment was marked as spam.

var traceIdFactory = provider.GetService<TraceIdFactory>();
var consumer = provider.GetService<IConsumer<TraceProto>>();

// Check that we have all the needed services.

This comment was marked as spam.

This comment was marked as spam.

throw new InvalidOperationException("Ensure Google Cloud Trace is properly set up.");
}

// If the trace header says to trace or if the rate limiter allows tracing continue.

This comment was marked as spam.

This comment was marked as spam.

// If the trace header says to trace or if the rate limiter allows tracing continue.
if (!headerContext.ShouldTrace)
{
TraceOptions options = rateLimitingFactory.CreateOptions();

This comment was marked as spam.

This comment was marked as spam.

/// and trace the time taken for the next delegate to run. The time taken and metadata
/// will be sent to the Stackdriver Trace API.
/// </summary>
public sealed class CloudTraceMiddleware

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

/// <summary>
/// Creates a map of labels to represent a <see cref="StackTrace"/> for a span.
/// </summary>
public static Dictionary<string, string> FromStackTrace(StackTrace stackTrace) =>

This comment was marked as spam.

This comment was marked as spam.

Copy link
Contributor Author

@iantalarico iantalarico left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank Jon, PTAL

{
public class CloudTraceExtensionTest
{
private const string projectId = "pid";

This comment was marked as spam.

TraceHeaderContext context, bool rateLimiterShouldTrace)
{
Mock<IConsumer<TraceProto>> mockConsumer = new Mock<IConsumer<TraceProto>>();
Mock<IServiceProvider> mockProvider = new Mock<IServiceProvider>();

This comment was marked as spam.

var traceIdFactory = provider.GetService<TraceIdFactory>();
var consumer = provider.GetService<IConsumer<TraceProto>>();

// Check that we have all the needed services.

This comment was marked as spam.

throw new InvalidOperationException("Ensure Google Cloud Trace is properly set up.");
}

// If the trace header says to trace or if the rate limiter allows tracing continue.

This comment was marked as spam.

// If the trace header says to trace or if the rate limiter allows tracing continue.
if (!headerContext.ShouldTrace)
{
TraceOptions options = rateLimitingFactory.CreateOptions();

This comment was marked as spam.

/// and trace the time taken for the next delegate to run. The time taken and metadata
/// will be sent to the Stackdriver Trace API.
/// </summary>
public sealed class CloudTraceMiddleware

This comment was marked as spam.

/// <summary>
/// Creates a map of labels to represent a <see cref="StackTrace"/> for a span.
/// </summary>
public static Dictionary<string, string> FromStackTrace(StackTrace stackTrace) =>

This comment was marked as spam.

TraceHeaderContext context, bool rateLimiterShouldTrace)
{
Mock<IConsumer<TraceProto>> mockConsumer = new Mock<IConsumer<TraceProto>>();
Mock<IServiceProvider> mockProvider = new Mock<IServiceProvider>();

This comment was marked as spam.

/// and trace the time taken for the next delegate to run. The time taken and metadata
/// will be sent to the Stackdriver Trace API.
/// </summary>
public sealed class CloudTraceMiddleware

This comment was marked as spam.

GaxPreconditions.CheckState(consumer != null,
string.Format(message, typeof(TraceProto).GetType()));

// If the trace header and rate limiter say not to trace, return a no-op tracer.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

Copy link
Contributor Author

@iantalarico iantalarico left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Jon, PTAL

TraceHeaderContext context, bool rateLimiterShouldTrace)
{
Mock<IConsumer<TraceProto>> mockConsumer = new Mock<IConsumer<TraceProto>>();
Mock<IServiceProvider> mockProvider = new Mock<IServiceProvider>();

This comment was marked as spam.

GaxPreconditions.CheckState(consumer != null,
string.Format(message, typeof(TraceProto).GetType()));

// If the trace header and rate limiter say not to trace, return a no-op tracer.

This comment was marked as spam.

/// and trace the time taken for the next delegate to run. The time taken and metadata
/// will be sent to the Stackdriver Trace API.
/// </summary>
public sealed class CloudTraceMiddleware

This comment was marked as spam.

Copy link
Collaborator

@jskeet jskeet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comments, but feel free to merge.

TraceHeaderContext context, bool rateLimiterShouldTrace)
{
Mock<IConsumer<TraceProto>> mockConsumer = new Mock<IConsumer<TraceProto>>();
Mock<IServiceProvider> mockProvider = new Mock<IServiceProvider>();

This comment was marked as spam.

private const string ProjectId = "pid";
private const string TraceId = "105445aa7843bc8bf206b12000100f00";
private const ulong SpanId = 81237123;
private const string projectId = "pid";

This comment was marked as spam.

This comment was marked as spam.

GaxPreconditions.CheckState(consumer != null,
string.Format(message, typeof(TraceProto).GetType()));

// If the trace header and rate limiter say not to trace, return a no-op tracer.

This comment was marked as spam.

/// and trace the time taken for the next delegate to run. The time taken and metadata
/// will be sent to the Stackdriver Trace API.
/// </summary>
internal sealed class CloudTraceMiddleware

This comment was marked as spam.

# Conflicts:
#	apis/Google.Cloud.Diagnostics.AspNetCore/Google.Cloud.Diagnostics.AspNetCore.Snippets/AspNetCoreSnippets.cs
#	apis/Google.Cloud.Diagnostics.AspNetCore/docs/index.md
Copy link
Contributor Author

@iantalarico iantalarico left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Jon, sorry about the odd rename change...

@iantalarico iantalarico merged commit 94bf514 into googleapis:master Jan 23, 2017
@iantalarico iantalarico deleted the trace-core-2 branch January 26, 2017 22:20
gcf-owl-bot bot added a commit that referenced this pull request Feb 11, 2022
Source-Link: googleapis/googleapis@249e9a1

Source-Link: googleapis/googleapis-gen@1f2c41b
Copy-Tag: eyJwIjoiYXBpcy9Hb29nbGUuQ2xvdWQuQ29tcHV0ZS5WMS8uT3dsQm90LnlhbWwiLCJoIjoiMWYyYzQxYmJmZTA5NzYxNmNlYjFlOWZiNjc4NzJhNmJiMWRjNTlkNCJ9
gcf-owl-bot bot added a commit that referenced this pull request Feb 11, 2022
Source-Link: googleapis/googleapis@249e9a1

Source-Link: googleapis/googleapis-gen@1f2c41b
Copy-Tag: eyJwIjoiYXBpcy9Hb29nbGUuQ2xvdWQuQ29tcHV0ZS5WMS8uT3dsQm90LnlhbWwiLCJoIjoiMWYyYzQxYmJmZTA5NzYxNmNlYjFlOWZiNjc4NzJhNmJiMWRjNTlkNCJ9
jskeet pushed a commit that referenced this pull request Feb 11, 2022
Source-Link: googleapis/googleapis@249e9a1

Source-Link: googleapis/googleapis-gen@1f2c41b
Copy-Tag: eyJwIjoiYXBpcy9Hb29nbGUuQ2xvdWQuQ29tcHV0ZS5WMS8uT3dsQm90LnlhbWwiLCJoIjoiMWYyYzQxYmJmZTA5NzYxNmNlYjFlOWZiNjc4NzJhNmJiMWRjNTlkNCJ9
jskeet added a commit to jskeet/google-cloud-dotnet that referenced this pull request Feb 14, 2022
Changes in this release:

### New features

- Update compute API to revision 20220112 ([issue 700](googleapis#700)) ([commit ebe518a](googleapis@ebe518a))
  - The original commit contained breaking changes, but they were reverted in [commit 50ea200](googleapis@50ea200)
gcf-merge-on-green bot pushed a commit that referenced this pull request Feb 14, 2022
Changes in this release:

### New features

- Update compute API to revision 20220112 ([issue 700](#700)) ([commit ebe518a](ebe518a))
  - The original commit contained breaking changes, but they were reverted in [commit 50ea200](50ea200)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants