1
0
mirror of https://github.com/golang/go synced 2024-11-19 02:24:41 -07:00
go/internal/lsp/protocol/context.go
Ian Cottrell 046aa1cdaf 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>
2020-03-03 22:56:52 +00:00

36 lines
693 B
Go

package protocol
import (
"context"
"fmt"
"golang.org/x/tools/internal/telemetry"
"golang.org/x/tools/internal/xcontext"
)
type contextKey int
const (
clientKey = contextKey(iota)
)
func WithClient(ctx context.Context, client Client) context.Context {
return context.WithValue(ctx, clientKey, client)
}
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 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
}