-
Notifications
You must be signed in to change notification settings - Fork 1
/
builder.go
155 lines (139 loc) · 4.24 KB
/
builder.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
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
package exceptionless
import (
"encoding/json"
"fmt"
"time"
"github.com/satori/go.uuid"
"github.com/go-errors/errors"
)
//Event is the main model for events
type Event struct {
EventType string `json:"type"`
Source string `json:"source,omitempty"`
Date string `json:"date,omitempty"`
Tags []string `json:"tags,omitempty"`
Message string `json:"message,omitempty"`
Geo string `json:"geo,omitempty"`
Value uint `json:"value,omitempty"`
Data map[string]interface{} `json:"data,omitempty"`
ReferenceID uuid.UUID `json:"reference_id,omitempty"`
Count uint `json:"count,omitempty"`
}
func GetBaseURL() string {
return "https://collector.exceptionless.com/api/v2/"
}
//GetBaseEvent returns an empty Event struct that can be built into any type of event.
func GetBaseEvent(eventType string, message string, date string) Event {
return Event{
EventType: eventType,
Message: message,
Date: date,
}
}
//AddSource adds a string value source to an event
func AddSource(event Event, source string) Event {
event.Source = source
return event
}
//AddTags adds a string array of tags for the event
func AddTags(event Event, tags []string) Event {
event.Tags = tags
return event
}
//AddGeo adds the lat and long location of the user the event impacted
func AddGeo(event Event, geo string) Event {
event.Geo = geo
return event
}
//AddValue adds an arbitrary number value to the event
func AddValue(event Event, value uint) Event {
event.Value = value
return event
}
//AddReferenceID adds an indentifier to later refer to this event
func AddReferenceID(event Event, referenceID uuid.UUID) Event {
event.ReferenceID = referenceID
return event
}
//AddCount adds a number to help track the number of times the event has occurred
func AddCount(event Event, count uint) Event {
event.Count = count
return event
}
func AddLogLevel(event Event, level string) Event {
var updatedEvent Event
if event.Data != nil {
event.Data["@level"] = level
updatedEvent = event
} else {
data := map[string]interface{}{}
data["@level"] = level
updatedEvent = AddData(event, data)
}
return updatedEvent
}
//AddData adds a string mapping to create a data object of additional values
func AddData(event Event, data map[string]interface{}) Event {
event.Data = data
return event
}
//SubmitError is a convenience wrapper to quickly build and submit an error
func SubmitError(err error) string {
if ExceptionlessClient.UpdateSettingsWhenIdleInterval > 0 {
config := GetConfig()
fmt.Println(config)
// We are stripping out info accoring to the config settings
}
referenceID := uuid.Must(uuid.NewV4())
errorMap := map[string]interface{}{}
errorMap["type"] = "error"
errorMap["message"] = err.Error()
errorMap["date"] = time.Now().Format(time.RFC3339)
errorMap["stack_trace"] = err.(*errors.Error).ErrorStack()
data := map[string]interface{}{}
data["@simple_error"] = errorMap
var mainEvent = Event{
EventType: "error",
Message: err.Error(),
Data: data,
ReferenceID: referenceID,
}
json, err := json.Marshal(mainEvent)
if err != nil {
fmt.Println(err)
return err.Error()
}
resp := SubmitEvent(string(json))
return resp
}
func SubmitLog(message string, level string) string {
exceptionlessClient := GetClient()
referenceID := uuid.Must(uuid.NewV4())
if exceptionlessClient.UpdateSettingsWhenIdleInterval > 0 {
config := GetConfig()
fmt.Println(config)
// We are stripping out info accoring to the config settings
// We would also prevent logs of levels below the log level set by the settings
}
var event Event
date := time.Now().Format(time.RFC3339)
event = GetBaseEvent("log", message, date)
event = AddReferenceID(event, referenceID)
data := map[string]interface{}{}
data["@level"] = level
event = AddData(event, data)
json, err := json.Marshal(event)
if err != nil {
fmt.Println(err)
return err.Error()
}
resp := SubmitEvent(string(json))
return resp
}
func SubmitEvent(eventBody string) string {
if ExceptionlessClient.ApiKey == "" {
fmt.Println("is zero value")
}
resp := Post("events", eventBody, ExceptionlessClient.ApiKey)
return resp
}