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

[Problem] Logging integration with opentelemetry auto instrumentation #4427

Open
g3david405 opened this issue Feb 13, 2025 · 3 comments · May be fixed by #4436
Open

[Problem] Logging integration with opentelemetry auto instrumentation #4427

g3david405 opened this issue Feb 13, 2025 · 3 comments · May be fixed by #4436

Comments

@g3david405
Copy link

g3david405 commented Feb 13, 2025

Is your feature request related to a problem?

I use python auto instrumentation for my FastAPI application.

I want to do zero code insert for opentelemetry like this docs

The log and trace were perfectly send to opentelemetry collector by following instrumentation in kubernetes:

apiVersion: opentelemetry.io/v1alpha1
kind: Instrumentation
metadata:
  name: opentelemetry-instrumentation
  namespace: opentelemetry-operator-system
spec:
  defaults: {}
  exporter:
    endpoint: http://opentelemetry-collector.open-telemetry.svc.cluster.local:4318
  propagators:
    - tracecontext
    - baggage
  python:
    env:
      - name: OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED
        value: 'true'      
    image: >-
      ghcr.io/open-telemetry/opentelemetry-operator/autoinstrumentation-python:0.50b0
    resourceRequirements:
      limits:
        cpu: 500m
        memory: 64Mi
      requests:
        cpu: 50m
        memory: 64Mi
    volumeClaimTemplate:
      metadata: {}
      spec:
        resources: {}
  resource: {}
  sampler:
    argument: '1'
    type: parentbased_traceidratio

If I specified OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED to true, the log which use logging will be send to opentelemetry collector, but disappear in console.

How can I send the log to opentelemetry and print in console by logging using python auto instrumentation?

If possible, I want to maintain the concept of zero code. I don't want to change the application code.

thanks

Describe the solution you'd like

I can use variable or something that I can send logging log to console and opentelemetry collector

Describe alternatives you've considered

No response

Additional Context

No response

Would you like to implement a fix?

None

@g3david405
Copy link
Author

OK I fond the solution,
Just specified env like following

    env:
      - name: OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED
        value: 'true'      
      - name: OTEL_LOGS_EXPORTER
        value: 'console,otlp'

But the log format is not same as before, how can I remain the original log format in console?

@pmcollins
Copy link
Member

If your code calls logging.basicConfig() then that will be a no-op because auto-instrumentation will already have set it up.

You can add a stream handler explicitly like this, then logging should show up on the console as well:

stream_handler = logging.StreamHandler()
stream_handler.setFormatter(logging.Formatter(logging.BASIC_FORMAT, None, "%"))
logging.getLogger().addHandler(stream_handler)

We might want to add a capability like this to this distro. Will bring it up in the SIG.

@g3david405
Copy link
Author

@pmcollins
Thanks man, It works!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants