Skip to content

Commit

Permalink
[KFS-2199] extended the webserver to be dynamic so endpoint can be ad…
Browse files Browse the repository at this point in the history
…ded and removed at runtime
  • Loading branch information
luca-filipponi committed Jul 10, 2024
1 parent 21fbc0f commit 4e2fa69
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 17 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

- Support for stdoutlog exporter in `go.opentelemetry.io/contrib/config`. (#5850)
- Add macOS ARM64 platform to the compatibility testing suite. (#5868)
- Added option for adding default attributes to otel http transport in `go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp`. (#5876)
- Added option for extracting attributes from the http request in http transport in `go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp`. (#5876)

### Removed

Expand Down
12 changes: 7 additions & 5 deletions instrumentation/net/http/otelhttp/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ type config struct {
ClientTrace func(context.Context) *httptrace.ClientTrace
Attributes []attribute.KeyValue

TracerProvider trace.TracerProvider
MeterProvider metric.MeterProvider
TracerProvider trace.TracerProvider
MeterProvider metric.MeterProvider
ReqAttributesMapper func(*http.Request) []attribute.KeyValue
}

// Option interface used for setting optional config properties.
Expand Down Expand Up @@ -198,9 +199,10 @@ func WithServerName(server string) Option {
})
}

// WithAttributes returns an option that sets of attributes to be always included in metrics.
func WithAttributes(attributes []attribute.KeyValue) Option {
// WithReqAttributesMapper returns an Option to set a function that maps an HTTP request to a slice of attribute.KeyValue.
// These attributes will be included in metrics for every request.
func WithReqAttributesMapper(reqAttributesMapper func(r *http.Request) []attribute.KeyValue) Option {
return optionFunc(func(c *config) {
c.Attributes = attributes
c.ReqAttributesMapper = reqAttributesMapper
})
}
4 changes: 3 additions & 1 deletion instrumentation/net/http/otelhttp/test/transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,9 @@ func TestDefaultAttributesHandling(t *testing.T) {

transport := otelhttp.NewTransport(
http.DefaultTransport, otelhttp.WithMeterProvider(provider),
otelhttp.WithAttributes(defaultAttributes))
otelhttp.WithReqAttributesMapper(func(_ *http.Request) []attribute.KeyValue {
return defaultAttributes
}))
client := http.Client{Transport: transport}

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
Expand Down
28 changes: 18 additions & 10 deletions instrumentation/net/http/otelhttp/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ import (
type Transport struct {
rt http.RoundTripper

tracer trace.Tracer
meter metric.Meter
propagators propagation.TextMapPropagator
spanStartOptions []trace.SpanStartOption
filters []Filter
spanNameFormatter func(string, *http.Request) string
clientTrace func(context.Context) *httptrace.ClientTrace
attributes []attribute.KeyValue
tracer trace.Tracer
meter metric.Meter
propagators propagation.TextMapPropagator
spanStartOptions []trace.SpanStartOption
filters []Filter
spanNameFormatter func(string, *http.Request) string
clientTrace func(context.Context) *httptrace.ClientTrace
reqAttributesMapper func(*http.Request) []attribute.KeyValue

semconv semconv.HTTPClient
requestBytesCounter metric.Int64Counter
Expand Down Expand Up @@ -80,7 +80,7 @@ func (t *Transport) applyConfig(c *config) {
t.filters = c.Filters
t.spanNameFormatter = c.SpanNameFormatter
t.clientTrace = c.ClientTrace
t.attributes = c.Attributes
t.reqAttributesMapper = c.ReqAttributesMapper
}

func (t *Transport) createMeasures() {
Expand Down Expand Up @@ -172,7 +172,7 @@ func (t *Transport) RoundTrip(r *http.Request) (*http.Response, error) {
}

// metrics
metricAttrs := append(append(labeler.Get(), semconvutil.HTTPClientRequestMetrics(r)...), t.attributes...)
metricAttrs := append(append(labeler.Get(), semconvutil.HTTPClientRequestMetrics(r)...), t.attributesFromRequest(r)...)
if res.StatusCode > 0 {
metricAttrs = append(metricAttrs, semconv.HTTPStatusCode(res.StatusCode))
}
Expand All @@ -198,6 +198,14 @@ func (t *Transport) RoundTrip(r *http.Request) (*http.Response, error) {
return res, err
}

func (t *Transport) attributesFromRequest(r *http.Request) []attribute.KeyValue {
var attributeForRequest []attribute.KeyValue
if t.reqAttributesMapper != nil {
attributeForRequest = t.reqAttributesMapper(r)
}
return attributeForRequest
}

// newWrappedBody returns a new and appropriately scoped *wrappedBody as an
// io.ReadCloser. If the passed body implements io.Writer, the returned value
// will implement io.ReadWriteCloser.
Expand Down

0 comments on commit 4e2fa69

Please sign in to comment.