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

Updated otel to 1.14.0 and otel metrics to 0.37.0 #54

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ go 1.17

require (
github.com/gin-gonic/gin v1.8.1
go.opentelemetry.io/otel v1.11.1
go.opentelemetry.io/otel/metric v0.33.0
go.opentelemetry.io/otel v1.14.0
go.opentelemetry.io/otel/metric v0.37.0
)

require (
Expand All @@ -23,7 +23,7 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
github.com/ugorji/go/codec v1.2.7 // indirect
go.opentelemetry.io/otel/trace v1.11.1 // indirect
go.opentelemetry.io/otel/trace v1.14.0 // indirect
golang.org/x/crypto v0.2.0 // indirect
golang.org/x/net v0.2.0 // indirect
golang.org/x/sys v0.2.0 // indirect
Expand Down
24 changes: 10 additions & 14 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -59,38 +59,36 @@ github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUA
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/ugorji/go v1.2.7 h1:qYhyWUUd6WbiM+C6JZAUkIJt/1WrjzNHY9+KCIjVqTo=
github.com/ugorji/go v1.2.7/go.mod h1:nF9osbDWLy6bDVv/Rtoh6QgnvNDpmCalQV5urGCCS6M=
github.com/ugorji/go/codec v1.2.7 h1:YPXUKf7fYbp/y8xloBqZOw2qaVggbfwMlI8WM3wZUJ0=
github.com/ugorji/go/codec v1.2.7/go.mod h1:WGN1fab3R1fzQlVQTkfxVtIBhWDRqOviHU95kRgeqEY=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.opentelemetry.io/otel v1.11.1 h1:4WLLAmcfkmDk2ukNXJyq3/kiz/3UzCaYq6PskJsaou4=
go.opentelemetry.io/otel v1.11.1/go.mod h1:1nNhXBbWSD0nsL38H6btgnFN2k4i0sNLHNNMZMSbUGE=
go.opentelemetry.io/otel/metric v0.33.0 h1:xQAyl7uGEYvrLAiV/09iTJlp1pZnQ9Wl793qbVvED1E=
go.opentelemetry.io/otel/metric v0.33.0/go.mod h1:QlTYc+EnYNq/M2mNk1qDDMRLpqCOj2f/r5c7Fd5FYaI=
go.opentelemetry.io/otel/trace v1.11.1 h1:ofxdnzsNrGBYXbP7t7zpUK281+go5rF7dvdIZXF8gdQ=
go.opentelemetry.io/otel/trace v1.11.1/go.mod h1:f/Q9G7vzk5u91PhbmKbg1Qn0rzH1LJ4vbPHFGkTPtOk=
go.opentelemetry.io/otel v1.14.0 h1:/79Huy8wbf5DnIPhemGB+zEPVwnN6fuQybr/SRXa6hM=
go.opentelemetry.io/otel v1.14.0/go.mod h1:o4buv+dJzx8rohcUeRmWUZhqupFvzWis188WlggnNeU=
go.opentelemetry.io/otel/metric v0.37.0 h1:pHDQuLQOZwYD+Km0eb657A25NaRzy0a+eLyKfDXedEs=
go.opentelemetry.io/otel/metric v0.37.0/go.mod h1:DmdaHfGt54iV6UKxsV9slj2bBRJcKC1B1uvDLIioc1s=
go.opentelemetry.io/otel/trace v1.14.0 h1:wp2Mmvj41tDsyAJXiWDWpfNsOiIyd38fy85pyKcFq/M=
go.opentelemetry.io/otel/trace v1.14.0/go.mod h1:8avnQLK+CG77yNLUae4ea2JDQ6iT+gozhnZjy/rw9G8=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.1.0 h1:MDRAIl0xIo9Io2xV565hzXHw3zVseKrJKodhohM5CjU=
golang.org/x/crypto v0.1.0/go.mod h1:RecgLatLF4+eUMCP1PoPZQb+cVrJcOPbHkTkbkB9sbw=
golang.org/x/crypto v0.2.0 h1:BRXPfhNivWL5Yq0BGQ39a2sW6t44aODpfxkWjYdzewE=
golang.org/x/crypto v0.2.0/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.1.0 h1:hZ/3BUoy5aId7sCpA/Tc5lt8DkFgdVS2onTpJsZ/fl0=
golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco=
golang.org/x/net v0.2.0 h1:sZfSu1wtKLGlWI4ZZayP0ck9Y73K1ynO6gqzTdBVdPU=
golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand All @@ -104,13 +102,11 @@ golang.org/x/sys v0.0.0-20210806184541-e5e7981a1069/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.2.0 h1:ljd4t30dBnAvMZaQCevtY0xLLD0A+bRZXbgLMLU1F/A=
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
Expand Down
4 changes: 3 additions & 1 deletion otelginmetrics/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ type config struct {
func defaultConfig() *config {
return &config{
recordInFlight: true,
recordDuration: true,
recordSize: true,
recordDuration: true,
groupedStatus: true,
recorder: nil,
attributes: DefaultAttributes,
shouldRecord: func(_, _ string, _ *http.Request) bool {
return true
Expand All @@ -38,6 +39,7 @@ var DefaultAttributes = func(serverName, route string, request *http.Request) []
if serverName != "" {
attrs = append(attrs, semconv.HTTPServerNameKey.String(serverName))
}

if route != "" {
attrs = append(attrs, semconv.HTTPRouteKey.String(route))
}
Expand Down
33 changes: 16 additions & 17 deletions otelginmetrics/otelrecorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/global"
"go.opentelemetry.io/otel/metric/instrument"
"go.opentelemetry.io/otel/metric/instrument/syncint64"
"go.opentelemetry.io/otel/metric/unit"
)

const instrumentationName = "github.com/technologize/otel-go-contrib/otelginmetrics"
Expand All @@ -18,11 +16,11 @@ const instrumentationName = "github.com/technologize/otel-go-contrib/otelginmetr
// has the required methods to be used with the HTTP
// middlewares.
type otelRecorder struct {
attemptsCounter syncint64.UpDownCounter
totalDuration syncint64.Histogram
activeRequestsCounter syncint64.UpDownCounter
requestSize syncint64.Histogram
responseSize syncint64.Histogram
attemptsCounter instrument.Int64UpDownCounter
totalDuration instrument.Int64Histogram
activeRequestsCounter instrument.Int64UpDownCounter
requestSize instrument.Int64Histogram
responseSize instrument.Int64Histogram
}

func GetRecorder(metricsPrefix string) Recorder {
Expand All @@ -32,12 +30,13 @@ func GetRecorder(metricsPrefix string) Recorder {
}
return metricName
}

meter := global.MeterProvider().Meter(instrumentationName, metric.WithInstrumentationVersion(SemVersion()))
attemptsCounter, _ := meter.SyncInt64().UpDownCounter(metricName("http.server.request_count"), instrument.WithDescription("Number of Requests"), instrument.WithUnit(unit.Dimensionless))
totalDuration, _ := meter.SyncInt64().Histogram(metricName("http.server.duration"), instrument.WithDescription("Time Taken by request"), instrument.WithUnit(unit.Milliseconds))
activeRequestsCounter, _ := meter.SyncInt64().UpDownCounter(metricName("http.server.active_requests"), instrument.WithDescription("Number of requests inflight"), instrument.WithUnit(unit.Dimensionless))
requestSize, _ := meter.SyncInt64().Histogram(metricName("http.server.request_content_length"), instrument.WithDescription("Request Size"), instrument.WithUnit(unit.Bytes))
responseSize, _ := meter.SyncInt64().Histogram(metricName("http.server.response_content_length"), instrument.WithDescription("Response Size"), instrument.WithUnit(unit.Bytes))
attemptsCounter, _ := meter.Int64UpDownCounter(metricName("http.server.request_count"), instrument.WithDescription("Number of Requests"))
totalDuration, _ := meter.Int64Histogram(metricName("http.server.duration"), instrument.WithDescription("Time Taken by request"), instrument.WithUnit("ms"))
activeRequestsCounter, _ := meter.Int64UpDownCounter(metricName("http.server.active_requests"), instrument.WithDescription("Number of requests inflight"))
requestSize, _ := meter.Int64Histogram(metricName("http.server.request_content_length"), instrument.WithDescription("Request Size"), instrument.WithUnit("by"))
responseSize, _ := meter.Int64Histogram(metricName("http.server.response_content_length"), instrument.WithDescription("Response Size"), instrument.WithUnit("by"))
return &otelRecorder{
attemptsCounter: attemptsCounter,
totalDuration: totalDuration,
Expand All @@ -49,25 +48,25 @@ func GetRecorder(metricsPrefix string) Recorder {

// AddRequests increments the number of requests being processed.
func (r *otelRecorder) AddRequests(ctx context.Context, quantity int64, attributes []attribute.KeyValue) {
r.attemptsCounter.Add(ctx, quantity, attributes...)
r.attemptsCounter.Add(ctx, quantity, metric.WithAttributes(attributes...))
}

// ObserveHTTPRequestDuration measures the duration of an HTTP request.
func (r *otelRecorder) ObserveHTTPRequestDuration(ctx context.Context, duration time.Duration, attributes []attribute.KeyValue) {
r.totalDuration.Record(ctx, int64(duration/time.Millisecond), attributes...)
r.totalDuration.Record(ctx, int64(duration/time.Millisecond), metric.WithAttributes(attributes...))
}

// ObserveHTTPRequestSize measures the size of an HTTP request in bytes.
func (r *otelRecorder) ObserveHTTPRequestSize(ctx context.Context, sizeBytes int64, attributes []attribute.KeyValue) {
r.requestSize.Record(ctx, sizeBytes, attributes...)
r.requestSize.Record(ctx, sizeBytes, metric.WithAttributes(attributes...))
}

// ObserveHTTPResponseSize measures the size of an HTTP response in bytes.
func (r *otelRecorder) ObserveHTTPResponseSize(ctx context.Context, sizeBytes int64, attributes []attribute.KeyValue) {
r.responseSize.Record(ctx, sizeBytes, attributes...)
r.responseSize.Record(ctx, sizeBytes, metric.WithAttributes(attributes...))
}

// AddInflightRequests increments and decrements the number of inflight request being processed.
func (r *otelRecorder) AddInflightRequests(ctx context.Context, quantity int64, attributes []attribute.KeyValue) {
r.activeRequestsCounter.Add(ctx, quantity, attributes...)
r.activeRequestsCounter.Add(ctx, quantity, metric.WithAttributes(attributes...))
}
22 changes: 10 additions & 12 deletions otelhttpmetrics/otelrecorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/metric/global"
"go.opentelemetry.io/otel/metric/instrument"
"go.opentelemetry.io/otel/metric/instrument/syncint64"
"go.opentelemetry.io/otel/metric/unit"
)

const instrumentationName = "github.com/technologize/otel-go-contrib/otelhttpmetrics"
Expand All @@ -18,11 +16,11 @@ const instrumentationName = "github.com/technologize/otel-go-contrib/otelhttpmet
// has the required methods to be used with the HTTP
// middlewares.
type otelRecorder struct {
attemptsCounter syncint64.UpDownCounter
totalDuration syncint64.Histogram
activeRequestsCounter syncint64.UpDownCounter
requestSize syncint64.Histogram
responseSize syncint64.Histogram
attemptsCounter instrument.Int64UpDownCounter
totalDuration instrument.Int64Histogram
activeRequestsCounter instrument.Int64UpDownCounter
requestSize instrument.Int64Histogram
responseSize instrument.Int64Histogram
}

func GetRecorder(metricsPrefix string) Recorder {
Expand All @@ -33,11 +31,11 @@ func GetRecorder(metricsPrefix string) Recorder {
return metricName
}
meter := global.MeterProvider().Meter(instrumentationName, metric.WithInstrumentationVersion(SemVersion()))
attemptsCounter, _ := meter.SyncInt64().UpDownCounter(metricName("http.client.request_count"), instrument.WithDescription("Number of Requests"), instrument.WithUnit(unit.Dimensionless))
totalDuration, _ := meter.SyncInt64().Histogram(metricName("http.client.duration"), instrument.WithDescription("Time Taken by request"), instrument.WithUnit(unit.Milliseconds))
activeRequestsCounter, _ := meter.SyncInt64().UpDownCounter(metricName("http.client.active_requests"), instrument.WithDescription("Number of requests inflight"), instrument.WithUnit(unit.Dimensionless))
requestSize, _ := meter.SyncInt64().Histogram(metricName("http.client.request_content_length"), instrument.WithDescription("Request Size"), instrument.WithUnit(unit.Bytes))
responseSize, _ := meter.SyncInt64().Histogram(metricName("http.client.response_content_length"), instrument.WithDescription("Response Size"), instrument.WithUnit(unit.Bytes))
attemptsCounter, _ := meter.Int64UpDownCounter(metricName("http.client.request_count"), instrument.WithDescription("Number of Requests"))
totalDuration, _ := meter.Int64Histogram(metricName("http.client.duration"), instrument.WithDescription("Time Taken by request"), instrument.WithUnit("ms"))
activeRequestsCounter, _ := meter.Int64UpDownCounter(metricName("http.client.active_requests"), instrument.WithDescription("Number of requests inflight"))
requestSize, _ := meter.Int64Histogram(metricName("http.client.request_content_length"), instrument.WithDescription("Request Size"), instrument.WithUnit("by"))
responseSize, _ := meter.Int64Histogram(metricName("http.client.response_content_length"), instrument.WithDescription("Response Size"), instrument.WithUnit("by"))
return &otelRecorder{
attemptsCounter: attemptsCounter,
totalDuration: totalDuration,
Expand Down