/
echotrace.go
136 lines (121 loc) · 4.34 KB
/
echotrace.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// 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 echo provides functions to trace the labstack/echo package (https://github.com/labstack/echo).
package echo
import (
"fmt"
"math"
"net/http"
"strconv"
"gopkg.in/DataDog/dd-trace-go.v1/contrib/internal/httptrace"
"gopkg.in/DataDog/dd-trace-go.v1/contrib/internal/options"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace"
"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/appsec"
"gopkg.in/DataDog/dd-trace-go.v1/internal/log"
"gopkg.in/DataDog/dd-trace-go.v1/internal/telemetry"
"github.com/labstack/echo/v4"
)
const componentName = "labstack/echo.v4"
func init() {
telemetry.LoadIntegration(componentName)
tracer.MarkIntegrationImported("github.com/labstack/echo/v4")
}
// Middleware returns echo middleware which will trace incoming requests.
func Middleware(opts ...Option) echo.MiddlewareFunc {
cfg := new(config)
defaults(cfg)
for _, fn := range opts {
fn(cfg)
}
log.Debug("contrib/labstack/echo.v4: Configuring Middleware: %#v", cfg)
spanOpts := make([]ddtrace.StartSpanOption, 0, 3+len(cfg.tags))
spanOpts = append(spanOpts, tracer.ServiceName(cfg.serviceName))
for k, v := range cfg.tags {
spanOpts = append(spanOpts, tracer.Tag(k, v))
}
spanOpts = append(spanOpts,
tracer.Tag(ext.Component, componentName),
tracer.Tag(ext.SpanKind, ext.SpanKindServer),
)
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
// If we have an ignoreRequestFunc, use it to see if we proceed with tracing
if cfg.ignoreRequestFunc != nil && cfg.ignoreRequestFunc(c) {
if err := next(c); err != nil {
c.Error(err)
return err
}
return nil
}
request := c.Request()
route := c.Path()
resource := request.Method + " " + route
opts := options.Copy(spanOpts...) // opts must be a copy of spanOpts, locally scoped, to avoid races.
if !math.IsNaN(cfg.analyticsRate) {
opts = append(opts, tracer.Tag(ext.EventSampleRate, cfg.analyticsRate))
}
opts = append(opts,
tracer.ResourceName(resource),
tracer.Tag(ext.HTTPRoute, route),
httptrace.HeaderTagsFromRequest(request, cfg.headerTags))
var finishOpts []tracer.FinishOption
if cfg.noDebugStack {
finishOpts = []tracer.FinishOption{tracer.NoDebugStack()}
}
span, ctx := httptrace.StartRequestSpan(request, opts...)
defer func() {
span.Finish(finishOpts...)
}()
// pass the span through the request context
c.SetRequest(request.WithContext(ctx))
if appsec.Enabled() {
next = withAppSec(next, span)
}
// serve the request to the next middleware
err := next(c)
if err != nil && !shouldIgnoreError(cfg, err) {
// invokes the registered HTTP error handler
c.Error(err)
// It is impossible to determine what the final status code of a request is in echo.
// This is the best we can do.
if echoErr, ok := cfg.translateError(err); ok {
if cfg.isStatusError(echoErr.Code) {
finishOpts = append(finishOpts, tracer.WithError(err))
}
span.SetTag(ext.HTTPCode, strconv.Itoa(echoErr.Code))
} else {
// Any error that is not an *echo.HTTPError will be treated as an error with 500 status code.
if cfg.isStatusError(500) {
finishOpts = append(finishOpts, tracer.WithError(err))
}
span.SetTag(ext.HTTPCode, "500")
}
} else if status := c.Response().Status; status > 0 {
if cfg.isStatusError(status) {
if statusErr := errorFromStatusCode(status); !shouldIgnoreError(cfg, statusErr) {
finishOpts = append(finishOpts, tracer.WithError(statusErr))
}
}
span.SetTag(ext.HTTPCode, strconv.Itoa(status))
} else {
if cfg.isStatusError(200) {
if statusErr := errorFromStatusCode(200); !shouldIgnoreError(cfg, statusErr) {
finishOpts = append(finishOpts, tracer.WithError(statusErr))
}
}
span.SetTag(ext.HTTPCode, "200")
}
return err
}
}
}
func errorFromStatusCode(statusCode int) error {
return fmt.Errorf("%d: %s", statusCode, http.StatusText(statusCode))
}
func shouldIgnoreError(cfg *config, err error) bool {
return cfg.errCheck != nil && !cfg.errCheck(err)
}