1
0
mirror of https://github.com/golang/go synced 2024-11-18 12:44:49 -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)
if event.Error != nil || i == nil {
if event.Type == telemetry.EventLog && (event.Error != nil || i == nil) {
fmt.Fprintf(e.stderr, "%v\n", event)
}
if i == nil {
return
}
protocol.LogEvent(ctx, event)
if i.ocagent != nil {
i.ocagent.Log(ctx, event)
if i == nil {
return ctx
}
if i.ocagent != nil {
ctx = i.ocagent.ProcessEvent(ctx, event)
}
return ctx
}
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)
}
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)
if !ok {
return
return ctx
}
msg := &LogMessageParams{Type: Info, Message: fmt.Sprint(event)}
if event.Error != nil {
msg.Type = Error
}
go client.LogMessage(xcontext.Detach(ctx), msg)
return ctx
}

View File

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

View File

@ -18,14 +18,14 @@ import (
)
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)
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)
}
@ -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))
if exporterPtr == nil {
return
return ctx
}
// If context has a span we need to add the event to it
span := telemetry.GetSpan(ctx)
if span != nil {
span.Events = append(span.Events, event)
}
// and now also hand the event of to the current observer
(*exporterPtr).Log(ctx, event)
// and now also hand the event of to the current exporter
return (*exporterPtr).ProcessEvent(ctx, event)
}
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) 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 {
return
return ctx
}
fmt.Fprintf(w.writer, "%v\n", event)
return ctx
}
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)
}
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) {
e.mu.Lock()

View File

@ -18,7 +18,8 @@ type Event telemetry.Event
// With sends a tag list to the installed loggers.
func With(ctx context.Context, tags ...telemetry.Tag) {
export.Log(ctx, telemetry.Event{
export.ProcessEvent(ctx, telemetry.Event{
Type: telemetry.EventLog,
At: time.Now(),
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
// list before delivering them to the loggers.
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(),
Message: message,
Tags: tags,
@ -42,7 +44,8 @@ func Error(ctx context.Context, message string, err error, tags ...telemetry.Tag
err = errorString(message)
message = ""
}
export.Log(ctx, telemetry.Event{
export.ProcessEvent(ctx, telemetry.Event{
Type: telemetry.EventLog,
At: time.Now(),
Message: message,
Error: err,