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

Update instrumentors to use span processors #852

Merged
merged 8 commits into from
Jul 8, 2020

Conversation

ocelotl
Copy link
Contributor

@ocelotl ocelotl commented Jun 24, 2020

Fixes #832

This can be implemented as a fix right now, but the issue highlights a more pressing matter, the fact that there is an overlap between configuration in the standard sense (the kind that uses Configuration) and the kind that is implemented by the end user as application code.

@ocelotl ocelotl requested a review from a team June 24, 2020 05:29
@ocelotl ocelotl marked this pull request as draft June 24, 2020 05:31
@lzchen
Copy link
Contributor

lzchen commented Jun 24, 2020

Can you explain a bit more about what the issue is and how we are solving it? Is this a quick fix or a permanent solution? I also am confused with this: the fact that there is an overlap between configuration in the standard sense (the kind that uses Configuration) and the kind that is implemented by the end user as application code, how does this relate to the problem? An example would be nice.

@ocelotl
Copy link
Contributor Author

ocelotl commented Jun 24, 2020

Can you explain a bit more about what the issue is and how we are solving it? Is this a quick fix or a permanent solution? I also am confused with this: the fact that there is an overlap between configuration in the standard sense (the kind that uses Configuration) and the kind that is implemented by the end user as application code, how does this relate to the problem? An example would be nice.

Sure, the problem here happens with auto instrumentation. The example from @mat-rumian in #832 used auto instrumentation to launch several scripts. Among them, this code was getting executed:

from opentelemetry import trace
from opentelemetry.ext.jaeger import JaegerSpanExporter
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import (
    ConsoleSpanExporter,
    SimpleExportSpanProcessor,
    BatchExportSpanProcessor,
)
import opentelemetry.ext.requests


def configure_jaeger_span_exporter(configuration: dict):
    exporter = JaegerSpanExporter(
        service_name=configuration['service_name'],
        agent_host_name=configuration['agent_host'],
        agent_port=int(configuration['agent_port']),
    )

    trace.set_tracer_provider(TracerProvider())
    trace.get_tracer(__name__)

    span_processor = BatchExportSpanProcessor(exporter)
    trace.get_tracer_provider().add_span_processor(span_processor)

    trace.get_tracer_provider().add_span_processor(
        SimpleExportSpanProcessor(ConsoleSpanExporter())
    )

Span processors were added to the tracer, but this happened after the auto instrumentation process happened. In this process, a tracer is created (without span processors). This object is stored in a function as a closure: here and here.

So, the requests and the dbapi instrumentations end up using this tracer object that lacks the span processors and that makes their spans not to be exported and that is why they were not showing up in the Jaeger exporter.

This is still a draft, it could be added as a fix in the instrumentations, it will solve the issue in these two instrumentations but I think there is a bigger issue, the fact that some configuration can happen in the environment variables (configuration that is accessible to the auto instrumentation process) and some can happen in the application code (configuration that is not accessible to the auto instrumentation process, like the addition of span processors in the application code).

@ocelotl ocelotl self-assigned this Jun 24, 2020
@ocelotl ocelotl marked this pull request as ready for review June 25, 2020 00:37
@ocelotl ocelotl changed the title Add fix in instrumentors Update instrumentors to use span processors Jun 25, 2020
@ocelotl ocelotl added ext instrumentation Related to the instrumentation of third party libraries or frameworks labels Jun 25, 2020
@ocelotl ocelotl requested review from lzchen and codeboten June 25, 2020 02:05
@toumorokoshi
Copy link
Member

Yeah, this is a behavioral issue that has bitten me a little bit to. You have to be very careful with the ordering of the creation of the required components:

  1. named tracer, which binds to whatever trace provider (noop by default)
  2. instrumentation, which can be designed to create a name tracer on instrumentation right away, or deferred
  3. trace provider.

To make this fool-proof, we need to ensure creation of the TracerProvider before auto-instrumentation. This is hard to do, because one typically configures their TraceProvider programatically, after the application initializes.

I agree with @ocelotl that the best option is to defer tracer creation until we absolutely have to. Not sure how we can codify it though, besides documenting that practice somewhere and hoping that others will follow it.

Copy link
Member

@toumorokoshi toumorokoshi left a comment

Choose a reason for hiding this comment

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

I think there's optimizations for processor creation, but there may be value in tackling that at a higher level.

This does resolve a critical issue with trace configuration with auto-instrumentation. Thanks for the fix!

Copy link
Contributor

@lzchen lzchen left a comment

Choose a reason for hiding this comment

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

Nice

@ocelotl ocelotl force-pushed the issue_832 branch 2 times, most recently from bae0674 to f104639 Compare July 7, 2020 01:06
@toumorokoshi toumorokoshi merged commit 09df35c into open-telemetry:master Jul 8, 2020
cnnradams pushed a commit to cnnradams/opentelemetry-python that referenced this pull request Jul 23, 2020
Fixes open-telemetry#832. By having tracer creation occur on demand, late tracer provider configuration will be honored. 

This resolves issues with instrumentation occurring before tracer providers are set by the application developer, which would result in the no-op tracer used for the lifetime of the instrumentation.

Co-authored-by: alrex <[email protected]>
Co-authored-by: Leighton Chen <[email protected]>
Co-authored-by: Yusuke Tsutsumi <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
instrumentation Related to the instrumentation of third party libraries or frameworks
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Missing spans in case of multi library auto-instrumented project
3 participants