From 4cbabe42820dec0a050b7b1228502e30fc674f48 Mon Sep 17 00:00:00 2001 From: trunglt251292 Date: Thu, 23 Nov 2023 14:39:52 +0700 Subject: [PATCH] add signoz --- constants.go | 25 +++++++++++++++++++ fatal.go | 12 +++++++++ model.go | 23 ++++++++++++++++++ signoz.go | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 129 insertions(+) create mode 100644 constants.go create mode 100644 fatal.go create mode 100644 model.go create mode 100644 signoz.go diff --git a/constants.go b/constants.go new file mode 100644 index 0000000..b3e73a4 --- /dev/null +++ b/constants.go @@ -0,0 +1,25 @@ +package logger + +const ( + CtxKeyRequestID = "request_id" +) + +const ( + SeverityTraceLevel = 6 + SeverityDebugLevel = 5 + SeverityInfoLevel = 4 + SeverityWarnLevel = 3 + SeverityErrorLevel = 2 + SeverityFatalLevel = 1 +) + +var ( + MapStringSeverityLevel = map[int]string{ + SeverityFatalLevel: "FATAL", // Any error that is forcing a shutdown of the service or application to prevent data loss (or further data loss). I reserve these only for the most heinous errors and situations where there is guaranteed to have been data corruption or loss + SeverityErrorLevel: "ERROR", // Any error which is fatal to the operation, but not the service or application (can't open a required file, missing data, etc.). These errors will force user (administrator, or direct user) intervention. These are usually reserved (in my apps) for incorrect connection strings, missing services, etc. + SeverityWarnLevel: "WARN", // Anything that can potentially cause application oddities, but for which I am automatically recovering. (Such as switching from a primary to backup server, retrying an operation, missing secondary data, etc.) + SeverityInfoLevel: "INFO", // Generally useful information to log (service start/stop, configuration assumptions, etc). Info I want to always have available but usually don't care about under normal circumstances. This is my out-of-the-box config level. + SeverityDebugLevel: "DEBUG", // Information that is diagnostically helpful to people more than just developers (IT, sysadmins, etc.). + SeverityTraceLevel: "TRACE", // Only when I would be "tracing" the code and trying to find one part of a function specifically. + } +) diff --git a/fatal.go b/fatal.go new file mode 100644 index 0000000..3ed8f4f --- /dev/null +++ b/fatal.go @@ -0,0 +1,12 @@ +package logger + +import ( + "encoding/json" + "go.uber.org/zap" +) + +// Fatal ... +func Fatal(content string, data LogData) { + jsonData, _ := json.Marshal(data) + zapLogger.Fatal(content, zap.String("data", string(jsonData))) +} diff --git a/model.go b/model.go new file mode 100644 index 0000000..fe5037e --- /dev/null +++ b/model.go @@ -0,0 +1,23 @@ +package logger + +type SignozConfiguration struct { + HttpEndPoint string // Example: http://signoz:2255 + ServiceName string // Name server +} + +type SignozLogPayload struct { + SeverityLevel int `json:"severity_level"` + Message string `json:"message"` + Data interface{} `json:"data"` +} + +// PRIVATE +type logData struct { + SeverityNumber int `json:"severity_number"` + SeverityText string `json:"severity_text"` + Timestamp int64 `json:"timestamp"` + TraceID string `json:"trace_id"` + SpanID string `json:"span_id"` + Message string `json:"message"` + Source string `json:"source"` +} diff --git a/signoz.go b/signoz.go new file mode 100644 index 0000000..4c5b593 --- /dev/null +++ b/signoz.go @@ -0,0 +1,69 @@ +package logger + +import ( + "bytes" + "context" + "encoding/json" + "net/http" + "time" +) + +var ( + cfgSignoz SignozConfiguration +) + +// InitSignoz ... +func InitSignoz(config SignozConfiguration) { + cfgSignoz = config +} + +// SendLogToSignoz ... +func SendLogToSignoz(ctx context.Context, payload SignozLogPayload) error { + client := http.Client{} + b := generateBody(ctx, payload) + r, errRequest := http.NewRequest(http.MethodPost, cfgSignoz.HttpEndPoint, bytes.NewBuffer(b)) + if errRequest != nil { + return errRequest + } + r.Header.Set("Content-Type", "application/json") + resp, errRequest := client.Do(r) + if errRequest != nil { + return errRequest + } + defer resp.Body.Close() + return nil +} + +func generateBody(ctx context.Context, info SignozLogPayload) []byte { + var ( + timestamp = time.Now().Unix() + payload = logData{ + SeverityNumber: info.SeverityLevel, + SeverityText: getSeverityLevelText(info.SeverityLevel), + Timestamp: timestamp, + Source: cfgSignoz.ServiceName, + Message: "", + } + ) + body := map[string]interface{}{ + "message": info.Message, + "level_number": info.SeverityLevel, + "level": getSeverityLevelText(info.SeverityLevel), + "request_id": "", + "data": info.Data, + } + rid := ctx.Value(CtxKeyRequestID) + if rid != nil { + body["request_id"] = rid.(string) + payload.TraceID = rid.(string) + payload.SpanID = rid.(string) + } + msg, _ := json.Marshal(body) + payload.Message = string(msg) + b, _ := json.Marshal(payload) + return b +} + +func getSeverityLevelText(level int) string { + return MapStringSeverityLevel[level] +}