-
Notifications
You must be signed in to change notification settings - Fork 3
/
demo_automation.go
129 lines (114 loc) · 3.28 KB
/
demo_automation.go
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
package main
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"time"
"cloud.google.com/go/storage"
"github.com/getsentry/sentry-go"
"google.golang.org/api/iterator"
)
type DemoAutomation struct{}
// Get events from both Sentry and GCS
func (d *DemoAutomation) getEvents() []Event {
var events []Event
events1 := d.getEventsFromSentry()
events2 := d.getEventsFromGCS()
events = append(events, events1...)
events = append(events, events2...)
return events
}
// Download the events from Sentry. You may have to be a team member on the org you're downloading events from (SENTRY_AUTH_TOKEN)
func (d *DemoAutomation) getEventsFromSentry() []Event {
var events []Event
discoverAPI := DiscoverAPI{}
eventsAPI := EventsAPI{}
for _, org := range config.Sources {
eventMetadata := discoverAPI.latestEventMetadata(org, *n)
_events := eventsAPI.getEvents(org, eventMetadata)
events = append(events, _events...)
}
fmt.Printf("\n> EVENTS from API: %v \n", len(events))
return events
}
// Gets events from Google Cloud Storage
func (d *DemoAutomation) getEventsFromGCS() []Event {
// Initialize/Connect the Client
ctx := context.Background()
client, err := storage.NewClient(ctx)
if err != nil {
sentry.CaptureException(err)
log.Fatalln("storage.NewClient:", err)
}
defer client.Close()
ctx, cancel := context.WithTimeout(ctx, time.Second*50)
defer cancel()
// Get the bucket and its file names
bucketName := config.Bucket
bucketHandle := client.Bucket(bucketName)
var fileNames []string
query := &storage.Query{Prefix: *filePrefix}
it := bucketHandle.Objects(ctx, query)
for {
obj, err := it.Next()
if err == iterator.Done {
sentry.ConfigureScope(func(scope *sentry.Scope) {
scope.SetTag("files", fmt.Sprint(len(fileNames)))
})
sentry.CaptureMessage(fmt.Sprintf("finished retrieving files"))
break
}
if err != nil {
sentry.CaptureException(err)
log.Fatalln("listBucket: unable to list bucket", err)
}
fileNames = append(fileNames, obj.Name)
printObj(obj)
}
// Get the files
var events []Event
for _, fileName := range fileNames {
rc, err := bucketHandle.Object(fileName).NewReader(ctx)
if err != nil {
sentry.CaptureException(err)
log.Fatalln("NewReader:", err)
}
byteValue, _ := ioutil.ReadAll(rc)
// Dev Note - The Event's UnmarshalJSON method is overriden in Event.go
var event Event
if err := json.Unmarshal(byteValue, &event); err != nil {
sentry.CaptureException(err)
panic(err)
}
event.setPlatform()
event.undertake()
events = append(events, event)
events = removeMechanism(events)
}
return events
}
func removeMechanism(_events []Event) []Event {
for _, event := range _events {
if event.Kind == ERROR || event.Kind == DEFAULT {
exception := event.Error.Exception
values := exception["values"]
if values != nil {
for _, value := range values.([]interface{}) {
mechanism := value.(map[string]interface{})["mechanism"]
if mechanism != nil {
mechanismType := mechanism.(map[string]interface{})["type"]
if mechanismType == "minidump" {
delete(value.(map[string]interface{}), "mechanism")
}
}
}
}
}
}
return _events
}
func printObj(obj *storage.ObjectAttrs) {
fmt.Printf("filename: /%v/%v \n", obj.Bucket, obj.Name) // .ContentType .Owner .Size
}