diff --git a/exceptionless/api.go b/exceptionless/api.go index 0129bc3..ac6beb1 100644 --- a/exceptionless/api.go +++ b/exceptionless/api.go @@ -6,24 +6,11 @@ import ( "fmt" "io/ioutil" "net/http" - "os" ) // Post posts to the Exceptionless Server func Post(endpoint string, postBody string, authorization string) string { - exceptionless := GetClient() - // err := godotenv.Load(".env") - - // if err != nil { - // log.Fatalf("Error loading .env file") - // } - - var baseURL string = os.Getenv("BASE_API_URL") - if exceptionless.ServerURL != "" { - baseURL = exceptionless.ServerURL - } - - url := baseURL + endpoint + url := conf.ServerURL + endpoint var jsonStr = []byte(postBody) req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr)) if err != nil { @@ -43,19 +30,7 @@ func Post(endpoint string, postBody string, authorization string) string { // GET makes api GET requests func Get(endpoint string, authorization string) map[string]interface{} { - exceptionless := GetClient() - // err := godotenv.Load(".env") - - // if err != nil { - // log.Fatalf("Error loading .env file") - // } - - var baseURL string = os.Getenv("BASE_API_URL") - if exceptionless.ServerURL != "" { - baseURL = exceptionless.ServerURL - } - - url := baseURL + endpoint + url := conf.ServerURL + endpoint httpClient := &http.Client{} req, err := http.NewRequest("GET", url, nil) diff --git a/exceptionless/builder.go b/exceptionless/builder.go deleted file mode 100644 index 7daf196..0000000 --- a/exceptionless/builder.go +++ /dev/null @@ -1,143 +0,0 @@ -package exceptionless - -import ( - "encoding/json" - "fmt" - "time" - - "github.com/google/uuid" -) - -// 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"` -} - -// 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 { - exceptionlessClient := GetClient() - if exceptionlessClient.UpdateSettingsWhenIdleInterval > 0 { - config := GetConfig() - fmt.Println(config) - // We are stripping out info accoring to the config settings - } - referenceID := uuid.Must(uuid.NewUUID()) - errorMap := map[string]interface{}{} - errorMap["type"] = "error" - errorMap["message"] = err.Error() - errorMap["date"] = time.Now().Format(time.RFC3339) - 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.NewUUID()) - 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 -} diff --git a/exceptionless/exceptionless.go b/exceptionless/exceptionless.go index d95b44c..59569b6 100644 --- a/exceptionless/exceptionless.go +++ b/exceptionless/exceptionless.go @@ -1,46 +1,36 @@ package exceptionless import ( + "encoding/json" "fmt" "math/rand" "time" + + "github.com/google/uuid" ) var config map[string]interface{} = nil -// Exceptionless type defines the client configuration structure -type Exceptionless struct { +// Config type defines the client configuration structure +type Config struct { ApiKey string ServerURL string UpdateSettingsWhenIdleInterval int32 - IncludePrivateInformation bool } -var client = Exceptionless{} +var conf *Config -func main() { - handlePolling() -} +func Init(config *Config) { + conf = config + if conf == nil { + return + } -func handlePolling() { - if client.ApiKey != "" && client.UpdateSettingsWhenIdleInterval > 0 { - fmt.Println("polling!") + if conf.ApiKey != "" && conf.UpdateSettingsWhenIdleInterval > 0 { poll() } } -// Configure is the function that creates an Exceptionless client -func Configure(config Exceptionless) Exceptionless { - client = config - handlePolling() - return client -} - -// GetClient returns the Exceptionless client -func GetClient() Exceptionless { - return client -} - // GetConfig returns the project configuration func GetConfig() map[string]interface{} { return config @@ -48,10 +38,10 @@ func GetConfig() map[string]interface{} { // SubmitEvent sends log events to Exceptionless func SubmitEvent(eventBody string) string { - if client.ApiKey == "" { - fmt.Println("is zero value") + if conf.ApiKey == "" { + return "" } - resp := Post("events", eventBody, client.ApiKey) + resp := Post("events", eventBody, conf.ApiKey) return resp } @@ -59,9 +49,166 @@ func poll() { r := rand.New(rand.NewSource(99)) c := time.Tick(10 * time.Second) for _ = range c { - resp := Get("projects/config", client.ApiKey) + resp := Get("projects/config", conf.ApiKey) config = resp - jitter := time.Duration(r.Int31n(client.UpdateSettingsWhenIdleInterval)) * time.Millisecond + jitter := time.Duration(r.Int31n(conf.UpdateSettingsWhenIdleInterval)) * time.Millisecond time.Sleep(jitter) } } + +// 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 string `json:"reference_id,omitempty"` + Count uint `json:"count,omitempty"` +} + +// 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.String() + 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 +} + +func SubmitAppError(source string, reqid *string, err error) string { + referenceID := uuid.Must(uuid.NewUUID()).String() + if reqid != nil { + referenceID = *reqid + } + + errorMap := map[string]interface{}{} + errorMap["type"] = "error" + errorMap["message"] = err.Error() + errorMap["date"] = time.Now().Format(time.RFC3339) + data := map[string]interface{}{} + data["@simple_error"] = errorMap + var event = Event{ + EventType: "error", + Message: err.Error(), + Data: data, + ReferenceID: referenceID, + Source: source, + } + json, err := json.Marshal(event) + if err != nil { + return err.Error() + } + resp := SubmitEvent(string(json)) + return resp +} + +// SubmitError is a convenience wrapper to quickly build and submit an error +func SubmitError(err error) string { + if conf.UpdateSettingsWhenIdleInterval > 0 { + GetConfig() + } + referenceID := uuid.Must(uuid.NewUUID()) + errorMap := map[string]interface{}{} + errorMap["type"] = "error" + errorMap["message"] = err.Error() + errorMap["date"] = time.Now().Format(time.RFC3339) + data := map[string]interface{}{} + data["@simple_error"] = errorMap + var mainEvent = Event{ + EventType: "error", + Message: err.Error(), + Data: data, + ReferenceID: referenceID.String(), + } + 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 { + referenceID := uuid.Must(uuid.NewUUID()) + if conf.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 +} diff --git a/exceptionless/userdescription.go b/exceptionless/userdescription.go deleted file mode 100644 index 1083c38..0000000 --- a/exceptionless/userdescription.go +++ /dev/null @@ -1,7 +0,0 @@ -package exceptionless - -type UserDescription struct { - email string - description string - data string -} diff --git a/exceptionless/useridentity.go b/exceptionless/useridentity.go deleted file mode 100644 index 2750cd3..0000000 --- a/exceptionless/useridentity.go +++ /dev/null @@ -1,7 +0,0 @@ -package exceptionless - -type UserIdentity struct { - identity string - name string - data string -}