-
Notifications
You must be signed in to change notification settings - Fork 371
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
Is only ASP.NET Core supported by Diagnostics libraries? #6367
Comments
The Google.Cloud.Diagnostics.AspNet and Google.Cloud.Diagnostics.AspNetCore packages are integration packages, but they just use the Logging and Trace APIs under the hood - which you can use via the Google.Cloud.Logging.V2 and Google.Cloud.Trace.V2 packages. Could you give more details about what you'd expect to see? Are you still hoping for an ILoggerProvider, but without other ASP.NET Core dependencies, or are you not using ILogger at all? |
(Assigned to Amanda as she does more with Diagnostics than I do.) |
For background services take a look at #4855, it has a Logging working example. Might not be entirely what you are looking for but will probably point you in the right direction. #5018 and #5135 might also help. As @jskeet the Google.Cloud.Diagnostics.AspNetCore* libraries are integration libraries with ASP.NET Core and heavily depend on ASP.NET Core's pipeline. You can use the API client libraries directly and even Google.Cloud.Diagnostics.Common if you want to implement Diagnostics in other than ASP.NET Core. You might also be able to reuse some of Google.Cloud.Diagnostics.AspNetCore* for that. Let me know if the info here helps you or if you continue to be blocked, then share more info in what exactly you are attempting to do. |
@jskeet yes we would definitely be using ILogger. I believe a colleague of mine here at PetSmart (whos just went on PTO) was able to figure out an installer that uses IHostBuilder instead of IWebHostBuilder that seems to be inspired from some code that's already in the Google.Diagnostics.AspNetCore package and it appears to be working at the moment but it does rely on the Google.Diagnostics.AspNetCore package. The installer looks something like this, in case it gives any inspiration: public static class GcpDiagnosticsInstaller
{
private const string GoogleProjectIdSection = "Google:ProjectId";
private const string GoogleLogLevelSection = "Google:LogLevel";
/// <summary>
/// Adds Google Logging and Tracing capabilities.
/// </summary>
/// <param name="builder">An instance of <see cref="IHostBuilder"/></param>
/// <returns></returns>
public static IHostBuilder AddGoogleDiagnostics(this IHostBuilder builder)
{
return builder.ConfigureServices((context, services) =>
{
var settings = GetSettingsFromConfig(context);
var loggerOpts = LoggerOptions.Create(settings.LogLevel);
services.AddSingleton<ILoggerProvider>(provider => GoogleLoggerProvider.Create(provider, settings.ProjectId, loggerOpts));
services.AddSingleton<IStartupFilter>(new GcpDiagnosticsStartupFilter());
services.AddGoogleTrace(opt =>
{
opt.ProjectId = settings.ProjectId;
});
});
}
... internal class GcpDiagnosticsStartupFilter : IStartupFilter
{
public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next)
{
return app =>
{
app.UseGoogleTrace();
next(app);
};
}
} Do you see this being as being problematic? I haven't dug too deep into it yet to see if logs and traces were correlating, but logs do seem to be coming through. |
I'll wait for @amanda-tarafa to comment as she knows the code better than I do. My initial concern would be around buffering of log output. |
Logging should be fine. Tracing I'm not sure it is working. When you do |
Ok I will take a look at that specifically once I find a moment, I downloaded the zip. At least we know logging is good. |
@amanda-tarafa Finally got back to this. Ok so the example in the other thread appears to just fall back to using an ASP project type instead of the worker, and then using |
It looks like this is actually creating a NullManagedTracer It actually appears to always give a NullManagedTracer if there's no HttpContext. I recreated as asp project and if I do a trace from the controller, it works fine, but if I do it from the background worker, I get NullManagedTracer. I see yours is doing it in the background, so I'm not sure what the difference is unless you're inadvertently capturing the http context or something when you do the enqueue |
Yes, look only at how to establish the tracer, i.e. the code in
No that's not why, this way of creating the tracer is not dependendt on HttpContext. You are getting a
Let me know. |
@amanda-tarafa nice to meet you and Jon today! I went back and got things set up the way we talked about and initially, the logs and traces were coming through and correlating correctly, so I moved them back into position for what would be the final implementation and then things broke again. The difference between the test and the final implementation is that the code that is used to establish the trace context happens in a delegate: Above, the reachable code works. If I comment that part out to make the rest reachable, it no longer works. If I step through the part that isn't working, I can see I'm getting a SimpleManagedTracer so I'm not sure what the hangup is. The idea here is any time a message is picked up from Azure Service Bus, the ReceiveMessage callback is invoked and that creates it's own trace context. As for setup: options.Options = TraceOptions.Create(
qpsSampleRate: double.MaxValue,
bufferOptions: BufferOptions.NoBuffer(),
retryOptions: RetryOptions.NoRetry(ExceptionHandling.Propagate)); And I have something similar for the logging options. When I find some time, I might be able to get a repro put together, but I wanted to give you a head start in case there was something obvious that I'm missing. |
Same here, nice to e-meet you :). Nothing inmediately jumps out at me. I will try to recreate what I think you are doig but it would be useful if you could share the code that calls the processors, i.e., the code that receives the messages. (I'm also not certain what classes are from Azure libraries and what are yours). Things to check:
I'll try to reproduce this and report back early next week, if you can get a repro together that will probably make things faster. |
How many functions have you attached to processors.ProcessMessageAsync? Could it be that the one that starts the tracer is not the first one to execute?
Could it be that something is silently failing before reaching the ReceiveMessage handler? Maybe service scope is an issue. I'll try to create an example that mimicks this setup. |
So I was able to create what I felt was a decent facsimile of all the components involved in my implementation, all the way up to the point the delgate/handler was registered. The only real difference that I can see is that my example doesn't use My facsimile actually works correctly. I then tried swapping the fake implementation into my real code and it started working again... so this is starting to lead me to believe that there may be something with how fake container public void CreateProcessors(
ServiceBusProcessorOptions options,
Func<ProcessMessageEventArgs, Task> receiver,
Func<ProcessErrorEventArgs, Task> errorHandler)
{
for (var i = 0; i < 4; i++) // create 4 fake processors
{
var processor = new FakeProcessor();
processor.MessageReceived += receiver;
_processors.Add(processor);
}
}
public Task StartProcessorsAsync()
{
_isRunning = true;
// simulate incoming messages.
Task.Run(async () =>
{
while (_isRunning)
{
await Task.Delay(_rand.Next(25, 300)); // simulate messages coming in at random
foreach (var processor in _processors)
{
await processor.SendMessage($"Heartbeat {Guid.NewGuid():N}");
}
}
}).FireAndForget();
return Task.CompletedTask;
} actual container public void CreateProcessors(
ServiceBusProcessorOptions options,
Func<ProcessMessageEventArgs, Task> receiver,
Func<ProcessErrorEventArgs, Task> errorHandler)
{
_logger.LogInformation("Creating processors for {processorCount} subscriptions...", _subscriptions.Count);
foreach (var subscription in _subscriptions)
{
var processor = _client.CreateProcessor(subscription.Topic, subscription.Subscription, options);
processor.ProcessMessageAsync += receiver;
processor.ProcessErrorAsync += errorHandler;
_processors.Add(processor);
}
}
public async Task StartProcessorsAsync()
{
_logger.LogInformation("Starting {processorCount} processors...", _processors.Count);
foreach (var processor in _processors)
await processor.StartProcessingAsync();
} Receive method that is the delegate which gets called when a message comes in private async Task ReceiveMessage(ProcessMessageEventArgs args)
{
const string emptySubject = "Empty";
var label = args.Message?.Subject ?? emptySubject;
var tracer = _tracerFactory(TraceHeaderContext.Create(null, null, true));
ContextTracerManager.SetCurrentTracer(tracer);
using var span = tracer.StartSpan($"{args.Message?.MessageId ?? "NoLabel"}_{label}");
if (label == emptySubject)
{
_logger.LogError("Incoming message did not have a label/subject! Sending to dead letter...");
_logger.LogDebug("Message missing label. Message Body: {messageBody} Message Headers: {messageHeaders}",
args.Message?.Body?.ToString() ?? "NoBody",
JsonSerializer.Serialize(args.Message?.ApplicationProperties ?? new Dictionary<string, object>()));
await args.DeadLetterMessageAsync(args.Message, deadLetterReason: "MissingLabel", deadLetterErrorDescription: "No label was contained on the message, so we didn't know how to route it!");
return;
}
if(!_eventMap.TryGetAdapter(label, out var adapter))
{
_logger.LogError("There was no registration for the label '{label}'. Sending to dead letter", label);
await args.DeadLetterMessageAsync(args.Message, deadLetterReason: "MissingAdapterRegistration", deadLetterErrorDescription: $"The label '{label}' was not registered in EventMapping.cs");
return;
}
await adapter.AdaptAndRepublish(args.Message, args.CancellationToken);
await args.CompleteMessageAsync(args.Message);
} I can share you the real code but it has some private packages in it and needs azure service bus to run the broken version. The code I put together to actually reproduce the problem turned out to work, so I don't think that's going to be much help. |
I won't have time to look at this today, but I'll look this week. We'll figure it out :) |
Just a quick update: Same as you I haven't been able to reproduce any problems with the code as you pasted it. Today I'll try to tweak it and see if I get to a point where I can reproduce lack of traces. |
OK, a few more questions, because I haven't been able to reproduce any issues:
|
Changes in Google.Cloud.Diagnostics.AspNetCore version 4.3.0-beta01: - [Commit 60e8cd8](googleapis@60e8cd8): - feat: Copies GoogleLogger to Common. This allows easier use of GoogleLogger in non ASP.NET Core applications. - Towards [issue 6367](googleapis#6367) - Replicate LoggerOptions in Common, and have AspNetCore\*.LoggerOptions be just a wrapper of Common.LoggerOptions. - Copies ILogEntryLabelProvider to Common and marks the one in AspNetCore* Obsolete. - Makes AspNetCore*.IExternalTraceProvider obsolete. It can now be replaced by Common.ITraceContext. - [Commit 32cb606](googleapis@32cb606): - feat: Allows per log entry labels. - Closes [issue 5313](googleapis#5313) - Closes [issue 5929](googleapis#5929) - [Commit c8e9a48](googleapis@c8e9a48): - refactor: Makes GoogleLoggerScope extendable. Moves GoogleLoggerScope to Diagnostics.Common. In preparation for allowing LogEntry augmentation and making it easier to use Google logging from non ASP.NET Core apps. Towards [issue 5313](googleapis#5313), [issue 5360](googleapis#5360), [issue 5929](googleapis#5929) and [issue 6367](googleapis#6367) - [Commit 7f5f89e](googleapis@7f5f89e): - docs: Change Stackdriver to Google Cloud, and fix some typos, including in test code. - [Commit c4c9cd5](googleapis@c4c9cd5): - feat: Makes it easier to use tracing from non ASP.NET Core applications Closes [issue 5897](googleapis#5897) Towards [issue 6367](googleapis#6367) - [Commit b35b9ea](googleapis@b35b9ea): - feat: Decouples Diagnostics tracing from Google's trace header. Towards [issue 5360](googleapis#5360) and [issue 5897](googleapis#5897) - [Commit 0c00d2c](googleapis@0c00d2c): - refactor: Remove unnecesary service provider extension method. There's an equivalent method offered by Microsoft.Extensions.DependencyInjection so we don't need our own. - [Commit bb0c7b2](googleapis@bb0c7b2): - refactor: Remove unnecesary interface IManagedTracerFactory. It's an internal interface that we don't use anywhere. We can always add it back in if we need it at some point. - [Commit 8ef3983](googleapis@8ef3983): - fix: X-Cloud-Trace-Context trace mask values should be 0-1. See https://cloud.google.com/trace/docs/setup#force-trace Note: changing a generic type parameter constraint in `LabelProviderExtensions` is notionally a breaking change, but due to how it will be used, we don't expect it to actually break any customers. Changes in Google.Cloud.Diagnostics.AspNetCore3 version 4.3.0-beta01: - [Commit 60e8cd8](googleapis@60e8cd8): - feat: Copies GoogleLogger to Common. This allows easier use of GoogleLogger in non ASP.NET Core applications. - Towards [issue 6367](googleapis#6367) - Replicate LoggerOptions in Common, and have AspNetCore\*.LoggerOptions be just a wrapper of Common.LoggerOptions. - Copies ILogEntryLabelProvider to Common and marks the one in AspNetCore* Obsolete. - Makes AspNetCore*.IExternalTraceProvider obsolete. It can now be replaced by Common.ITraceContext. - [Commit 32cb606](googleapis@32cb606): - feat: Allows per log entry labels. - Closes [issue 5313](googleapis#5313) - Closes [issue 5929](googleapis#5929) - [Commit c8e9a48](googleapis@c8e9a48): - refactor: Makes GoogleLoggerScope extendable. Moves GoogleLoggerScope to Diagnostics.Common. In preparation for allowing LogEntry augmentation and making it easier to use Google logging from non ASP.NET Core apps. Towards [issue 5313](googleapis#5313), [issue 5360](googleapis#5360), [issue 5929](googleapis#5929) and [issue 6367](googleapis#6367) - [Commit 7f5f89e](googleapis@7f5f89e): - docs: Change Stackdriver to Google Cloud, and fix some typos, including in test code. - [Commit c4c9cd5](googleapis@c4c9cd5): - feat: Makes it easier to use tracing from non ASP.NET Core applications Closes [issue 5897](googleapis#5897) Towards [issue 6367](googleapis#6367) - [Commit b35b9ea](googleapis@b35b9ea): - feat: Decouples Diagnostics tracing from Google's trace header. Towards [issue 5360](googleapis#5360) and [issue 5897](googleapis#5897) - [Commit 0c00d2c](googleapis@0c00d2c): - refactor: Remove unnecesary service provider extension method. There's an equivalent method offered by Microsoft.Extensions.DependencyInjection so we don't need our own. - [Commit bb0c7b2](googleapis@bb0c7b2): - refactor: Remove unnecesary interface IManagedTracerFactory. It's an internal interface that we don't use anywhere. We can always add it back in if we need it at some point. - [Commit 8ef3983](googleapis@8ef3983): - fix: X-Cloud-Trace-Context trace mask values should be 0-1. See https://cloud.google.com/trace/docs/setup#force-trace Note: changing a generic type parameter constraint in `LabelProviderExtensions` is notionally a breaking change, but due to how it will be used, we don't expect it to actually break any customers. Packages in this release: - Release Google.Cloud.Diagnostics.AspNetCore version 4.3.0-beta01 - Release Google.Cloud.Diagnostics.AspNetCore3 version 4.3.0-beta01 - Release Google.Cloud.Diagnostics.Common version 4.3.0-beta01
Changes in Google.Cloud.Diagnostics.AspNetCore version 4.3.0-beta01: - [Commit 60e8cd8](60e8cd8): - feat: Copies GoogleLogger to Common. This allows easier use of GoogleLogger in non ASP.NET Core applications. - Towards [issue 6367](#6367) - Replicate LoggerOptions in Common, and have AspNetCore\*.LoggerOptions be just a wrapper of Common.LoggerOptions. - Copies ILogEntryLabelProvider to Common and marks the one in AspNetCore* Obsolete. - Makes AspNetCore*.IExternalTraceProvider obsolete. It can now be replaced by Common.ITraceContext. - [Commit 32cb606](32cb606): - feat: Allows per log entry labels. - Closes [issue 5313](#5313) - Closes [issue 5929](#5929) - [Commit c8e9a48](c8e9a48): - refactor: Makes GoogleLoggerScope extendable. Moves GoogleLoggerScope to Diagnostics.Common. In preparation for allowing LogEntry augmentation and making it easier to use Google logging from non ASP.NET Core apps. Towards [issue 5313](#5313), [issue 5360](#5360), [issue 5929](#5929) and [issue 6367](#6367) - [Commit 7f5f89e](7f5f89e): - docs: Change Stackdriver to Google Cloud, and fix some typos, including in test code. - [Commit c4c9cd5](c4c9cd5): - feat: Makes it easier to use tracing from non ASP.NET Core applications Closes [issue 5897](#5897) Towards [issue 6367](#6367) - [Commit b35b9ea](b35b9ea): - feat: Decouples Diagnostics tracing from Google's trace header. Towards [issue 5360](#5360) and [issue 5897](#5897) - [Commit 0c00d2c](0c00d2c): - refactor: Remove unnecesary service provider extension method. There's an equivalent method offered by Microsoft.Extensions.DependencyInjection so we don't need our own. - [Commit bb0c7b2](bb0c7b2): - refactor: Remove unnecesary interface IManagedTracerFactory. It's an internal interface that we don't use anywhere. We can always add it back in if we need it at some point. - [Commit 8ef3983](8ef3983): - fix: X-Cloud-Trace-Context trace mask values should be 0-1. See https://cloud.google.com/trace/docs/setup#force-trace Note: changing a generic type parameter constraint in `LabelProviderExtensions` is notionally a breaking change, but due to how it will be used, we don't expect it to actually break any customers. Changes in Google.Cloud.Diagnostics.AspNetCore3 version 4.3.0-beta01: - [Commit 60e8cd8](60e8cd8): - feat: Copies GoogleLogger to Common. This allows easier use of GoogleLogger in non ASP.NET Core applications. - Towards [issue 6367](#6367) - Replicate LoggerOptions in Common, and have AspNetCore\*.LoggerOptions be just a wrapper of Common.LoggerOptions. - Copies ILogEntryLabelProvider to Common and marks the one in AspNetCore* Obsolete. - Makes AspNetCore*.IExternalTraceProvider obsolete. It can now be replaced by Common.ITraceContext. - [Commit 32cb606](32cb606): - feat: Allows per log entry labels. - Closes [issue 5313](#5313) - Closes [issue 5929](#5929) - [Commit c8e9a48](c8e9a48): - refactor: Makes GoogleLoggerScope extendable. Moves GoogleLoggerScope to Diagnostics.Common. In preparation for allowing LogEntry augmentation and making it easier to use Google logging from non ASP.NET Core apps. Towards [issue 5313](#5313), [issue 5360](#5360), [issue 5929](#5929) and [issue 6367](#6367) - [Commit 7f5f89e](7f5f89e): - docs: Change Stackdriver to Google Cloud, and fix some typos, including in test code. - [Commit c4c9cd5](c4c9cd5): - feat: Makes it easier to use tracing from non ASP.NET Core applications Closes [issue 5897](#5897) Towards [issue 6367](#6367) - [Commit b35b9ea](b35b9ea): - feat: Decouples Diagnostics tracing from Google's trace header. Towards [issue 5360](#5360) and [issue 5897](#5897) - [Commit 0c00d2c](0c00d2c): - refactor: Remove unnecesary service provider extension method. There's an equivalent method offered by Microsoft.Extensions.DependencyInjection so we don't need our own. - [Commit bb0c7b2](bb0c7b2): - refactor: Remove unnecesary interface IManagedTracerFactory. It's an internal interface that we don't use anywhere. We can always add it back in if we need it at some point. - [Commit 8ef3983](8ef3983): - fix: X-Cloud-Trace-Context trace mask values should be 0-1. See https://cloud.google.com/trace/docs/setup#force-trace Note: changing a generic type parameter constraint in `LabelProviderExtensions` is notionally a breaking change, but due to how it will be used, we don't expect it to actually break any customers. Packages in this release: - Release Google.Cloud.Diagnostics.AspNetCore version 4.3.0-beta01 - Release Google.Cloud.Diagnostics.AspNetCore3 version 4.3.0-beta01 - Release Google.Cloud.Diagnostics.Common version 4.3.0-beta01
@jstafford5380 Have you had any chance trying out our latest beta releases? They include features to have the Diagnostics libraries work from non ASP.NET Core apps. Also, I wonder if the errors you were seeing in Azure could be fixed by using the new versions. I'm keeping this issue open because I want to improve the docs around using the libraries outside of ASP.NET Core, but I'll close it as soon as I've done that. If you continue to experiment issues in Azure, we can open a new issue just focusing on that. Let me know. |
I have not tried it yet, but I will attempt to do so this week, thanks! |
Thanks! And if I close the issue, we can always reopen, is jut so that it doesn't catch my attention while there's nothing I can do on my side. |
I just spent the past week testing the new 4.3.0-beta03. I tested both ASP.NET Core and a BackgroundService that listens to Google PubSub (without any *.aspnetcore library, only Google.Cloud.Diagnostics.Common). |
That's good to know @idofl . I'll be shortly updating the docs describing these new features, and given that you've already used them it'll be great if I get your input on the PR, so I'll flag you there (eow most likely). |
@idofl yeah I could use a hand. I'm looking at it right now and trying to figure out what I'm supposed to do. So far, I am doing the following in my test installer: public static void InstallGoogleDiagnostics(this IServiceCollection services, IConfiguration config)
{
services.AddHttpContextAccessor();
CloudTraceExtensions.AddGoogleTrace(services, options =>
{
options.ProjectId = config["Google:ProjectId"];
options.Options = TraceOptions.Create(double.MaxValue, BufferOptions.NoBuffer(), RetryOptions.NoRetry(ExceptionHandling.Propagate));
});
services.AddScoped(ProvideZipkinTraceContext);
services.AddSingleton<Action<HttpResponse, ITraceContext>>((response, traceContext) =>
response.Headers.Add("some_header", traceContext.TraceId));
}
private static ITraceContext ProvideZipkinTraceContext(IServiceProvider serviceProvider)
{
var httpContext = serviceProvider.GetRequiredService<IHttpContextAccessor>().HttpContext;
var adapter = ZipkinTraceAdapter.FromHeaders(httpContext.Request.Headers);
return adapter;
} I'm not getting any errors but the |
Make sure you are calling
See #6672 for the explanation of why |
@jstafford5380 , as Amanda wrote, in non-ASP apps, use the Google.Cloud.Diagnostics.Common. CloudTraceExtensions .AddGoogleTrace. It's available in the latest beta version:
To store the trace context in a non-HTTP scenario, use the following code to get the trace context:
In the above code snippets, I stored the value of traceContextValue in the pubsub message. I hope this helps. |
@jstafford5380 Just to make it clearer: First:
This part of my answer applies if you are extracting trace context information from HTTP Requests (which seems to be what you were trying to do in your code). Second: |
Yeah it looks like we have a few too many related topics all in flight at the same time. At this point in time, I am exploring the changes that allow me to use a non-google trace headers (e.g. B3) in an ASP context. If I do it using what Ido described above, I get the traces but without any of the ASP-injected info. So that looks useful for our workers, but at the moment, I'm trying to adapt b3 headers coming from apigee and anthos. I was digging through some of the commits looking for how to achieve this, and it looked I needed to replace some stuff in DI which is what I pasted above, replacing ITraceContext and adding that action to DI for responses but the code never gets hit. I feel like I'm missing a registration of a new factory or something |
Also worth noting that using Ido's example, none of the logs were correlated. |
@jstafford5380 Getting logs to correlate is quite straightforward. Add cloud logging in program.cs:
When the Cloud Logging provider is invoked, it will call the ContextTracerManager to get the current trace context, and will add the trace id and span id to the log record. If you rely on console logging and want all console logs to get correlated, then you need to use .NET 5 and create a custom console logger formatter that adheres to the logging json schema. We have an example for that which shows how to successfully correlate logs and traces in GKE, GCF, and Cloud Run through console logging. |
Oh right! Sorry about that, I forgot that's a separate setup (been off this for too long). Ok so that correlates now. Just the other thing remaining - custom trace ids in ASP |
@jstafford5380 For custom trace ids, look at the sample here: Line 372 in c4c9cd5
It shows how to add a custom header to the request and response, and how to use it to initialize the trace context. BTW, if you host your application behind the google cloud HTTP(S) load balancer, the LB adds the traceparent and x-cloud-trace-context headers to each incoming request and set both headers to the same value, so both Cloud Trace and the Activity object are initialized with the same trace id. If you are using a trace mechanism that looks for the traceparent header in incoming requests, then you might not need to manually change the trace id for cloud trace, as it will already have the same value and will use it when propagating the trace through new http requests. |
We are behind a google lb but it is not inserting a traceparent. However, it does insert the b3 headers and x-cloud-trace-context so if we leave it alone, the sdk correlates correctly by default, but we're actually wanting to implement the w3c traceparent longer term. It seems like it would be easy convert between that and b3 headers which is why i was experimenting with it. This does make it challenging though because our service bus stuff is on the new traceparent format. So if we use x-cloud-trace-context then when re-establishing a trace that uses traceparent... is their a sane way to convert between the two? Maybe just use the traceid (32 hex chars) and convert the parentid to integer? That would probably work in a custom formatter.
Ok so then I was looking in a correct place and that's what I based my code on. I can use tracer factory like you said in a non-http context, but the code never runs in an http context. |
@jstafford5380 You are correct regarding LB. I saw this behavior in our serverless and assumed both headers got added by the LB. The traceparent was probably added by the serverless framework.
When you say the code doesn't run in an http context, which part of the code do you mean? the delegate that extracts the custom trace header or the one that places the custom trace id in the request/response headers? For adding the header - scroll down that file, there is an example how to create an HttpClient using an HttpClient factory. That worked for me when I tested it. |
I want to go back a few comments from @jstafford5380 :
What @idofl described is only needed in non ASP.NET Core contexts (I'm guessing that when you say ASP, you really mean ASP.NET Core). You can get it to work, but you'll end up writing a lot of code that's already in public static void InstallGoogleDiagnostics(this IServiceCollection services, IConfiguration config)
{
// No need to add the HTTP Context accesor, we do it already.
// Make sure this is the one defined in Google.Cloud.Diagnostics.AspNetCore(3).
// Notice that the class in Google.Cloud.Diagnostics.AspNetCore(3) is named without an s at the end.
Google.Cloud.Diagnostics.AspNetCore3.CloudTraceExtension.AddGoogleTrace(services, options =>
{
options.ProjectId = config["Google:ProjectId"];
options.Options = TraceOptions.Create(double.MaxValue, BufferOptions.NoBuffer(), RetryOptions.NoRetry(ExceptionHandling.Propagate));
});
services.AddScoped(ProvideZipkinTraceContext);
services.AddSingleton<Action<HttpResponse, ITraceContext>>((response, traceContext) =>
response.Headers.Add("some_header", traceContext.TraceId));
}
private static ITraceContext ProvideZipkinTraceContext(IServiceProvider serviceProvider)
{
var httpContext = serviceProvider.GetRequiredService<IHttpContextAccessor>().HttpContext;
var adapter = ZipkinTraceAdapter.FromHeaders(httpContext.Request.Headers);
return adapter;
}
// And then, you need to configure the middleware on your Startup.cs or similar.
Google.Cloud.Diagnostics.AspNetCore(3)
public void Configure(IApplicationBuilder app, ...)
{
// Use at the start of the request pipeline to ensure the entire
// request is traced.
app.UseGoogleTrace();
/// ...
} |
On a separate note, I think this issue is very overloaded and at the point where it's not easy to follow, even for all of us that have been on it from the beginning. |
The documentation all assumes aspnetcore, but what about worker services? Right now, I'm assuming that I still have to bring in those asp libraries to enable diagnostics, but I cannot use the .UseGoogleDiagnostics() installer since there is no webhostbuilder, and I would instead have to install the products individually. Also, this forces me to bring asp dependencies into the service which is not idea.
The text was updated successfully, but these errors were encountered: