Skip to content

Allow ability to add attributes to span with otelhttp span #6769

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

Open
rbkumar88 opened this issue Feb 12, 2025 · 5 comments
Open

Allow ability to add attributes to span with otelhttp span #6769

rbkumar88 opened this issue Feb 12, 2025 · 5 comments
Labels
enhancement New feature or request

Comments

@rbkumar88
Copy link

Problem Statement

Add ability to add custom span attributes to the span with instrumentationScopeName go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp and kind client

Proposed Solution

A clear and concise description of what you want to happen.

Alternatives

A clear and concise description of any alternative solutions or features you've considered.

Prior Art

A clear and concise list of any similar and existing solutions from other projects that provide context to possible solutions.

Additional Context

Add any other context or screenshots about the feature request here.

@rbkumar88 rbkumar88 added the enhancement New feature or request label Feb 12, 2025
@dmathieu
Copy link
Member

Your request is rather unclear.
You can retrieve the current span and add attributes to it at any moment.

span := trace.SpanFromContext(req.Context())
span.SetAttributes(attribute.Bool("isTrue", true), attribute.String("stringAttr", "hi!"))

So all you need to add custom attributes to the current span in HTTP, is a custom handler/middleware that retrieves the span and does what you wish to do.

@rbkumar88
Copy link
Author

@dmathieu i could customize the span name of the span created by otelhttp (instrumentationScopeName: go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp) and (kind: client), the one like below

cloud#region dev  
error true  
http#method POST  
http#request_content_length 109  
http#status_code 404  
http#url http://localhost:8080/  
instrumentationScopeName go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp  
instrumentationScopeVersion 0.59.0  
kind client  
net#peer#name localhost  
net#peer#port 8080  
os#type darwin  
status_code ERROR`

When I try to add like below
span := trace.SpanFromContext(req.Context()) span.SetAttributes(attribute.Bool("isTrue", true), attribute.String("stringAttr", "hi!"))

it is adding the attributes to the parent span, but not to the one above.

@dmathieu
Copy link
Member

If you're talking about the span for an outgoing HTTP call, it's even easier.
You can use the WithSpanOptions option to do just that.

client := http.Client{
  Transport: otelhttp.NewTransport(
    http.DefaultTransport,
    otelhttp.WithSpanOptions(
      trace.WithAttributes(
        attribute.Bool("isTrue", true),
        attribute.String("stringAttr", "hi!"),
      ),
    ),
  ),
}

@rbkumar88
Copy link
Author

rbkumar88 commented Feb 13, 2025

Is there a way to add per request span attributes. for example. i want to extract some fields from request body and set them as span attributes. the above is while initializing the http.Client

@dmathieu
Copy link
Member

A custom HTTP transport would allow you to hook into the middle of the request, and modify the span.

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

No branches or pull requests

2 participants