1
0
mirror of https://github.com/golang/go synced 2024-11-18 14:34:39 -07:00

internal/telemetry: moving towards a unified event based exporter

This adds a type to events and makes the logging calls use it.

Change-Id: Iaa50fe2e332caae611b1e00424c878a3bc479feb
Reviewed-on: https://go-review.googlesource.com/c/tools/+/221741
Run-TryBot: Ian Cottrell <iancottrell@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com>
This commit is contained in:
Ian Cottrell 2020-03-01 12:16:26 -05:00
parent a1c56757aa
commit 046aa1cdaf
7 changed files with 42 additions and 24 deletions

View File

@ -570,18 +570,19 @@ func (e *exporter) FinishSpan(ctx context.Context, spn *telemetry.Span) {
} }
} }
func (e *exporter) Log(ctx context.Context, event telemetry.Event) { func (e *exporter) ProcessEvent(ctx context.Context, event telemetry.Event) context.Context {
i := GetInstance(ctx) i := GetInstance(ctx)
if event.Error != nil || i == nil { if event.Type == telemetry.EventLog && (event.Error != nil || i == nil) {
fmt.Fprintf(e.stderr, "%v\n", event) fmt.Fprintf(e.stderr, "%v\n", event)
} }
if i == nil {
return
}
protocol.LogEvent(ctx, event) protocol.LogEvent(ctx, event)
if i.ocagent != nil { if i == nil {
i.ocagent.Log(ctx, event) return ctx
} }
if i.ocagent != nil {
ctx = i.ocagent.ProcessEvent(ctx, event)
}
return ctx
} }
func (e *exporter) Metric(ctx context.Context, data telemetry.MetricData) { func (e *exporter) Metric(ctx context.Context, data telemetry.MetricData) {

View File

@ -18,14 +18,18 @@ func WithClient(ctx context.Context, client Client) context.Context {
return context.WithValue(ctx, clientKey, client) return context.WithValue(ctx, clientKey, client)
} }
func LogEvent(ctx context.Context, event telemetry.Event) { func LogEvent(ctx context.Context, event telemetry.Event) context.Context {
if event.Type != telemetry.EventLog {
return ctx
}
client, ok := ctx.Value(clientKey).(Client) client, ok := ctx.Value(clientKey).(Client)
if !ok { if !ok {
return return ctx
} }
msg := &LogMessageParams{Type: Info, Message: fmt.Sprint(event)} msg := &LogMessageParams{Type: Info, Message: fmt.Sprint(event)}
if event.Error != nil { if event.Error != nil {
msg.Type = Error msg.Type = Error
} }
go client.LogMessage(xcontext.Detach(ctx), msg) go client.LogMessage(xcontext.Detach(ctx), msg)
return ctx
} }

View File

@ -9,7 +9,14 @@ import (
"time" "time"
) )
type EventType uint8
const (
EventLog = EventType(iota)
)
type Event struct { type Event struct {
Type EventType
At time.Time At time.Time
Message string Message string
Error error Error error

View File

@ -18,14 +18,14 @@ import (
) )
type Exporter interface { type Exporter interface {
// ProcessEvent is a function that handles all events.
// Exporters may use information in the context to decide what to do with a
// given event.
ProcessEvent(context.Context, telemetry.Event) context.Context
StartSpan(context.Context, *telemetry.Span) StartSpan(context.Context, *telemetry.Span)
FinishSpan(context.Context, *telemetry.Span) FinishSpan(context.Context, *telemetry.Span)
// Log is a function that handles logging events.
// Observers may use information in the context to decide what to do with a
// given log event.
Log(context.Context, telemetry.Event)
Metric(context.Context, telemetry.MetricData) Metric(context.Context, telemetry.MetricData)
} }
@ -85,18 +85,18 @@ func Tag(ctx context.Context, at time.Time, tags telemetry.TagList) {
}) })
} }
func Log(ctx context.Context, event telemetry.Event) { func ProcessEvent(ctx context.Context, event telemetry.Event) context.Context {
exporterPtr := (*Exporter)(atomic.LoadPointer(&exporter)) exporterPtr := (*Exporter)(atomic.LoadPointer(&exporter))
if exporterPtr == nil { if exporterPtr == nil {
return return ctx
} }
// If context has a span we need to add the event to it // If context has a span we need to add the event to it
span := telemetry.GetSpan(ctx) span := telemetry.GetSpan(ctx)
if span != nil { if span != nil {
span.Events = append(span.Events, event) span.Events = append(span.Events, event)
} }
// and now also hand the event of to the current observer // and now also hand the event of to the current exporter
(*exporterPtr).Log(ctx, event) return (*exporterPtr).ProcessEvent(ctx, event)
} }
func Metric(ctx context.Context, data telemetry.MetricData) { func Metric(ctx context.Context, data telemetry.MetricData) {

View File

@ -27,10 +27,11 @@ type logWriter struct {
func (w *logWriter) StartSpan(context.Context, *telemetry.Span) {} func (w *logWriter) StartSpan(context.Context, *telemetry.Span) {}
func (w *logWriter) FinishSpan(context.Context, *telemetry.Span) {} func (w *logWriter) FinishSpan(context.Context, *telemetry.Span) {}
func (w *logWriter) Log(ctx context.Context, event telemetry.Event) { func (w *logWriter) ProcessEvent(ctx context.Context, event telemetry.Event) context.Context {
if w.onlyErrors && event.Error == nil { if w.onlyErrors && event.Error == nil {
return return ctx
} }
fmt.Fprintf(w.writer, "%v\n", event) fmt.Fprintf(w.writer, "%v\n", event)
return ctx
} }
func (w *logWriter) Metric(context.Context, telemetry.MetricData) {} func (w *logWriter) Metric(context.Context, telemetry.MetricData) {}

View File

@ -92,7 +92,9 @@ func (e *Exporter) FinishSpan(ctx context.Context, span *telemetry.Span) {
e.spans = append(e.spans, span) e.spans = append(e.spans, span)
} }
func (e *Exporter) Log(context.Context, telemetry.Event) {} func (e *Exporter) ProcessEvent(ctx context.Context, event telemetry.Event) context.Context {
return ctx
}
func (e *Exporter) Metric(ctx context.Context, data telemetry.MetricData) { func (e *Exporter) Metric(ctx context.Context, data telemetry.MetricData) {
e.mu.Lock() e.mu.Lock()

View File

@ -18,7 +18,8 @@ type Event telemetry.Event
// With sends a tag list to the installed loggers. // With sends a tag list to the installed loggers.
func With(ctx context.Context, tags ...telemetry.Tag) { func With(ctx context.Context, tags ...telemetry.Tag) {
export.Log(ctx, telemetry.Event{ export.ProcessEvent(ctx, telemetry.Event{
Type: telemetry.EventLog,
At: time.Now(), At: time.Now(),
Tags: tags, Tags: tags,
}) })
@ -27,7 +28,8 @@ func With(ctx context.Context, tags ...telemetry.Tag) {
// Print takes a message and a tag list and combines them into a single tag // Print takes a message and a tag list and combines them into a single tag
// list before delivering them to the loggers. // list before delivering them to the loggers.
func Print(ctx context.Context, message string, tags ...telemetry.Tag) { func Print(ctx context.Context, message string, tags ...telemetry.Tag) {
export.Log(ctx, telemetry.Event{ export.ProcessEvent(ctx, telemetry.Event{
Type: telemetry.EventLog,
At: time.Now(), At: time.Now(),
Message: message, Message: message,
Tags: tags, Tags: tags,
@ -42,7 +44,8 @@ func Error(ctx context.Context, message string, err error, tags ...telemetry.Tag
err = errorString(message) err = errorString(message)
message = "" message = ""
} }
export.Log(ctx, telemetry.Event{ export.ProcessEvent(ctx, telemetry.Event{
Type: telemetry.EventLog,
At: time.Now(), At: time.Now(),
Message: message, Message: message,
Error: err, Error: err,