-
Notifications
You must be signed in to change notification settings - Fork 436
/
option.go
96 lines (79 loc) · 2.53 KB
/
option.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// 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 fasthttp
import (
"github.com/valyala/fasthttp"
"github.com/DataDog/dd-trace-go/v2/ddtrace/tracer"
"github.com/DataDog/dd-trace-go/v2/internal/namingschema"
)
const defaultServiceName = "fasthttp"
type config struct {
serviceName string
spanName string
spanOpts []tracer.StartSpanOption
isStatusError func(int) bool
resourceNamer func(*fasthttp.RequestCtx) string
ignoreRequest func(*fasthttp.RequestCtx) bool
}
// Option describes options for the FastHTTP integration.
type Option interface {
apply(*config)
}
// OptionFn represents options applicable to WrapHandler.
type OptionFn func(*config)
func (fn OptionFn) apply(cfg *config) {
fn(cfg)
}
func newConfig() *config {
return &config{
serviceName: namingschema.ServiceName(defaultServiceName),
spanName: namingschema.OpName(namingschema.HTTPServer),
isStatusError: defaultIsServerError,
resourceNamer: defaultResourceNamer,
ignoreRequest: defaultIgnoreRequest,
}
}
// WithService sets the given service name for the router.
func WithService(name string) OptionFn {
return func(cfg *config) {
cfg.serviceName = name
}
}
// WithSpanOptions applies the given set of options to the spans started
// by the router.
func WithSpanOptions(opts ...tracer.StartSpanOption) OptionFn {
return func(cfg *config) {
cfg.spanOpts = opts
}
}
// WithStatusCheck allows customization over which status code(s) to consider "error"
func WithStatusCheck(fn func(statusCode int) bool) OptionFn {
return func(cfg *config) {
cfg.isStatusError = fn
}
}
// WithResourceNamer specifies a function which will be used to
// obtain the resource name for a given request
func WithResourceNamer(fn func(fctx *fasthttp.RequestCtx) string) OptionFn {
return func(cfg *config) {
cfg.resourceNamer = fn
}
}
// WithIgnoreRequest specifies a function to use for determining if the
// incoming HTTP request tracing should be skipped.
func WithIgnoreRequest(f func(fctx *fasthttp.RequestCtx) bool) OptionFn {
return func(cfg *config) {
cfg.ignoreRequest = f
}
}
func defaultIsServerError(statusCode int) bool {
return statusCode >= 500 && statusCode < 600
}
func defaultResourceNamer(fctx *fasthttp.RequestCtx) string {
return string(fctx.Method()) + " " + string(fctx.Path())
}
func defaultIgnoreRequest(_ *fasthttp.RequestCtx) bool {
return false
}