add signoz

This commit is contained in:
trunglt251292 2023-11-23 14:39:52 +07:00
parent c6f8ab690f
commit 4cbabe4282
4 changed files with 129 additions and 0 deletions

25
constants.go Normal file
View File

@ -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.
}
)

12
fatal.go Normal file
View File

@ -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)))
}

23
model.go Normal file
View File

@ -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"`
}

69
signoz.go Normal file
View File

@ -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]
}