Skip to content

Commit

Permalink
Merge pull request #13 from technologize/external-attributes
Browse files Browse the repository at this point in the history
Added external attributes option
  • Loading branch information
bharat-gadde-dev authored Mar 21, 2022
2 parents 30ef2f8 + 5a6e7a8 commit 90e0407
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 32 deletions.
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

0 comments on commit 90e0407

Please sign in to comment.