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

Added external attributes option #13

Merged
merged 1 commit into from
Mar 21, 2022
Merged
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
20 changes: 19 additions & 1 deletion otelginmetrics/config.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package otelginmetrics

import (
"net/http"

"go.opentelemetry.io/otel/attribute"
semconv "go.opentelemetry.io/otel/semconv/v1.7.0"
)

type config struct {
recordInFlight bool
recordSize bool
recordDuration bool
groupedStatus bool
attributes []attribute.KeyValue
recorder Recorder
attributes func(serverName, route string, request *http.Request) []attribute.KeyValue
}

func defaultConfig() *config {
Expand All @@ -19,5 +22,20 @@ func defaultConfig() *config {
recordDuration: true,
recordSize: true,
groupedStatus: true,
attributes: DefaultAttributes,
}
}

var DefaultAttributes = func(serverName, route string, request *http.Request) []attribute.KeyValue {
attrs := []attribute.KeyValue{
semconv.HTTPMethodKey.String(request.Method),
}

if serverName != "" {
attrs = append(attrs, semconv.HTTPServerNameKey.String(serverName))
}
if route != "" {
attrs = append(attrs, semconv.HTTPRouteKey.String(route))
}
return attrs
}
5 changes: 4 additions & 1 deletion otelginmetrics/example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,10 @@ func main() {
initMetrics()
router.Use(otelginmetrics.Middleware(
"TEST-SERVICE",
otelginmetrics.WithAdditionalAttributes(map[string]string{"label": "value"}),
// Custom attributes
otelginmetrics.WithAttributes(func(serverName, route string, request *http.Request) []attribute.KeyValue {
return append(otelginmetrics.DefaultAttributes(serverName, route, request), attribute.String("Custom-attribute", "value"))
}),
))

logic := func(ctx *gin.Context, sleep int) {
Expand Down
24 changes: 1 addition & 23 deletions otelginmetrics/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"time"

"github.com/gin-gonic/gin"
"go.opentelemetry.io/otel/attribute"
semconv "go.opentelemetry.io/otel/semconv/v1.7.0"
)

Expand All @@ -32,7 +31,7 @@ func Middleware(service string, options ...Option) gin.HandlerFunc {

start := time.Now()

reqAttributes := append(cfg.attributes, getRequestAttributes(service, route, ginCtx.Request)...)
reqAttributes := cfg.attributes(service, route, ginCtx.Request)

if cfg.recordInFlight {
recorder.AddInflightRequests(ctx, 1, reqAttributes)
Expand Down Expand Up @@ -67,27 +66,6 @@ func Middleware(service string, options ...Option) gin.HandlerFunc {
}
}

func getRequestAttributes(serverName, route string, request *http.Request) []attribute.KeyValue {
attrs := []attribute.KeyValue{
semconv.HTTPMethodKey.String(request.Method),
semconv.HTTPTargetKey.String(request.RequestURI),
}

if serverName != "" {
attrs = append(attrs, semconv.HTTPServerNameKey.String(serverName))
}
if route != "" {
attrs = append(attrs, semconv.HTTPRouteKey.String(route))
}
if ua := request.UserAgent(); ua != "" {
attrs = append(attrs, semconv.HTTPUserAgentKey.String(ua))
}
if request.Host != "" {
attrs = append(attrs, semconv.HTTPHostKey.String(request.Host))
}
return attrs
}

func computeApproximateRequestSize(r *http.Request) int64 {
s := 0
if r.URL != nil {
Expand Down
13 changes: 6 additions & 7 deletions otelginmetrics/option.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package otelginmetrics

import (
"net/http"

"go.opentelemetry.io/otel/attribute"
)

Expand All @@ -15,14 +17,11 @@ func (fn optionFunc) apply(cfg *config) {
fn(cfg)
}

// WithAdditionalAttributes sets a list of attribute.KeyValue labels for all metrics associated with this round tripper
func WithAdditionalAttributes(attributes map[string]string) Option {
// WithAttributes sets a func using which what attributes to be recorded can be specified.
// By default the DefaultAttributes is used
func WithAttributes(attributes func(serverName, route string, request *http.Request) []attribute.KeyValue) Option {
return optionFunc(func(cfg *config) {
attr := make([]attribute.KeyValue, 0, len(attributes))
for k, v := range attributes {
attr = append(attr, attribute.String(k, v))
}
cfg.attributes = attr
cfg.attributes = attributes
})
}

Expand Down