sync
This commit is contained in:
@ -2,6 +2,7 @@ package gelfexporter
|
||||
|
||||
type config struct {
|
||||
GelfUrl string
|
||||
AppName string
|
||||
}
|
||||
|
||||
// newConfig creates a validated Config configured with options.
|
||||
@ -18,20 +19,6 @@ 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)
|
||||
}
|
||||
@ -43,26 +30,13 @@ func (o gelfUrlOption) apply(cfg config) config {
|
||||
return cfg
|
||||
}
|
||||
|
||||
// // WithPrettyPrint prettifies the emitted output.
|
||||
// func WithPrettyPrint() Option {
|
||||
// return prettyPrintOption(true)
|
||||
// }
|
||||
func WithAppName(url string) Option {
|
||||
return appName(url)
|
||||
}
|
||||
|
||||
// type prettyPrintOption bool
|
||||
type appName string
|
||||
|
||||
// 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
|
||||
// }
|
||||
func (o appName) apply(cfg config) config {
|
||||
cfg.AppName = string(o)
|
||||
return cfg
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package gelfexporter
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"maal/tracer/pkg/level"
|
||||
"time"
|
||||
@ -30,6 +31,7 @@ func Log(writer *gelf.UDPWriter, msg GELFMessage) {
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
fmt.Printf("msg: %v sent\n", msg)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -2,17 +2,15 @@ package gelfexporter
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"maal/tracer/pkg/level"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"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.
|
||||
@ -33,16 +31,13 @@ func New(options ...Option) (*Exporter, error) {
|
||||
|
||||
}
|
||||
|
||||
// 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
|
||||
appName string
|
||||
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
|
||||
@ -62,27 +57,70 @@ func (e *Exporter) ExportSpans(ctx context.Context, spans []trace.ReadOnlySpan)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
var attributes = make(map[string]interface{})
|
||||
for _, attr := range stub.Attributes {
|
||||
attributes[string(attr.Key)] = GetByType(attr.Value)
|
||||
}
|
||||
|
||||
fmt.Printf("stub: %v\n", stub)
|
||||
attributes["from"] = "test"
|
||||
|
||||
Log(e.gelfWriter, GELFMessage{
|
||||
Host: "test",
|
||||
ShortMessage: "test",
|
||||
})
|
||||
for i := range stub.Events {
|
||||
event := stub.Events[i]
|
||||
|
||||
var gelf GELFMessage = GELFMessage{
|
||||
Host: "salego",
|
||||
ShortMessage: event.Name,
|
||||
Timestamp: stub.StartTime,
|
||||
Level: level.DEBUG,
|
||||
ExtraFields: attributes,
|
||||
}
|
||||
for _, attr := range event.Attributes {
|
||||
if attr.Key == "long_message_" {
|
||||
gelf.LongMessage = attr.Value.AsString()
|
||||
continue
|
||||
}
|
||||
|
||||
if attr.Key == "level_" {
|
||||
gelf.Level = level.SyslogLevel(attr.Value.AsInt64())
|
||||
continue
|
||||
}
|
||||
|
||||
attributes[string(attr.Key)] = GetByType(attr.Value)
|
||||
}
|
||||
|
||||
Log(e.gelfWriter, gelf)
|
||||
}
|
||||
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetByType(val attribute.Value) interface{} {
|
||||
switch val.Type() {
|
||||
case attribute.INVALID:
|
||||
return "invalid value"
|
||||
case attribute.BOOL:
|
||||
return val.AsBool()
|
||||
case attribute.INT64:
|
||||
return val.AsInt64()
|
||||
case attribute.FLOAT64:
|
||||
return val.AsFloat64()
|
||||
case attribute.STRING:
|
||||
return val.AsString()
|
||||
case attribute.BOOLSLICE:
|
||||
return val.AsBoolSlice()
|
||||
case attribute.INT64SLICE:
|
||||
return val.AsInt64Slice()
|
||||
case attribute.FLOAT64SLICE:
|
||||
return val.AsFloat64Slice()
|
||||
case attribute.STRINGSLICE:
|
||||
return val.AsStringSlice()
|
||||
}
|
||||
|
||||
return "invalid value"
|
||||
}
|
||||
|
||||
// Shutdown is called to stop the exporter, it performs no action.
|
||||
func (e *Exporter) Shutdown(ctx context.Context) error {
|
||||
e.stoppedMu.Lock()
|
||||
|
Reference in New Issue
Block a user