Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method and benchmarks for pooling metric options #6394

Merged
merged 16 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Added support exporting logs via OTLP over gRPC in `go.opentelemetry.io/contrib/config`. (#6340)
- The `go.opentelemetry.io/contrib/bridges/otellogr` module.
This module provides an OpenTelemetry logging bridge for `github.com/go-logr/logr`. (#6386)
- Use a pool for the semantic convention metrics options in `go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp`. (#6394)
MrAlias marked this conversation as resolved.
Show resolved Hide resolved

### Changed

Expand Down
22 changes: 16 additions & 6 deletions instrumentation/net/http/otelhttp/internal/semconv/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/http"
"os"
"strings"
"sync"

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
Expand Down Expand Up @@ -102,18 +103,27 @@ type MetricData struct {
ElapsedTime float64
}

var metricAddOptionPool = &sync.Pool{
New: func() interface{} {
return &[]metric.AddOption{}
},
}

func (s HTTPServer) RecordMetrics(ctx context.Context, md ServerMetricData) {
if s.requestBytesCounter == nil || s.responseBytesCounter == nil || s.serverLatencyMeasure == nil {
// This will happen if an HTTPServer{} is used instead of NewHTTPServer.
// This will happen if an HTTPServer{} is used insted of NewHTTPServer.
MrAlias marked this conversation as resolved.
Show resolved Hide resolved
return
}

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...)
s.responseBytesCounter.Add(ctx, md.ResponseSize, addOpts...)
addOpts := metricAddOptionPool.Get().(*[]metric.AddOption)
vordimous marked this conversation as resolved.
Show resolved Hide resolved
*addOpts = append(*addOpts, o)
s.requestBytesCounter.Add(ctx, md.RequestSize, *addOpts...)
s.responseBytesCounter.Add(ctx, md.ResponseSize, *addOpts...)
s.serverLatencyMeasure.Record(ctx, md.ElapsedTime, o)
*addOpts = (*addOpts)[:0]
metricAddOptionPool.Put(addOpts)

// TODO: Duplicate Metrics
}
Expand Down Expand Up @@ -206,7 +216,7 @@ func (c HTTPClient) MetricOptions(ma MetricAttributes) MetricOpts {

func (s HTTPClient) RecordMetrics(ctx context.Context, md MetricData, opts MetricOpts) {
if s.requestBytesCounter == nil || s.latencyMeasure == nil {
// This will happen if an HTTPClient{} is used instead of NewHTTPClient().
// This will happen if an HTTPClient{} is used insted of NewHTTPClient().
MrAlias marked this conversation as resolved.
Show resolved Hide resolved
return
}

Expand All @@ -218,7 +228,7 @@ func (s HTTPClient) RecordMetrics(ctx context.Context, md MetricData, opts Metri

func (s HTTPClient) RecordResponseSize(ctx context.Context, responseData int64, opts metric.AddOption) {
if s.responseBytesCounter == nil {
// This will happen if an HTTPClient{} is used instead of NewHTTPClient().
// This will happen if an HTTPClient{} is used insted of NewHTTPClient().
MrAlias marked this conversation as resolved.
Show resolved Hide resolved
return
}

Expand Down
39 changes: 39 additions & 0 deletions instrumentation/net/http/otelhttp/internal/semconv/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,42 @@ func NewTestHTTPClient() HTTPClient {
latencyMeasure: &testInst{},
}
}

func BenchmarkRecordMetrics(b *testing.B) {
benchmarks := []struct {
name string
server HTTPServer
}{
{
name: "empty",
server: HTTPServer{},
},
{
name: "nil meter",
server: NewHTTPServer(nil),
},
{
name: "with Meter",
server: NewHTTPServer(noop.Meter{}),
},
}

for _, bm := range benchmarks {
b.Run(bm.name, func(b *testing.B) {
req, _ := http.NewRequest("GET", "http://example.com", nil)
_ = bm.server.RequestTraceAttrs("stuff", req)
_ = bm.server.ResponseTraceAttrs(ResponseTelemetry{StatusCode: 200})
ctx := context.Background()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
vordimous marked this conversation as resolved.
Show resolved Hide resolved
bm.server.RecordMetrics(ctx, ServerMetricData{
ServerName: bm.name,
MetricAttributes: MetricAttributes{
Req: req,
},
})
}
})
}
}
Loading