-
Notifications
You must be signed in to change notification settings - Fork 251
Expand file tree
/
Copy pathutil.go
More file actions
172 lines (156 loc) · 4.89 KB
/
util.go
File metadata and controls
172 lines (156 loc) · 4.89 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package sentry
import (
"encoding/json"
"fmt"
"os"
"runtime/debug"
"strings"
"time"
"github.com/getsentry/sentry-go/internal/debuglog"
"github.com/getsentry/sentry-go/internal/protocol"
exec "golang.org/x/sys/execabs"
)
func uuid() string {
return protocol.GenerateEventID()
}
func fileExists(fileName string) bool {
_, err := os.Stat(fileName)
return err == nil
}
// monotonicTimeSince replaces uses of time.Now() to take into account the
// monotonic clock reading stored in start, such that duration = end - start is
// unaffected by changes in the system wall clock.
func monotonicTimeSince(start time.Time) (end time.Time) {
return start.Add(time.Since(start))
}
// nolint: unused
func prettyPrint(data interface{}) {
dbg, _ := json.MarshalIndent(data, "", " ")
fmt.Println(string(dbg))
}
// defaultRelease attempts to guess a default release for the currently running
// program.
func defaultRelease() (release string) {
// Return first non-empty environment variable known to hold release info, if any.
envs := []string{
"SENTRY_RELEASE",
"HEROKU_BUILD_COMMIT",
"HEROKU_SLUG_COMMIT", // Deprecated, kept for backwards compatibility
"SOURCE_VERSION",
"CODEBUILD_RESOLVED_SOURCE_VERSION",
"CIRCLE_SHA1",
"GAE_DEPLOYMENT_ID",
"GITHUB_SHA", // GitHub Actions - https://help.github.com/en/actions
"COMMIT_REF", // Netlify - https://docs.netlify.com/
"VERCEL_GIT_COMMIT_SHA", // Vercel - https://vercel.com/
"ZEIT_GITHUB_COMMIT_SHA", // Zeit (now known as Vercel)
"ZEIT_GITLAB_COMMIT_SHA",
"ZEIT_BITBUCKET_COMMIT_SHA",
}
for _, e := range envs {
if release = os.Getenv(e); release != "" {
debuglog.Printf("Using release from environment variable %s: %s", e, release)
return release
}
}
if info, ok := debug.ReadBuildInfo(); ok {
buildInfoVcsRevision := revisionFromBuildInfo(info)
if len(buildInfoVcsRevision) > 0 {
return buildInfoVcsRevision
}
}
// Derive a version string from Git. Example outputs:
// v1.0.1-0-g9de4
// v2.0-8-g77df-dirty
// 4f72d7
if _, err := exec.LookPath("git"); err == nil {
cmd := exec.Command("git", "describe", "--long", "--always", "--dirty")
b, err := cmd.Output()
if err != nil {
// Either Git is not available or the current directory is not a
// Git repository.
var s strings.Builder
fmt.Fprintf(&s, "Release detection failed: %v", err)
if err, ok := err.(*exec.ExitError); ok && len(err.Stderr) > 0 {
fmt.Fprintf(&s, ": %s", err.Stderr)
}
debuglog.Print(s.String())
} else {
release = strings.TrimSpace(string(b))
debuglog.Printf("Using release from Git: %s", release)
return release
}
}
debuglog.Print("Some Sentry features will not be available. See https://docs.sentry.io/product/releases/.")
debuglog.Print("To stop seeing this message, pass a Release to sentry.Init or set the SENTRY_RELEASE environment variable.")
return ""
}
func revisionFromBuildInfo(info *debug.BuildInfo) string {
for _, setting := range info.Settings {
if setting.Key == "vcs.revision" && setting.Value != "" {
debuglog.Printf("Using release from debug info: %s", setting.Value)
return setting.Value
}
}
return ""
}
func Pointer[T any](v T) *T {
return &v
}
var sensitiveHeaders = map[string]struct{}{
"_csrf": {},
"_csrf_token": {},
"_session": {},
"_xsrf": {},
"api-key": {},
"apikey": {},
"auth": {},
"authorization": {},
"cookie": {},
"credentials": {},
"csrf": {},
"csrf-token": {},
"csrftoken": {},
"ip-address": {},
"passwd": {},
"password": {},
"private-key": {},
"privatekey": {},
"proxy-authorization": {},
"remote-addr": {},
"secret": {},
"session": {},
"sessionid": {},
"token": {},
"user-session": {},
"x-api-key": {},
"x-csrftoken": {},
"x-forwarded-for": {},
"x-real-ip": {},
"xsrf-token": {},
}
// IsSensitiveHeader reports whether a header or metadata key should be treated as sensitive.
func IsSensitiveHeader(key string) bool {
_, ok := sensitiveHeaders[strings.ToLower(key)]
return ok
}
// eventIdentifier returns a human-readable identifier for the event to be used in log messages.
// Format: "<description> [<event-id>]".
func eventIdentifier(event *Event) string {
var description string
switch event.Type {
case errorType:
description = "error"
case transactionType:
description = "transaction"
case checkInType:
description = "check-in"
case logEvent.Type:
description = fmt.Sprintf("%d log events", len(event.Logs))
case traceMetricEvent.Type:
description = fmt.Sprintf("%d metric events", len(event.Metrics))
default:
description = fmt.Sprintf("%s event", event.Type)
}
return fmt.Sprintf("%s [%s]", description, event.EventID)
}