Skip to content

Commit

Permalink
Switch the otelhttp semconv tests to use metricdatatest (#6400)
Browse files Browse the repository at this point in the history
This switches the otelhttp semconv tests to use the
[metricdatatest](https://pkg.go.dev/go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest)
package rather than a custom struct with a partial metrics
implementation.

This is meant to make it easier to start implementing the client and
server metrics.

---------

Co-authored-by: Robert Pająk <[email protected]>
Co-authored-by: Tyler Yahn <[email protected]>
  • Loading branch information
3 people authored Dec 10, 2024
1 parent 637e1e5 commit 5139bd7
Show file tree
Hide file tree
Showing 14 changed files with 758 additions and 608 deletions.
155 changes: 0 additions & 155 deletions instrumentation/net/http/otelhttp/internal/semconv/common_test.go

This file was deleted.

28 changes: 14 additions & 14 deletions instrumentation/net/http/otelhttp/internal/semconv/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,24 +50,24 @@ type HTTPServer struct {
// The req Host will be used to determine the server instead.
func (s HTTPServer) RequestTraceAttrs(server string, req *http.Request) []attribute.KeyValue {
if s.duplicate {
return append(oldHTTPServer{}.RequestTraceAttrs(server, req), newHTTPServer{}.RequestTraceAttrs(server, req)...)
return append(OldHTTPServer{}.RequestTraceAttrs(server, req), CurrentHTTPServer{}.RequestTraceAttrs(server, req)...)
}
return oldHTTPServer{}.RequestTraceAttrs(server, req)
return OldHTTPServer{}.RequestTraceAttrs(server, req)
}

// ResponseTraceAttrs returns trace attributes for telemetry from an HTTP response.
//
// If any of the fields in the ResponseTelemetry are not set the attribute will be omitted.
func (s HTTPServer) ResponseTraceAttrs(resp ResponseTelemetry) []attribute.KeyValue {
if s.duplicate {
return append(oldHTTPServer{}.ResponseTraceAttrs(resp), newHTTPServer{}.ResponseTraceAttrs(resp)...)
return append(OldHTTPServer{}.ResponseTraceAttrs(resp), CurrentHTTPServer{}.ResponseTraceAttrs(resp)...)
}
return oldHTTPServer{}.ResponseTraceAttrs(resp)
return OldHTTPServer{}.ResponseTraceAttrs(resp)
}

// Route returns the attribute for the route.
func (s HTTPServer) Route(route string) attribute.KeyValue {
return oldHTTPServer{}.Route(route)
return OldHTTPServer{}.Route(route)
}

// Status returns a span status code and message for an HTTP status code
Expand Down Expand Up @@ -108,7 +108,7 @@ func (s HTTPServer) RecordMetrics(ctx context.Context, md ServerMetricData) {
return
}

attributes := oldHTTPServer{}.MetricAttributes(md.ServerName, md.Req, md.StatusCode, md.AdditionalAttributes)
attributes := OldHTTPServer{}.MetricAttributes(md.ServerName, md.Req, md.StatusCode, md.AdditionalAttributes)
o := metric.WithAttributeSet(attribute.NewSet(attributes...))
addOpts := []metric.AddOption{o}
s.requestBytesCounter.Add(ctx, md.RequestSize, addOpts...)
Expand All @@ -124,7 +124,7 @@ func NewHTTPServer(meter metric.Meter) HTTPServer {
server := HTTPServer{
duplicate: duplicate,
}
server.requestBytesCounter, server.responseBytesCounter, server.serverLatencyMeasure = oldHTTPServer{}.createMeasures(meter)
server.requestBytesCounter, server.responseBytesCounter, server.serverLatencyMeasure = OldHTTPServer{}.createMeasures(meter)
return server
}

Expand All @@ -142,25 +142,25 @@ func NewHTTPClient(meter metric.Meter) HTTPClient {
client := HTTPClient{
duplicate: env == "http/dup",
}
client.requestBytesCounter, client.responseBytesCounter, client.latencyMeasure = oldHTTPClient{}.createMeasures(meter)
client.requestBytesCounter, client.responseBytesCounter, client.latencyMeasure = OldHTTPClient{}.createMeasures(meter)
return client
}

// RequestTraceAttrs returns attributes for an HTTP request made by a client.
func (c HTTPClient) RequestTraceAttrs(req *http.Request) []attribute.KeyValue {
if c.duplicate {
return append(oldHTTPClient{}.RequestTraceAttrs(req), newHTTPClient{}.RequestTraceAttrs(req)...)
return append(OldHTTPClient{}.RequestTraceAttrs(req), CurrentHTTPClient{}.RequestTraceAttrs(req)...)
}
return oldHTTPClient{}.RequestTraceAttrs(req)
return OldHTTPClient{}.RequestTraceAttrs(req)
}

// ResponseTraceAttrs returns metric attributes for an HTTP request made by a client.
func (c HTTPClient) ResponseTraceAttrs(resp *http.Response) []attribute.KeyValue {
if c.duplicate {
return append(oldHTTPClient{}.ResponseTraceAttrs(resp), newHTTPClient{}.ResponseTraceAttrs(resp)...)
return append(OldHTTPClient{}.ResponseTraceAttrs(resp), CurrentHTTPClient{}.ResponseTraceAttrs(resp)...)
}

return oldHTTPClient{}.ResponseTraceAttrs(resp)
return OldHTTPClient{}.ResponseTraceAttrs(resp)
}

func (c HTTPClient) Status(code int) (codes.Code, string) {
Expand All @@ -175,7 +175,7 @@ func (c HTTPClient) Status(code int) (codes.Code, string) {

func (c HTTPClient) ErrorType(err error) attribute.KeyValue {
if c.duplicate {
return newHTTPClient{}.ErrorType(err)
return CurrentHTTPClient{}.ErrorType(err)
}

return attribute.KeyValue{}
Expand All @@ -195,7 +195,7 @@ func (o MetricOpts) AddOptions() metric.AddOption {
}

func (c HTTPClient) MetricOptions(ma MetricAttributes) MetricOpts {
attributes := oldHTTPClient{}.MetricAttributes(ma.Req, ma.StatusCode, ma.AdditionalAttributes)
attributes := OldHTTPClient{}.MetricAttributes(ma.Req, ma.StatusCode, ma.AdditionalAttributes)
// TODO: Duplicate Metrics
set := metric.WithAttributeSet(attribute.NewSet(attributes...))
return MetricOpts{
Expand Down
42 changes: 0 additions & 42 deletions instrumentation/net/http/otelhttp/internal/semconv/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ import (

"github.com/stretchr/testify/require"

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/embedded"
"go.opentelemetry.io/otel/metric/noop"
)

Expand Down Expand Up @@ -93,42 +90,3 @@ func TestHTTPClientDoesNotPanic(t *testing.T) {
})
}
}

type testInst struct {
embedded.Int64Counter
embedded.Float64Histogram

intValue int64
floatValue float64
attributes []attribute.KeyValue
}

func (t *testInst) Add(ctx context.Context, incr int64, options ...metric.AddOption) {
t.intValue = incr
cfg := metric.NewAddConfig(options)
attr := cfg.Attributes()
t.attributes = attr.ToSlice()
}

func (t *testInst) Record(ctx context.Context, value float64, options ...metric.RecordOption) {
t.floatValue = value
cfg := metric.NewRecordConfig(options)
attr := cfg.Attributes()
t.attributes = attr.ToSlice()
}

func NewTestHTTPServer() HTTPServer {
return HTTPServer{
requestBytesCounter: &testInst{},
responseBytesCounter: &testInst{},
serverLatencyMeasure: &testInst{},
}
}

func NewTestHTTPClient() HTTPClient {
return HTTPClient{
requestBytesCounter: &testInst{},
responseBytesCounter: &testInst{},
latencyMeasure: &testInst{},
}
}
Loading

0 comments on commit 5139bd7

Please sign in to comment.