• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pace / bricks / 11250208184

09 Oct 2024 07:25AM UTC coverage: 57.466% (-13.7%) from 71.177%
11250208184

push

github

web-flow
Merge pull request #380 from pace/sentry-tracing-poc

tracing: replace Jaeger with Sentry

140 of 206 new or added lines in 19 files covered. (67.96%)

3 existing lines in 3 files now uncovered.

5515 of 9597 relevant lines covered (57.47%)

21.77 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

61.19
/maintenance/tracing/tracing.go
1
// Copyright © 2018 by PACE Telematics GmbH. All rights reserved.
2

3
package tracing
4

5
import (
6
        "fmt"
7
        "net/http"
8
        "os"
9

10
        "github.com/getsentry/sentry-go"
11
        "github.com/pace/bricks/maintenance/log"
12
        "github.com/pace/bricks/maintenance/util"
13
        "github.com/zenazn/goji/web/mutil"
14
)
15

16
func init() {
1✔
17
        err := sentry.Init(sentry.ClientOptions{
1✔
18
                Dsn:              os.Getenv("SENTRY_DSN"),
1✔
19
                Environment:      os.Getenv("ENVIRONMENT"),
1✔
20
                EnableTracing:    true,
1✔
21
                TracesSampleRate: 1.0,
1✔
22
        })
1✔
23
        if err != nil {
1✔
NEW
24
                log.Fatalf("sentry.Init: %v", err)
×
25
        }
×
26
}
27

28
type traceHandler struct {
29
        next http.Handler
30
}
31

32
// Trace the service function handler execution
33
func (h *traceHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
1✔
34
        ctx := r.Context()
1✔
35

1✔
36
        hub := sentry.GetHubFromContext(ctx)
1✔
37
        if hub == nil {
2✔
38
                // Check the concurrency guide for more details: https://docs.sentry.io/platforms/go/concurrency/
1✔
39
                hub = sentry.CurrentHub().Clone()
1✔
40
                ctx = sentry.SetHubOnContext(ctx, hub)
1✔
41
        }
1✔
42

43
        options := []sentry.SpanOption{
1✔
44
                // Set the OP based on values from https://develop.sentry.dev/sdk/performance/span-operations/
1✔
45
                sentry.WithOpName("http.server"),
1✔
46
                sentry.ContinueFromRequest(r),
1✔
47
                sentry.WithTransactionSource(sentry.SourceURL),
1✔
48
        }
1✔
49

1✔
50
        span := sentry.StartTransaction(ctx,
1✔
51
                fmt.Sprintf("%s %s", r.Method, r.URL.Path),
1✔
52
                options...,
1✔
53
        )
1✔
54

1✔
55
        defer span.Finish()
1✔
56

1✔
57
        ctx = span.Context()
1✔
58
        ww := mutil.WrapWriter(w)
1✔
59

1✔
60
        h.next.ServeHTTP(ww, r.WithContext(ctx))
1✔
61
}
62

63
// Handler generates a tracing handler that decodes the current trace from the wire.
64
// The tracing handler will not start traces for the list of ignoredPrefixes.
65
func Handler(ignoredPrefixes ...string) func(http.Handler) http.Handler {
2✔
66
        return util.NewIgnorePrefixMiddleware(func(next http.Handler) http.Handler {
3✔
67
                return &traceHandler{
1✔
68
                        next: next,
1✔
69
                }
1✔
70
        }, ignoredPrefixes...)
1✔
71
}
72

73
type traceLogHandler struct {
74
        next http.Handler
75
}
76

77
// Trace the service function handler execution
78
func (h *traceLogHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
×
79
        ctx := r.Context()
×
80

×
NEW
81
        span := sentry.TransactionFromContext(ctx)
×
NEW
82
        defer span.Finish()
×
NEW
83

×
NEW
84
        r = r.WithContext(span.Context())
×
NEW
85

×
NEW
86
        span.SetData("req_id", log.RequestIDFromContext(ctx))
×
NEW
87
        span.SetData("path", r.URL.Path)
×
NEW
88
        span.SetData("method", r.Method)
×
NEW
89

×
90
        ww := mutil.WrapWriter(w)
×
NEW
91

×
NEW
92
        h.next.ServeHTTP(ww, r)
×
NEW
93
        span.SetData("bytes", ww.BytesWritten())
×
NEW
94
        span.SetData("status_code", ww.Status())
×
UNCOV
95
}
×
96

97
// TraceLogHandler generates a tracing handler that adds logging data to existing handler.
98
// The tracing handler will not start traces for the list of ignoredPrefixes.
99
func TraceLogHandler(ignoredPrefixes ...string) func(http.Handler) http.Handler {
×
100
        return util.NewIgnorePrefixMiddleware(func(next http.Handler) http.Handler {
×
101
                return &traceLogHandler{
×
102
                        next: next,
×
103
                }
×
104
        }, ignoredPrefixes...)
×
105
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc