Skip to content

Commit 0551a23

Browse files
[KFS-2199] extended the webserver to be dynamic so endpoint can be added and removed at runtime
1 parent 21fbc0f commit 0551a23

File tree

3 files changed

+28
-16
lines changed

3 files changed

+28
-16
lines changed

instrumentation/net/http/otelhttp/config.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ type config struct {
3636
ClientTrace func(context.Context) *httptrace.ClientTrace
3737
Attributes []attribute.KeyValue
3838

39-
TracerProvider trace.TracerProvider
40-
MeterProvider metric.MeterProvider
39+
TracerProvider trace.TracerProvider
40+
MeterProvider metric.MeterProvider
41+
ReqAttributesMapper func(*http.Request) []attribute.KeyValue
4142
}
4243

4344
// Option interface used for setting optional config properties.
@@ -198,9 +199,10 @@ func WithServerName(server string) Option {
198199
})
199200
}
200201

201-
// WithAttributes returns an option that sets of attributes to be always included in metrics.
202-
func WithAttributes(attributes []attribute.KeyValue) Option {
202+
// WithReqAttributesMapper returns an Option to set a function that maps an HTTP request to a slice of attribute.KeyValue.
203+
// These attributes will be included in metrics for every request.
204+
func WithReqAttributesMapper(reqAttributesMapper func(r *http.Request) []attribute.KeyValue) Option {
203205
return optionFunc(func(c *config) {
204-
c.Attributes = attributes
206+
c.ReqAttributesMapper = reqAttributesMapper
205207
})
206208
}

instrumentation/net/http/otelhttp/test/transport_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,9 @@ func TestDefaultAttributesHandling(t *testing.T) {
565565

566566
transport := otelhttp.NewTransport(
567567
http.DefaultTransport, otelhttp.WithMeterProvider(provider),
568-
otelhttp.WithAttributes(defaultAttributes))
568+
otelhttp.WithReqAttributesMapper(func(_ *http.Request) []attribute.KeyValue {
569+
return defaultAttributes
570+
}))
569571
client := http.Client{Transport: transport}
570572

571573
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {

instrumentation/net/http/otelhttp/transport.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ import (
2727
type Transport struct {
2828
rt http.RoundTripper
2929

30-
tracer trace.Tracer
31-
meter metric.Meter
32-
propagators propagation.TextMapPropagator
33-
spanStartOptions []trace.SpanStartOption
34-
filters []Filter
35-
spanNameFormatter func(string, *http.Request) string
36-
clientTrace func(context.Context) *httptrace.ClientTrace
37-
attributes []attribute.KeyValue
30+
tracer trace.Tracer
31+
meter metric.Meter
32+
propagators propagation.TextMapPropagator
33+
spanStartOptions []trace.SpanStartOption
34+
filters []Filter
35+
spanNameFormatter func(string, *http.Request) string
36+
clientTrace func(context.Context) *httptrace.ClientTrace
37+
reqAttributesMapper func(*http.Request) []attribute.KeyValue
3838

3939
semconv semconv.HTTPClient
4040
requestBytesCounter metric.Int64Counter
@@ -80,7 +80,7 @@ func (t *Transport) applyConfig(c *config) {
8080
t.filters = c.Filters
8181
t.spanNameFormatter = c.SpanNameFormatter
8282
t.clientTrace = c.ClientTrace
83-
t.attributes = c.Attributes
83+
t.reqAttributesMapper = c.ReqAttributesMapper
8484
}
8585

8686
func (t *Transport) createMeasures() {
@@ -172,7 +172,7 @@ func (t *Transport) RoundTrip(r *http.Request) (*http.Response, error) {
172172
}
173173

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

201+
func (t *Transport) attributesFromRequest(r *http.Request) []attribute.KeyValue {
202+
var attributeForRequest []attribute.KeyValue
203+
if t.reqAttributesMapper != nil {
204+
attributeForRequest = t.reqAttributesMapper(r)
205+
}
206+
return attributeForRequest
207+
}
208+
201209
// newWrappedBody returns a new and appropriately scoped *wrappedBody as an
202210
// io.ReadCloser. If the passed body implements io.Writer, the returned value
203211
// will implement io.ReadWriteCloser.

0 commit comments

Comments
 (0)