initial commit
This commit is contained in:
68
pkg/gelf_exporter/config.go
Normal file
68
pkg/gelf_exporter/config.go
Normal file
@ -0,0 +1,68 @@
|
||||
package gelfexporter
|
||||
|
||||
type config struct {
|
||||
GelfUrl string
|
||||
}
|
||||
|
||||
// newConfig creates a validated Config configured with options.
|
||||
func newConfig(options ...Option) (config, error) {
|
||||
cfg := config{}
|
||||
for _, opt := range options {
|
||||
cfg = opt.apply(cfg)
|
||||
}
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
// Option sets the value of an option for a Config.
|
||||
type Option interface {
|
||||
apply(config) config
|
||||
}
|
||||
|
||||
// WithWriter sets the export stream destination.
|
||||
// func WithWriter(w io.Writer) Option {
|
||||
// return writerOption{w}
|
||||
// }
|
||||
|
||||
// type writerOption struct {
|
||||
// W *gelf.Writer
|
||||
// }
|
||||
|
||||
// func (o writerOption) apply(cfg config) config {
|
||||
// cfg.Writer = o.W
|
||||
// return cfg
|
||||
// }
|
||||
|
||||
func WithGelfUrl(url string) Option {
|
||||
return gelfUrlOption(url)
|
||||
}
|
||||
|
||||
type gelfUrlOption string
|
||||
|
||||
func (o gelfUrlOption) apply(cfg config) config {
|
||||
cfg.GelfUrl = string(o)
|
||||
return cfg
|
||||
}
|
||||
|
||||
// // WithPrettyPrint prettifies the emitted output.
|
||||
// func WithPrettyPrint() Option {
|
||||
// return prettyPrintOption(true)
|
||||
// }
|
||||
|
||||
// type prettyPrintOption bool
|
||||
|
||||
// func (o prettyPrintOption) apply(cfg config) config {
|
||||
// cfg.PrettyPrint = bool(o)
|
||||
// return cfg
|
||||
// }
|
||||
|
||||
// // WithoutTimestamps sets the export stream to not include timestamps.
|
||||
// func WithoutTimestamps() Option {
|
||||
// return timestampsOption(false)
|
||||
// }
|
||||
|
||||
// type timestampsOption bool
|
||||
|
||||
// func (o timestampsOption) apply(cfg config) config {
|
||||
// cfg.Timestamps = bool(o)
|
||||
// return cfg
|
||||
// }
|
46
pkg/gelf_exporter/gelf.go
Normal file
46
pkg/gelf_exporter/gelf.go
Normal file
@ -0,0 +1,46 @@
|
||||
package gelfexporter
|
||||
|
||||
import (
|
||||
"log"
|
||||
"maal/tracer/pkg/level"
|
||||
"time"
|
||||
|
||||
"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 level.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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
}
|
||||
}
|
104
pkg/gelf_exporter/trace.go
Normal file
104
pkg/gelf_exporter/trace.go
Normal file
@ -0,0 +1,104 @@
|
||||
package gelfexporter
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"go.opentelemetry.io/otel/sdk/trace"
|
||||
"go.opentelemetry.io/otel/sdk/trace/tracetest"
|
||||
"gopkg.in/Graylog2/go-gelf.v2/gelf"
|
||||
)
|
||||
|
||||
var zeroTime time.Time
|
||||
|
||||
var _ trace.SpanExporter = &Exporter{}
|
||||
|
||||
// New creates an Exporter with the passed options.
|
||||
func New(options ...Option) (*Exporter, error) {
|
||||
cfg, err := newConfig(options...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
writer, err := gelf.NewUDPWriter(cfg.GelfUrl)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Exporter{
|
||||
gelfWriter: writer,
|
||||
}, nil
|
||||
|
||||
}
|
||||
|
||||
// Exporter is an implementation of trace.SpanSyncer that writes spans to stdout.
|
||||
type Exporter struct {
|
||||
timestamps bool
|
||||
gelfWriter *gelf.UDPWriter
|
||||
|
||||
stoppedMu sync.RWMutex
|
||||
stopped bool
|
||||
}
|
||||
|
||||
// ExportSpans writes spans in json format to stdout.
|
||||
func (e *Exporter) ExportSpans(ctx context.Context, spans []trace.ReadOnlySpan) error {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
e.stoppedMu.RLock()
|
||||
stopped := e.stopped
|
||||
e.stoppedMu.RUnlock()
|
||||
if stopped {
|
||||
return nil
|
||||
}
|
||||
|
||||
if len(spans) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
stubs := tracetest.SpanStubsFromReadOnlySpans(spans)
|
||||
|
||||
for i := range stubs {
|
||||
stub := &stubs[i]
|
||||
// Remove timestamps
|
||||
if !e.timestamps {
|
||||
stub.StartTime = zeroTime
|
||||
stub.EndTime = zeroTime
|
||||
for j := range stub.Events {
|
||||
ev := &stub.Events[j]
|
||||
ev.Time = zeroTime
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Printf("stub: %v\n", stub)
|
||||
|
||||
Log(e.gelfWriter, GELFMessage{
|
||||
Host: "test",
|
||||
ShortMessage: "test",
|
||||
})
|
||||
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Shutdown is called to stop the exporter, it performs no action.
|
||||
func (e *Exporter) Shutdown(ctx context.Context) error {
|
||||
e.stoppedMu.Lock()
|
||||
e.stopped = true
|
||||
e.stoppedMu.Unlock()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// // MarshalLog is the marshaling function used by the logging system to represent this Exporter.
|
||||
// func (e *Exporter) MarshalLog() interface{} {
|
||||
// return struct {
|
||||
// Type string
|
||||
// WithTimestamps bool
|
||||
// }{
|
||||
// Type: "stdout",
|
||||
// WithTimestamps: e.timestamps,
|
||||
// }
|
||||
// }
|
Reference in New Issue
Block a user