opencensus-python icon indicating copy to clipboard operation
opencensus-python copied to clipboard

Passing traceids/spanids to library functions and correlating calls

Open anandchugh opened this issue 5 years ago • 6 comments

I have a python library which implements Open census logging.

How to pass traceid from main application to the library functions so that all calls from main application can be correlated with library functions? Also, is it possible to see those calls in Application map. Is there some sample code covering this scenario? Do we also need to take care about passing span-ids?

Here is test code:

Wheel Package contains util.py. This is part of a library This contains some functions like

def test():
  logger.info("Test Log")
  with tracer.span(name="span1"):
     test2()

Now this library function is called from some other application main.py

from util import test

logger = logging.getlogger()
log_exporter = AzureExporter(connection_string=app_insights_cs)
tracer = Tracer(exporter=log_exporter, sampler=AlwaysOnSampler()
with tracer.span(name="test"):
   test()
``

I would like correlate calls from main to test() and then test2() function.

anandchugh avatar Jan 18 '21 21:01 anandchugh

@anandchugh If the tracer used in each function is the same, then the calls from main to test should be automatically correlated. As for test() it it will only be correlated if there is also a custom span (tracer.span) call inside or test2() involves some call that is automatically collected via integrations.

lzchen avatar Jan 19 '21 00:01 lzchen

@lzchen Since library creates a new tracer, so tracer from main and tracer in library are different. However, is there a way to set traceid automatically from main to the tracer in library code so that they can be correlated?

anandchugh avatar Jan 19 '21 04:01 anandchugh

Yes, you can manually set the current context tracer in the library to the one passed in from main.

In util.py

from opencensus.trace.execution_context import set_opencensus_tracer

tracer = <tracer-from-main.py>
set_opencensus_tracer(tracer)

def test():
  logger.info("Test Log")
  with tracer.span(name="span1"):
     test2()

lzchen avatar Jan 19 '21 05:01 lzchen

I tried this and its causing another issue:

I am initializing Tracer in library with ai.cloud.role = util Tracer is initialized in main with ai.cloud.role = main

Now when I pass tracer from main to library I loose cloud role name info of library code and all logs show cloud_RoleName == main, even though I am creating logger in library ai.cloud.role = util

Is it not possible to just set traceid rather than complete tracer in library?

anandchugh avatar Jan 19 '21 08:01 anandchugh

Is it not possible to just set traceid rather than complete tracer in library?

Yes you can use telemetry_processor to modify traceid before export. https://docs.microsoft.com/en-us/azure/azure-monitor/app/api-filtering-sampling#opencensus-python-telemetry-processors

The field you are looking for is envelope.tags['ai.operation.id'].

lzchen avatar Jan 19 '21 17:01 lzchen

Thanks @lzchen I was able to resolve this issue by doing like this:

In main.py, first we can created tracer and pass it to util.py In util.py, create tracer using span_context of passed tracer

parent_tracer = <tracer-from-main.py>
tracer = Tracer(
                span_context=parent_tracer.span_context,
                exporter=log_exporter,
                sampler=sampler,
            )

anandchugh avatar Jan 20 '21 12:01 anandchugh