forked from open-telemetry/opentelemetry-go-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HTTP Semconv migration Part1 Server Metrics - v1.20.0 support (open-t…
…elemetry#5818) This change moves the metric creation and use into the semconv package. This is because metrics names are defined by the semantic convention, so to be able to change them seamlessly they should be within the scope of the semconv package. --------- Co-authored-by: Aaron Clawson <[email protected]> Co-authored-by: Tyler Yahn <[email protected]>
- Loading branch information
1 parent
b9bb2f3
commit 9e8f1ec
Showing
9 changed files
with
300 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
instrumentation/net/http/otelhttp/internal/semconv/env_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package semconv | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"testing" | ||
|
||
"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" | ||
) | ||
|
||
func TestHTTPServerDoesNotPanic(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
server HTTPServer | ||
}{ | ||
{ | ||
name: "empty", | ||
server: HTTPServer{}, | ||
}, | ||
{ | ||
name: "nil meter", | ||
server: NewHTTPServer(nil), | ||
}, | ||
{ | ||
name: "with Meter", | ||
server: NewHTTPServer(noop.Meter{}), | ||
}, | ||
} | ||
for _, tt := range testCases { | ||
t.Run(tt.name, func(t *testing.T) { | ||
require.NotPanics(t, func() { | ||
req, err := http.NewRequest("GET", "http://example.com", nil) | ||
require.NoError(t, err) | ||
|
||
_ = tt.server.RequestTraceAttrs("stuff", req) | ||
_ = tt.server.ResponseTraceAttrs(ResponseTelemetry{StatusCode: 200}) | ||
tt.server.RecordMetrics(context.Background(), MetricData{ | ||
ServerName: "stuff", | ||
Req: req, | ||
}) | ||
}) | ||
}) | ||
} | ||
} | ||
|
||
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{}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.