mirror of
https://github.com/golang/go
synced 2024-11-19 02:24:41 -07:00
8bb11ff117
We should not be sending messages from within the telemetry worker. This does it in a new go routine now. Change-Id: I55e3b6df04699b8e45bc37b99997463f45ee114e Reviewed-on: https://go-review.googlesource.com/c/tools/+/186958 Run-TryBot: Ian Cottrell <iancottrell@google.com> Reviewed-by: Rebecca Stambler <rstambler@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
41 lines
862 B
Go
41 lines
862 B
Go
package protocol
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"golang.org/x/tools/internal/lsp/telemetry/log"
|
|
"golang.org/x/tools/internal/lsp/telemetry/tag"
|
|
"golang.org/x/tools/internal/xcontext"
|
|
)
|
|
|
|
func init() {
|
|
log.AddLogger(logger)
|
|
}
|
|
|
|
type contextKey int
|
|
|
|
const (
|
|
clientKey = contextKey(iota)
|
|
)
|
|
|
|
func WithClient(ctx context.Context, client Client) context.Context {
|
|
return context.WithValue(ctx, clientKey, client)
|
|
}
|
|
|
|
// logger implements log.Logger in terms of the LogMessage call to a client.
|
|
func logger(ctx context.Context, at time.Time, tags tag.List) bool {
|
|
client, ok := ctx.Value(clientKey).(Client)
|
|
if !ok {
|
|
return false
|
|
}
|
|
entry := log.ToEntry(ctx, time.Time{}, tags)
|
|
msg := &LogMessageParams{Type: Info, Message: fmt.Sprint(entry)}
|
|
if entry.Error != nil {
|
|
msg.Type = Error
|
|
}
|
|
go client.LogMessage(xcontext.Detach(ctx), msg)
|
|
return true
|
|
}
|