Files
mostlymatter/server/platform/services/tracing/tracing.go
Agniva De Sarker efaa6264cc MM-53032: Fix module path after repo rename (#23689)
It was a good decision in hindsight to keep the public module as 0.x
because this would have been a breaking change again.

https://mattermost.atlassian.net/browse/MM-53032
```release-note
Changed the Go module path from github.com/mattermost/mattermost-server/server/v8 to github.com/mattermost/mattermost/server/v8.

For the public facing module, it's path is also changed from github.com/mattermost/mattermost-server/server/public to github.com/mattermost/mattermost/server/public
```
2023-06-11 10:54:35 +05:30

88 строки
2.4 KiB
Go

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package tracing
import (
"context"
"io"
"time"
opentracing "github.com/opentracing/opentracing-go"
"github.com/uber/jaeger-client-go"
jaegercfg "github.com/uber/jaeger-client-go/config"
"github.com/uber/jaeger-client-go/zipkin"
"github.com/uber/jaeger-lib/metrics"
"github.com/mattermost/mattermost/server/public/shared/mlog"
)
// Tracer is a wrapper around Jaeger OpenTracing client, used to properly de-initialize jaeger on exit
type Tracer struct {
closer io.Closer
}
type LogrusAdapter struct {
}
// Error - logrus adapter for span errors
func (LogrusAdapter) Error(msg string) {
mlog.Error(msg)
}
// Infof - logrus adapter for span info logging
func (LogrusAdapter) Infof(msg string, args ...any) {
// we ignore Info messages from opentracing
}
// New instantiates Jaeger opentracing client with default options
// To override the defaults use environment variables listed here: https://github.com/jaegertracing/jaeger-client-go/blob/master/config/config.go
func New() (*Tracer, error) {
cfg := jaegercfg.Configuration{
Sampler: &jaegercfg.SamplerConfig{
Type: jaeger.SamplerTypeConst,
Param: 1,
},
Reporter: &jaegercfg.ReporterConfig{
LogSpans: true,
},
}
zipkinPropagator := zipkin.NewZipkinB3HTTPHeaderPropagator()
closer, err := cfg.InitGlobalTracer(
"mattermost",
jaegercfg.Logger(LogrusAdapter{}),
jaegercfg.Metrics(metrics.NullFactory),
jaegercfg.Tag("serverStartTime", time.Now().UTC().Format(time.RFC3339)),
jaegercfg.Injector(opentracing.HTTPHeaders, zipkinPropagator),
jaegercfg.Extractor(opentracing.HTTPHeaders, zipkinPropagator),
jaegercfg.ZipkinSharedRPCSpan(true),
)
if err != nil {
return nil, err
}
mlog.Info("Opentracing initialized")
return &Tracer{
closer: closer,
}, nil
}
func (t *Tracer) Close() error {
return t.closer.Close()
}
func StartRootSpanByContext(ctx context.Context, operationName string) (opentracing.Span, context.Context) {
return opentracing.StartSpanFromContext(ctx, operationName)
}
func StartSpanWithParentByContext(ctx context.Context, operationName string) (opentracing.Span, context.Context) {
parentSpan := opentracing.SpanFromContext(ctx)
if parentSpan == nil {
return StartRootSpanByContext(ctx, operationName)
}
return opentracing.StartSpanFromContext(ctx, operationName, opentracing.ChildOf(parentSpan.Context()))
}