open telemetry trace setting , automation and manually change a bit code - unix1998/technical_notes GitHub Wiki

OpenTelemetry provides both automatic and manual instrumentation options, but the degree of manual code modification required can vary by language and framework. Here’s an overview:

OpenTelemetry Automatic Instrumentation

  • Java: OpenTelemetry Java agent provides automatic instrumentation without code changes. You attach the agent at runtime similar to the Datadog agent.
  • Python: OpenTelemetry Python can automatically instrument some libraries using the opentelemetry-auto-instrumentation package. You still need to install the package and configure it, but it reduces the need for manual code changes.

OpenTelemetry Manual Instrumentation

For languages where automatic instrumentation is not fully mature or available, you might need to manually instrument your application by adding tracing code.

Summary by Language

  • Java:
    • Automatic: Yes, using the OpenTelemetry Java agent.
    • Manual: Optional for more custom traces.
  • Python:
    • Automatic: Yes, using the opentelemetry-auto-instrumentation package.
    • Manual: Optional for more custom traces.
  • Node.js:
    • Automatic: Partial support, automatic instrumentation available for some libraries.
    • Manual: Common practice, adding instrumentation code using the OpenTelemetry SDK.
  • Go:
    • Automatic: Limited support, mainly requires manual instrumentation.
    • Manual: Required, using the OpenTelemetry Go SDK.
  • .NET:
    • Automatic: Limited support, mainly requires manual instrumentation.
    • Manual: Required, using the OpenTelemetry .NET SDK.
  • Ruby:
    • Automatic: Limited support, mainly requires manual instrumentation.
    • Manual: Required, using the OpenTelemetry Ruby SDK.
  • PHP:
    • Automatic: Limited support, mainly requires manual instrumentation.
    • Manual: Required, using the OpenTelemetry PHP SDK.

Automatic Instrumentation Example for Python

To automatically instrument a Python application:

  1. Install the OpenTelemetry instrumentation package:
    pip install opentelemetry-instrumentation
    
  2. Run your application with the OpenTelemetry instrument command:
    opentelemetry-instrument python my_application.py
    

Manual Instrumentation Example for Python

For manual instrumentation:

  1. Install the necessary OpenTelemetry packages:
    pip install opentelemetry-api opentelemetry-sdk
    
  2. Add tracing code to your application:
    from opentelemetry import trace
    from opentelemetry.sdk.trace import TracerProvider
    from opentelemetry.sdk.trace.export import SimpleSpanProcessor, ConsoleSpanExporter
    
    trace.set_tracer_provider(TracerProvider())
    tracer = trace.get_tracer(__name__)
    
    span_processor = SimpleSpanProcessor(ConsoleSpanExporter())
    trace.get_tracer_provider().add_span_processor(span_processor)
    
    with tracer.start_as_current_span("my-span"):
        print("Hello, OpenTelemetry!")
    

Summary

  • Java and Python: Both can be instrumented automatically using OpenTelemetry without significant code changes.
  • Other Languages (Node.js, Go, .NET, Ruby, PHP): Generally require more manual instrumentation, though there might be some limited automatic instrumentation support for specific libraries or frameworks.

OpenTelemetry aims to provide broader automatic instrumentation over time, but for now, some languages require more manual setup than others.