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

pace / bricks / 12058212013

27 Nov 2024 08:45PM UTC coverage: 57.508% (+0.04%) from 57.466%
12058212013

Pull #387

github

monstermunchkin
pkg/context: Add deprecation notice
Pull Request #387: tracing: Fix bugs and clean up code

19 of 29 new or added lines in 7 files covered. (65.52%)

2 existing lines in 1 file now uncovered.

5519 of 9597 relevant lines covered (57.51%)

21.8 hits per line

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

63.08
/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✔
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) {
×
NEW
79
        span := sentry.TransactionFromContext(r.Context())
×
80
        defer span.Finish()
×
81

×
NEW
82
        ctx := span.Context()
×
83

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

×
88
        ww := mutil.WrapWriter(w)
×
89

×
NEW
90
        h.next.ServeHTTP(ww, r.WithContext(ctx))
×
91
        span.SetData("bytes", ww.BytesWritten())
×
92
        span.SetData("status_code", ww.Status())
×
93
}
×
94

95
// TraceLogHandler generates a tracing handler that adds logging data to existing handler.
96
// The tracing handler will not start traces for the list of ignoredPrefixes.
UNCOV
97
func TraceLogHandler(ignoredPrefixes ...string) func(http.Handler) http.Handler {
×
UNCOV
98
        return util.NewIgnorePrefixMiddleware(func(next http.Handler) http.Handler {
×
99
                return &traceLogHandler{
×
100
                        next: next,
×
101
                }
×
102
        }, ignoredPrefixes...)
×
103
}
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