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

Adding attributes to an existing span in auto (or programmatically) instrumented libraries #4394

Closed
rossgrambo opened this issue Jan 23, 2025 · 1 comment

Comments

@rossgrambo
Copy link

rossgrambo commented Jan 23, 2025

Hello! This is hopefully just a question.

We're ensuring our SDK works well with OpenTelemetry. Our guidance in .net has been to use the AutoInstrumentation as much as you can and then add an attributes.extraField. In .net it's easy because the Activity is available to us anywhere.

In python, it's unclear to me if it's possible to add attributes to the "current" span without fully creating your own span in manual instrumentation. For example, I want to advice developers to do something like:

(Example taken from https://opentelemetry.io/docs/languages/python/getting-started/#add-manual-instrumentation-to-automatic-instrumentation)
Existing code:

from random import randint
from flask import Flask

from opentelemetry import trace

# Acquire a tracer
tracer = trace.get_tracer("diceroller.tracer")

app = Flask(__name__)

@app.route("/rolldice")
def roll_dice():
    return str(roll())

def roll():
    # custom code

and add something like:

def roll():
    currentSpan.attribute["somethingImportant"] = generateValue(somethingFromRequest);

(currentSpan referring to the span created for the request by the auto instrumentation)

Is this possible? I don't think a processor works here because I need the request object- which isn't always available everywhere in some frameworks.

@rossgrambo
Copy link
Author

Nvm~

from opentelemetry import trace

trace.get_current_span().set_attribute("Foo", "Bar")

was all I needed.

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

No branches or pull requests

1 participant