/
goji.go
59 lines (54 loc) · 1.96 KB
/
goji.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016 Datadog, Inc.
// Package web provides functions to trace the zenazn/goji/web package (https://github.com/zenazn/goji).
package web // import "gopkg.in/DataDog/dd-trace-go.v1/contrib/zenazn/goji.v1/web"
import (
"fmt"
"math"
"net/http"
"sync"
httptrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/net/http"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/ext"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
"gopkg.in/DataDog/dd-trace-go.v1/internal/log"
"github.com/zenazn/goji/web"
)
// Middleware returns a goji middleware function that will trace incoming requests.
// If goji's Router middleware is also installed, the tracer will be able to determine
// the original route name (e.g. "/user/:id"), and include it as part of the traces' resource
// names.
func Middleware(opts ...Option) func(*web.C, http.Handler) http.Handler {
var (
cfg config
warnonce sync.Once
)
defaults(&cfg)
for _, fn := range opts {
fn(&cfg)
}
if !math.IsNaN(cfg.analyticsRate) {
cfg.spanOpts = append(cfg.spanOpts, tracer.Tag(ext.EventSampleRate, cfg.analyticsRate))
}
log.Debug("contrib/zenazn/goji.v1/web: Configuring Middleware: %#v", cfg)
return func(c *web.C, h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
resource := r.Method
p := web.GetMatch(*c).RawPattern()
if p != nil {
resource += fmt.Sprintf(" %s", p)
} else {
warnonce.Do(func() {
log.Warn("contrib/zenazn/goji.v1/web: routes are unavailable. To enable them add the goji Router middleware before the tracer middleware.")
})
}
httptrace.TraceAndServe(h, w, r, &httptrace.ServeConfig{
Service: cfg.serviceName,
Resource: resource,
FinishOpts: cfg.finishOpts,
SpanOpts: cfg.spanOpts,
})
})
}
}