-
Notifications
You must be signed in to change notification settings - Fork 2
/
datadog.go
67 lines (56 loc) · 1.79 KB
/
datadog.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
60
61
62
63
64
65
66
67
package datadog
import (
"context"
c "github.com/go-chi/chi/v5"
"net/http"
datadog "github.com/americanas-go/ignite/datadog/dd-trace-go.v1"
"github.com/americanas-go/ignite/go-chi/chi.v5"
"github.com/americanas-go/log"
chitrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/go-chi/chi.v5"
)
// Datadog struct that represents a datadog plugin for chi
type Datadog struct {
options *Options
}
// NewDatadogWithConfigPath returns a new datadog plugin with options from config path.
func NewDatadogWithConfigPath(path string, traceOptions ...chitrace.Option) (*Datadog, error) {
o, err := NewOptionsWithPath(path, traceOptions...)
if err != nil {
return nil, err
}
return NewDatadogWithOptions(o), nil
}
// NewDatadogWithOptions returns a new datadog plugin with options.
func NewDatadogWithOptions(options *Options) *Datadog {
return &Datadog{options: options}
}
// NewDatadog returns a new datadog plugin with default options.
func NewDatadog(traceOptions ...chitrace.Option) *Datadog {
o, err := NewOptions(traceOptions...)
if err != nil {
log.Fatalf(err.Error())
}
return NewDatadogWithOptions(o)
}
// Register registers the datadog plugin to a new chi config.
func (d *Datadog) Register(ctx context.Context, mux *c.Mux) (*chi.Config, error) {
if !d.options.Enabled || !datadog.IsTracerEnabled() {
return nil, nil
}
logger := log.FromContext(ctx)
logger.Trace("enabling datadog middleware in chi")
return &chi.Config{
Middlewares: []func(http.Handler) http.Handler{
chitrace.Middleware(d.options.TraceOptions...),
},
}, nil
}
// Register registers a default datadog plugin to a new chi config.
func Register(ctx context.Context, mux *c.Mux) (*chi.Config, error) {
o, err := NewOptions()
if err != nil {
return nil, err
}
d := NewDatadogWithOptions(o)
return d.Register(ctx, mux)
}