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

Establish a trace context in a non-http context #9856

Closed
christopheblin opened this issue Feb 8, 2023 · 5 comments
Closed

Establish a trace context in a non-http context #9856

christopheblin opened this issue Feb 8, 2023 · 5 comments
Assignees
Labels
api: cloudtrace Issues related to the Cloud Trace API. needs more info This issue needs more information from the customer to proceed. type: question Request for information or clarification. Not an issue.

Comments

@christopheblin
Copy link

I've seen #5897 and #9157 and #9476 and https://cloud.google.com/dotnet/docs/reference/Google.Cloud.Diagnostics.AspNetCore3/latest#custom-trace-context-example

However, after multiple tries, I'm not able to wrap my head around ITraceContext when I want both the "default" context (i.e for HTTP context) and my own context for non-http context, so I'm asking for help here...

Basically, I have a cloud run with an HTTP API where the default "integration" is OK (i.e when I do _logger.LogInformation("xxx"), i can see this log statement when I do "Show logs for the trace" from logs explorer)

Now, this cloud run also connects to ActiveMq, and I do not manage to create a new trace for each message received

Here is where I am stuck :

    private readonly IServiceProvider _sp;    

    //omitted details to establish connection to ActiveMq, jsut showing the method to handle one message

    private void Consume(IMessage message)
    {
        using var scope = _sp.CreateScope();
        //TODO: correctly configure GCP tracing & logging
        var logger = scope.ServiceProvider.GetRequiredService<ILogger<ActiveMQConsumer>>();
        var txtMessage = message as ITextMessage ?? throw new IllegalStateException("not an ITextMessage, impossible !!!");
        logger.LogInformation("received ActiveMQ message {}", txtMessage.Text);        
    }

I can provide details about what I've tried if necessary

@jskeet jskeet added type: question Request for information or clarification. Not an issue. api: cloudtrace Issues related to the Cloud Trace API. labels Feb 8, 2023
@amanda-tarafa
Copy link
Contributor

I'll try to reply tomorrow at some point, else on Friday.

@amanda-tarafa
Copy link
Contributor

Ok, if I understand your question correctly you need to look at docs on Establishing traces on non HTTP requests when you want to trace your ActiveMq activity (know that I'm not at all familiar with ActiveMq). So, integrating the code example from that link into your code, ir could look something like this:

private void Consume(IMessage message)
{
    using var scope = _sp.CreateScope();
    EstablishTrace(scope, message);
    var logger = scope.ServiceProvider.GetRequiredService<ILogger<ActiveMQConsumer>>();
    var txtMessage = message as ITextMessage ?? throw new IllegalStateException("not an ITextMessage, impossible !!!");
    logger.LogInformation("received ActiveMQ message {}", txtMessage.Text);        
}

private void EstablishTrace(IServiceScope scope, IMessage message)
{
    ITraceContext traceContext = GetTraceContextFromIncomingMessage(message);
    var tracerFactory = scope.GetRequiredService<Func<ITraceContext, IManagedTracer>>();
    IManagedTracer tracer = tracerFactory(traceContext);
    ContextTracerManager.SetCurrentTracer(tracer);
}

private ITraceContext GetTraceContextFromIncomingMessage(IMessage message)
{
    // You need to extract the trace context from the incoming message and create
    // an instance of ITraceContext (you can use Google.Cloud.Diagnostics.Common.SimpleTraceContext).
    // If the incoming message does not contain a trace context, you can create a new one by creating a new
   // trace ID.
}

Let me know if this helps.

@christopheblin
Copy link
Author

HI @amanda-tarafa

I tried the solution and now I do not see logs anymore on GCP 😒

However, I did fallback to Console.WriteLine and I do not see that either, meaning the server is simply not pulling messsages anymore so I really have another problem somewhere 😱

I've yet to find some time to understand what is going on before bothering you, but be assured that I will report back here as soon as possible 😄

Anyway, the code I added is the following :

    private void Consume(IMessage message)
    {       
        using var scope = _sp.CreateScope();
        EstablishTrace(scope, message);
        var logger = scope.ServiceProvider.GetRequiredService<ILogger<ActiveMQConsumer>>();
        var txtMessage = message as ITextMessage ?? throw new IllegalStateException("cannot deconstruct message (should not happen)");
        Console.WriteLine("Received from console {}", txtMessage.Text);
        logger.LogInformation("Received from logger {}", txtMessage.Text);
    }

    private void EstablishTrace(IServiceScope scope, IMessage message)
    {
        //see https://github.com/googleapis/google-cloud-dotnet/issues/9856#issuecomment-1425617476
        var tracerFactory = scope.ServiceProvider.GetService<Func<ITraceContext, IManagedTracer>>();
        if (tracerFactory == null) //GCP tracing not available
            return;
        Console.WriteLine("Enabling GCP tracing");
        var traceContext = new SimpleTraceContext(Guid.NewGuid().ToString().Replace("-", ""), null, true);
        var tracer = tracerFactory(traceContext);
        ContextTracerManager.SetCurrentTracer(tracer);
    }

@amanda-tarafa
Copy link
Contributor

OK, let me know when you know more and I can help then.

@amanda-tarafa amanda-tarafa added the needs more info This issue needs more information from the customer to proceed. label Feb 23, 2023
@amanda-tarafa amanda-tarafa added needs more info This issue needs more information from the customer to proceed. and removed needs more info This issue needs more information from the customer to proceed. labels Mar 31, 2023
@amanda-tarafa
Copy link
Contributor

@christopheblin I'll be closing this one until you are ready to provide more information, and that now seems to depend on #9948 . Happy to reopen as soon as you are ready.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: cloudtrace Issues related to the Cloud Trace API. needs more info This issue needs more information from the customer to proceed. type: question Request for information or clarification. Not an issue.
Projects
None yet
Development

No branches or pull requests

3 participants