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

pace / bricks / 13717956986

06 Mar 2025 02:30PM UTC coverage: 51.823% (-4.8%) from 56.612%
13717956986

push

github

web-flow
Merge pull request #406 from pace/gomod-update

This updates all dependencies to the latest version, excluding

github.com/bsm/redislock
github.com/dave/jennifer

as newer versions lead to unwanted behavior.

54 of 82 new or added lines in 9 files covered. (65.85%)

453 existing lines in 15 files now uncovered.

4889 of 9434 relevant lines covered (51.82%)

20.93 hits per line

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

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

3
package tracing
4

5
import (
6
        "net/http"
7
        "os"
8
        "strconv"
9
        "strings"
10

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

17
func init() {
1✔
18
        var (
1✔
19
                tracesSampleRate = 0.1
1✔
20
                enableTracing    = true
1✔
21
        )
1✔
22

1✔
23
        val := strings.TrimSpace(os.Getenv("SENTRY_TRACES_SAMPLE_RATE"))
1✔
24
        if val != "" {
1✔
25
                var err error
×
26

×
27
                tracesSampleRate, err = strconv.ParseFloat(val, 64)
×
28
                if err != nil {
×
29
                        log.Fatalf("failed to parse SENTRY_TRACES_SAMPLE_RATE: %v", err)
×
30
                }
×
31
        }
32

33
        valEnableTracing := strings.TrimSpace(os.Getenv("SENTRY_ENABLE_TRACING"))
1✔
34
        if valEnableTracing != "" {
1✔
35
                var err error
×
36

×
37
                enableTracing, err = strconv.ParseBool(valEnableTracing)
×
38
                if err != nil {
×
39
                        log.Fatalf("failed to parse SENTRY_ENABLE_TRACING: %v", err)
×
40
                }
×
41
        }
42

43
        err := sentry.Init(sentry.ClientOptions{
1✔
44
                Dsn:              os.Getenv("SENTRY_DSN"),
1✔
45
                Environment:      os.Getenv("ENVIRONMENT"),
1✔
46
                EnableTracing:    enableTracing,
1✔
47
                TracesSampleRate: tracesSampleRate,
1✔
48
                BeforeSendTransaction: func(event *sentry.Event, hint *sentry.EventHint) *sentry.Event {
1✔
UNCOV
49
                        // Drop request body.
×
UNCOV
50
                        if event.Request != nil {
×
UNCOV
51
                                event.Request.Data = ""
×
UNCOV
52
                        }
×
53

UNCOV
54
                        return event
×
55
                },
56
        })
57
        if err != nil {
1✔
58
                log.Fatalf("sentry.Init: %v", err)
×
59
        }
×
60
}
61

62
type traceHandler struct {
63
        next http.Handler
64
}
65

66
// Trace the service function handler execution
67
func (h *traceHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
1✔
68
        ctx := r.Context()
1✔
69
        hub := sentry.CurrentHub()
1✔
70

1✔
71
        options := []sentry.SpanOption{
1✔
72
                sentry.ContinueTrace(hub, r.Header.Get(sentry.SentryTraceHeader), r.Header.Get(sentry.SentryBaggageHeader)),
1✔
73
                sentry.WithOpName("http.server"),
1✔
74
                sentry.WithTransactionSource(sentry.SourceURL),
1✔
75
                sentry.WithSpanOrigin(sentry.SpanOriginStdLib),
1✔
76
        }
1✔
77

1✔
78
        transaction := sentry.StartTransaction(ctx,
1✔
79
                getHTTPSpanName(r),
1✔
80
                options...,
1✔
81
        )
1✔
82
        transaction.SetData("http.request.method", r.Method)
1✔
83

1✔
84
        ww := mutil.WrapWriter(w)
1✔
85

1✔
86
        defer func() {
2✔
87
                status := ww.Status()
1✔
88
                bytesWritten := ww.BytesWritten()
1✔
89
                transaction.Status = sentry.HTTPtoSpanStatus(status)
1✔
90

1✔
91
                transaction.SetData("http.response.status_code", status)
1✔
92
                transaction.SetData("http.response.content_length", bytesWritten)
1✔
93
                transaction.SetData("http.request.url", r.URL.String())
1✔
94

1✔
95
                transaction.Finish()
1✔
96
        }()
1✔
97

98
        hub.Scope().SetRequest(r)
1✔
99
        r = r.WithContext(transaction.Context())
1✔
100

1✔
101
        h.next.ServeHTTP(ww, r)
1✔
102
}
103

104
// Handler generates a tracing handler that decodes the current trace from the wire.
105
// The tracing handler will not start traces for the list of ignoredPrefixes.
106
func Handler(ignoredPrefixes ...string) func(http.Handler) http.Handler {
2✔
107
        return util.NewIgnorePrefixMiddleware(func(next http.Handler) http.Handler {
3✔
108
                return &traceHandler{
1✔
109
                        next: next,
1✔
110
                }
1✔
111
        }, ignoredPrefixes...)
1✔
112
}
113

114
// getHTTPSpanName grab needed fields from *http.Request to generate a span name for `http.server` span op.
115
func getHTTPSpanName(r *http.Request) string {
1✔
116
        if r.Pattern != "" {
1✔
117
                // If value does not start with HTTP methods, add them.
×
118
                // The method and the path should be separated by a space.
×
119
                if parts := strings.SplitN(r.Pattern, " ", 2); len(parts) == 1 {
×
120
                        return r.Method + " " + r.Pattern
×
121
                }
×
122

123
                return r.Pattern
×
124
        }
125

126
        return r.Method + " " + r.URL.Path
1✔
127
}
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