-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutils.go
More file actions
203 lines (186 loc) · 5.38 KB
/
Copy pathutils.go
File metadata and controls
203 lines (186 loc) · 5.38 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"math/rand"
"net/http"
"os"
"os/user"
"strings"
"time"
"github.com/getsentry/sentry-go"
"github.com/google/uuid"
"gopkg.in/yaml.v2"
)
func createUser() string {
rand.Seed(time.Now().UnixNano())
alpha := strings.Split("abcdefghijklmnopqrstuvwxyz", "")[rand.Intn(9)]
var alphanumeric string
for i := 0; i < 3; i++ {
alphanumeric += strings.Split("abcdefghijklmnopqrstuvwxyz0123456789", "")[rand.Intn(35)]
}
return fmt.Sprintf("%v%v@yahoo.com", alpha, alphanumeric)
}
func getTraceIds(events []Event) {
for _, event := range events {
var contexts map[string]interface{}
if event.Kind == ERROR {
contexts = event.Error.Contexts
}
if event.Kind == TRANSACTION {
contexts = event.Transaction.Contexts
}
if contexts != nil {
if _, found := contexts["trace"]; found {
trace := contexts["trace"]
trace_id := trace.(map[string]interface{})["trace_id"].(string)
if trace_id != "" {
matched := false
for _, value := range traceIds {
if trace_id == value {
matched = true
}
}
if !matched {
traceIds = append(traceIds, trace_id)
}
}
}
}
}
// fmt.Println("> getTraceids traceIds", traceIds)
}
// Capture errors if they occur during Replay's execution
func initializeSentry() {
err := sentry.Init(sentry.ClientOptions{
Dsn: config.SentryJobMonitor,
Environment: config.Environment,
Release: time.Now().Month().String(),
})
if err != nil {
log.Fatalf("sentry.Init: %s", err)
}
if user, _ := user.Current(); user != nil {
sentry.ConfigureScope(func(scope *sentry.Scope) {
scope.SetUser(sentry.User{Username: user.Username, IPAddress: ip()})
})
}
defer sentry.Flush(2 * time.Second)
}
func ip() string {
url := "https://api.ipify.org?format=text"
resp, err := http.Get(url)
if err != nil {
sentry.CaptureException(err)
panic(err)
}
defer resp.Body.Close()
ip, err := ioutil.ReadAll(resp.Body)
if err != nil {
sentry.CaptureException(err)
panic(err)
}
return string(ip)
}
type Config struct {
Environment string
SentryJobMonitor string
Bucket string
GoogleApplicationCredentials string
SentryAuthToken string
Sources []string
Destinations map[string][]string
Skip string
}
func parseYamlConfig() {
filename := "config.yaml"
file, err := ioutil.ReadFile(filename)
if err != nil {
sentry.CaptureException(err)
panic(err)
}
err = yaml.Unmarshal(file, &config)
if err != nil {
sentry.CaptureException(err)
panic(err)
}
var msg string
// Must use job monitor if powering via DemoAutomation (scheduled job) in GCP
if config.Environment == "production" && config.SentryJobMonitor == "" {
msg = "no sentry job monitor dsn set"
}
if config.Bucket == "" {
msg = "no bucket set"
}
if config.GoogleApplicationCredentials == "" {
msg = "no app credentials set"
} else {
os.Setenv("GOOGLE_APPLICATION_CREDENTIALS", config.GoogleApplicationCredentials)
}
if len(config.Sources) == 0 {
msg = "No sources defined"
}
// AuthToken is only if using DiscoverAPI EventsAPI in demo_automation.go, which is default disabled.
// if config.SentryAuthToken == "" {
// msg = "no auth token"
// }
// if config.Skip == "" {
// msg = "no skip list provided"
// }
if msg != "" {
sentry.CaptureException(errors.New(msg))
log.Fatal(msg)
}
}
func prettyPrint(v interface{}) {
pp, _ := json.MarshalIndent(v, "", " ")
fmt.Print(string(pp))
}
func print(arg1 string, arg2 string) {
fmt.Println(arg1, arg2)
}
func updateTraceIds(events []Event) {
for _, TRACE_ID := range traceIds {
var uuid4 = strings.ReplaceAll(uuid.New().String(), "-", "")
NEW_TRACE_ID := uuid4
for _, event := range events {
if event.Kind == ERROR {
contexts := event.Error.Contexts
if contexts != nil {
trace := contexts["trace"]
if trace != nil { // need this or else kind:default's error out
if TRACE_ID == trace.(map[string]interface{})["trace_id"] {
// fmt.Println("\n> MATCHED Error trace_id BEFORE", trace.(map[string]interface{})["trace_id"])
trace.(map[string]interface{})["trace_id"] = NEW_TRACE_ID
// fmt.Println("> MATCHED Error trace_id AFTER", transport.bodyError["contexts"].(map[string]interface{})["trace"].(map[string]interface{})["trace_id"].(string))
}
}
}
}
if event.Kind == TRANSACTION {
contexts := event.Transaction.Contexts
if contexts != nil {
trace := contexts["trace"]
if TRACE_ID == trace.(map[string]interface{})["trace_id"] {
trace.(map[string]interface{})["trace_id"] = NEW_TRACE_ID
//fmt.Println("> MATCHED Transaction trace_id AFTER", item.(map[string]interface{})["contexts"].(map[string]interface{})["trace"].(map[string]interface{})["trace_id"].(string))
// should check if 'Spans' field exists. it may have been set to 0 if nothing was unmarshal'd to it
if len(event.Transaction.Spans) > 0 {
spans := event.Transaction.Spans
// if len(spans.([]interface{})) > 0 {
for _, value := range spans {
// fmt.Println("\n> SPAN Transaction trace_id BEFORE ", value["trace_id"])
value["trace_id"] = NEW_TRACE_ID
// fmt.Println("> SPAN Transaction trace_id AFTER", event.Transaction.Spans[0]["trace_id"])
}
// }
}
}
}
}
}
}
}