Skip to content

Commit

Permalink
feat(otelhttp): generate New Server Metrics in otelhttp
Browse files Browse the repository at this point in the history
  • Loading branch information
flc1125 committed Dec 9, 2024
1 parent f62983d commit 35c0460
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 41 deletions.
16 changes: 8 additions & 8 deletions instrumentation/net/http/otelhttp/internal/semconv/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,14 @@ func (t *testRecorder[T]) Record(_ context.Context, value T, options ...metric.R
}

func NewTestHTTPServer() HTTPServer {
return HTTPServer{
requestBytesCounter: &testRecorder[int64]{},
responseBytesCounter: &testRecorder[int64]{},
serverLatencyMeasure: &testRecorder[float64]{},
requestBodySizeHistogram: &testRecorder[int64]{},
responseBodySizeHistogram: &testRecorder[int64]{},
requestDurationHistogram: &testRecorder[float64]{},
}
httpServer := NewHTTPServer(nil)
httpServer.requestBytesCounter = &testRecorder[int64]{}
httpServer.responseBytesCounter = &testRecorder[int64]{}
httpServer.serverLatencyMeasure = &testRecorder[float64]{}
httpServer.requestBodySizeHistogram = &testRecorder[int64]{}
httpServer.responseBodySizeHistogram = &testRecorder[int64]{}
httpServer.requestDurationHistogram = &testRecorder[float64]{}
return httpServer
}

func NewTestHTTPClient() HTTPClient {
Expand Down
98 changes: 65 additions & 33 deletions instrumentation/net/http/otelhttp/internal/semconv/httpconv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,46 +97,78 @@ func TestNewTraceResponse(t *testing.T) {
}

func TestNewRecordMetrics(t *testing.T) {
t.Setenv(OTelSemConvStabilityOptIn, "http/dup")
server := NewTestHTTPServer()
server.duplicate = true
req, err := http.NewRequest("POST", "http://example.com", nil)
assert.NoError(t, err)
tests := []struct {
name string
setEnv bool
expectedFunc func(server HTTPServer, t *testing.T)
}{
{
name: "set env",
setEnv: true,
expectedFunc: func(server HTTPServer, t *testing.T) {
assert.Equal(t, int64(100), server.requestBodySizeHistogram.(*testRecorder[int64]).value)
assert.Equal(t, int64(200), server.responseBodySizeHistogram.(*testRecorder[int64]).value)
assert.Equal(t, float64(300), server.requestDurationHistogram.(*testRecorder[float64]).value)

want := []attribute.KeyValue{
attribute.String("url.scheme", "http"),
attribute.String("http.request.method", "POST"),
attribute.Int64("http.response.status_code", 301),
attribute.String("key", "value"),
attribute.String("server.address", "stuff"),
attribute.String("network.protocol.name", "http"),
attribute.String("network.protocol.version", "1.1"),
}

server.RecordMetrics(context.Background(), ServerMetricData{
ServerName: "stuff",
ResponseSize: 200,
MetricAttributes: MetricAttributes{
Req: req,
StatusCode: 301,
AdditionalAttributes: []attribute.KeyValue{
attribute.String("key", "value"),
assert.ElementsMatch(t, want, server.requestBodySizeHistogram.(*testRecorder[int64]).attributes)
assert.ElementsMatch(t, want, server.responseBodySizeHistogram.(*testRecorder[int64]).attributes)
assert.ElementsMatch(t, want, server.requestDurationHistogram.(*testRecorder[float64]).attributes)
},
},
MetricData: MetricData{
RequestSize: 100,
ElapsedTime: 300,
{
name: "not set env",
setEnv: false,
expectedFunc: func(server HTTPServer, t *testing.T) {
assert.Equal(t, int64(0), server.requestBodySizeHistogram.(*testRecorder[int64]).value)
assert.Equal(t, int64(0), server.responseBodySizeHistogram.(*testRecorder[int64]).value)
assert.Equal(t, float64(0), server.requestDurationHistogram.(*testRecorder[float64]).value)

assert.Len(t, server.requestBodySizeHistogram.(*testRecorder[int64]).attributes, 0)
assert.Len(t, server.responseBodySizeHistogram.(*testRecorder[int64]).attributes, 0)
assert.Len(t, server.requestDurationHistogram.(*testRecorder[float64]).attributes, 0)
},
},
})
}

assert.Equal(t, int64(100), server.requestBodySizeHistogram.(*testRecorder[int64]).value)
assert.Equal(t, int64(200), server.responseBodySizeHistogram.(*testRecorder[int64]).value)
assert.Equal(t, float64(300), server.requestDurationHistogram.(*testRecorder[float64]).value)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.setEnv {
t.Setenv(OTelSemConvStabilityOptIn, "http/dup")
}

want := []attribute.KeyValue{
attribute.String("http.scheme", "http"),
attribute.String("http.method", "POST"),
attribute.Int64("http.status_code", 301),
attribute.String("key", "value"),
attribute.String("net.host.name", "stuff"),
attribute.String("net.protocol.name", "http"),
attribute.String("net.protocol.version", "1.1"),
}
_ = want
server := NewTestHTTPServer()
req, err := http.NewRequest("POST", "http://example.com", nil)
assert.NoError(t, err)

// assert.ElementsMatch(t, want, server.requestBodySizeHistogram.(*testRecorder[int64]).attributes)
// assert.ElementsMatch(t, want, server.responseBodySizeHistogram.(*testRecorder[int64]).attributes)
// assert.ElementsMatch(t, want, server.requestDurationHistogram.(*testRecorder[float64]).attributes)
server.RecordMetrics(context.Background(), ServerMetricData{
ServerName: "stuff",
ResponseSize: 200,
MetricAttributes: MetricAttributes{
Req: req,
StatusCode: 301,
AdditionalAttributes: []attribute.KeyValue{
attribute.String("key", "value"),
},
},
MetricData: MetricData{
RequestSize: 100,
ElapsedTime: 300,
},
})

tt.expectedFunc(server, t)
})
}
}

func TestNewMethod(t *testing.T) {
Expand Down

0 comments on commit 35c0460

Please sign in to comment.