observer/pkg/gelf_exporter/gelf.go
Natalia Goc ab5b70704d standardize commonly used attributes
Some commonly used at maal attributes have been encoded as consts with
convinience wrappers similar to those of semconv package from otel sdk.
Additionally some utils that can generate mutliple attributes were added.
2024-05-16 13:45:13 +02:00

50 lines
1.1 KiB
Go

package gelfexporter
import (
"fmt"
"log"
"time"
"git.ma-al.com/gora_filip/pkg/syslog"
"gopkg.in/Graylog2/go-gelf.v2/gelf"
)
type GELFMessage struct {
// Name of the application
Host string `json:"host"`
// Short, descriptive message
ShortMessage string `json:"short_message"`
// Optional long message.
LongMessage string `json:"long_message,omitempty"`
// Timestamp in Unix
Timestamp time.Time `json:"timestamp"`
// Severity level matching Syslog standard.
Level syslog.SyslogLevel `json:"level"`
// All additional field names must start with an underline.
ExtraFields map[string]interface{} `json:"extrafields,omitempty"`
}
func Log(writer *gelf.UDPWriter, msg GELFMessage) {
if writer != nil {
err := writer.WriteMessage(msg.GELFFormat())
if err != nil {
log.Println(err)
}
fmt.Printf("msg: %v sent\n", msg)
}
}
func (g GELFMessage) GELFFormat() *gelf.Message {
return &gelf.Message{
Version: "1.1",
Host: g.Host,
Short: g.ShortMessage,
Full: g.LongMessage,
TimeUnix: float64(g.Timestamp.Unix()),
Level: int32(g.Level),
Extra: g.ExtraFields,
}
}