introduce otlp tracing (#5132)
* introduce otel tracing Signed-off-by: Jörn Friedrich Dreyer <jfd@butonic.de> * use new trace provider initialization Signed-off-by: Jörn Friedrich Dreyer <jfd@butonic.de> * work * bump reva Signed-off-by: Jörn Friedrich Dreyer <jfd@butonic.de> * remove commented code Signed-off-by: Jörn Friedrich Dreyer <jfd@butonic.de> * add vendor changes Signed-off-by: Jörn Friedrich Dreyer <jfd@butonic.de> --------- Signed-off-by: Jörn Friedrich Dreyer <jfd@butonic.de>
This commit is contained in:
+47
@@ -0,0 +1,47 @@
|
||||
// Copyright 2018-2021 CERN
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
// In applying this license, CERN does not waive the privileges and immunities
|
||||
// granted to it by virtue of its status as an Intergovernmental Organization
|
||||
// or submit itself to any jurisdiction.
|
||||
|
||||
package trace
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
)
|
||||
|
||||
type ctxKey struct{}
|
||||
|
||||
// ContextSetTracerProvider returns a copy of ctx with p associated.
|
||||
func ContextSetTracerProvider(ctx context.Context, p trace.TracerProvider) context.Context {
|
||||
if tp, ok := ctx.Value(ctxKey{}).(trace.TracerProvider); ok {
|
||||
if tp == p {
|
||||
return ctx
|
||||
}
|
||||
}
|
||||
return context.WithValue(ctx, ctxKey{}, p)
|
||||
}
|
||||
|
||||
// ContextGetTracerProvider returns the TracerProvider associated with the ctx.
|
||||
// If no TracerProvider is associated is associated, the global default TracerProvider
|
||||
// is returned
|
||||
func ContextGetTracerProvider(ctx context.Context) trace.TracerProvider {
|
||||
if p, ok := ctx.Value(ctxKey{}).(trace.TracerProvider); ok {
|
||||
return p
|
||||
}
|
||||
return trace.NewNoopTracerProvider()
|
||||
}
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
package trace
|
||||
|
||||
import "google.golang.org/grpc/credentials"
|
||||
|
||||
// Options for trace
|
||||
type Options struct {
|
||||
Enabled bool
|
||||
Insecure bool
|
||||
Exporter string
|
||||
Collector string
|
||||
Endpoint string
|
||||
ServiceName string
|
||||
TransportCredentials credentials.TransportCredentials
|
||||
}
|
||||
|
||||
// Option for trace
|
||||
type Option func(o *Options)
|
||||
|
||||
// WithEnabled option
|
||||
func WithEnabled() Option {
|
||||
return func(o *Options) {
|
||||
o.Enabled = true
|
||||
}
|
||||
}
|
||||
|
||||
// WithExporter option
|
||||
func WithExporter(v string) Option {
|
||||
return func(o *Options) {
|
||||
o.Exporter = v
|
||||
}
|
||||
}
|
||||
|
||||
// WithInsecure option
|
||||
func WithInsecure() Option {
|
||||
return func(o *Options) {
|
||||
o.Insecure = true
|
||||
}
|
||||
}
|
||||
|
||||
// WithCollector option
|
||||
func WithCollector(v string) Option {
|
||||
return func(o *Options) {
|
||||
o.Collector = v
|
||||
}
|
||||
}
|
||||
|
||||
// WithEndpoint option
|
||||
func WithEndpoint(v string) Option {
|
||||
return func(o *Options) {
|
||||
o.Endpoint = v
|
||||
}
|
||||
}
|
||||
|
||||
// WithServiceName option
|
||||
func WithServiceName(v string) Option {
|
||||
return func(o *Options) {
|
||||
o.ServiceName = v
|
||||
}
|
||||
}
|
||||
+96
-35
@@ -25,12 +25,19 @@ import (
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/exporters/jaeger"
|
||||
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
|
||||
"go.opentelemetry.io/otel/propagation"
|
||||
"go.opentelemetry.io/otel/sdk/resource"
|
||||
sdktrace "go.opentelemetry.io/otel/sdk/trace"
|
||||
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials"
|
||||
"google.golang.org/grpc/credentials/insecure"
|
||||
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
)
|
||||
|
||||
@@ -48,64 +55,78 @@ type revaDefaultTracerProvider struct {
|
||||
provider trace.TracerProvider
|
||||
}
|
||||
|
||||
type ctxKey struct{}
|
||||
// NewTracerProvider returns a new TracerProvider, configure for the specified service
|
||||
func NewTracerProvider(opts ...Option) trace.TracerProvider {
|
||||
options := Options{}
|
||||
|
||||
// ContextSetTracerProvider returns a copy of ctx with p associated.
|
||||
func ContextSetTracerProvider(ctx context.Context, p trace.TracerProvider) context.Context {
|
||||
if tp, ok := ctx.Value(ctxKey{}).(trace.TracerProvider); ok {
|
||||
if tp == p {
|
||||
return ctx
|
||||
}
|
||||
for _, o := range opts {
|
||||
o(&options)
|
||||
}
|
||||
|
||||
if options.TransportCredentials == nil {
|
||||
options.TransportCredentials = credentials.NewClientTLSFromCert(nil, "")
|
||||
}
|
||||
|
||||
if !options.Enabled {
|
||||
return trace.NewNoopTracerProvider()
|
||||
}
|
||||
|
||||
// default to 'reva' as service name if not set
|
||||
if options.ServiceName == "" {
|
||||
options.ServiceName = "reva"
|
||||
}
|
||||
|
||||
switch options.Exporter {
|
||||
case "otlp":
|
||||
return getOtlpTracerProvider(options)
|
||||
default:
|
||||
return getJaegerTracerProvider(options)
|
||||
}
|
||||
return context.WithValue(ctx, ctxKey{}, p)
|
||||
}
|
||||
|
||||
// ContextGetTracerProvider returns the TracerProvider associated with the ctx.
|
||||
// If no TracerProvider is associated is associated, the global default TracerProvider
|
||||
// is returned
|
||||
func ContextGetTracerProvider(ctx context.Context) trace.TracerProvider {
|
||||
if p, ok := ctx.Value(ctxKey{}).(trace.TracerProvider); ok {
|
||||
return p
|
||||
}
|
||||
return DefaultProvider()
|
||||
// SetDefaultTracerProvider sets the default trace provider
|
||||
func SetDefaultTracerProvider(tp trace.TracerProvider) {
|
||||
defaultProvider.mutex.Lock()
|
||||
defer defaultProvider.mutex.Unlock()
|
||||
defaultProvider.provider = tp
|
||||
defaultProvider.initialized = true
|
||||
}
|
||||
|
||||
// InitDefaultTracerProvider initializes a global default TracerProvider at a package level.
|
||||
func InitDefaultTracerProvider(collectorEndpoint string, agentEndpoint string) {
|
||||
// InitDefaultTracerProvider initializes a global default jaeger TracerProvider at a package level.
|
||||
//
|
||||
// Deprecated: Use NewTracerProvider and SetDefaultTracerProvider to properly initialize a tracer provider with options
|
||||
func InitDefaultTracerProvider(collector, endpoint string) {
|
||||
defaultProvider.mutex.Lock()
|
||||
defer defaultProvider.mutex.Unlock()
|
||||
if !defaultProvider.initialized {
|
||||
defaultProvider.provider = GetTracerProvider(true, collectorEndpoint, agentEndpoint, "reva default provider")
|
||||
defaultProvider.provider = getJaegerTracerProvider(Options{
|
||||
Enabled: true,
|
||||
Collector: collector,
|
||||
Endpoint: endpoint,
|
||||
ServiceName: "reva default jaeger provider",
|
||||
})
|
||||
}
|
||||
defaultProvider.initialized = true
|
||||
}
|
||||
|
||||
// DefaultProvider returns the "global" default TracerProvider
|
||||
// Currently used by the pool to get the global tracer
|
||||
func DefaultProvider() trace.TracerProvider {
|
||||
defaultProvider.mutex.RLock()
|
||||
defer defaultProvider.mutex.RUnlock()
|
||||
return defaultProvider.provider
|
||||
}
|
||||
|
||||
// GetTracerProvider returns a new TracerProvider, configure for the specified service
|
||||
func GetTracerProvider(enabled bool, collectorEndpoint string, agentEndpoint, serviceName string) trace.TracerProvider {
|
||||
if !enabled {
|
||||
return trace.NewNoopTracerProvider()
|
||||
}
|
||||
|
||||
// default to 'reva' as service name if not set
|
||||
if serviceName == "" {
|
||||
serviceName = "reva"
|
||||
}
|
||||
|
||||
// getJaegerTracerProvider returns a new TracerProvider, configure for the specified service
|
||||
func getJaegerTracerProvider(options Options) trace.TracerProvider {
|
||||
var exp *jaeger.Exporter
|
||||
var err error
|
||||
|
||||
if agentEndpoint != "" {
|
||||
if options.Endpoint != "" {
|
||||
var agentHost string
|
||||
var agentPort string
|
||||
|
||||
agentHost, agentPort, err = parseAgentConfig(agentEndpoint)
|
||||
agentHost, agentPort, err = parseAgentConfig(options.Endpoint)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@@ -121,8 +142,8 @@ func GetTracerProvider(enabled bool, collectorEndpoint string, agentEndpoint, se
|
||||
}
|
||||
}
|
||||
|
||||
if collectorEndpoint != "" {
|
||||
exp, err = jaeger.New(jaeger.WithCollectorEndpoint(jaeger.WithEndpoint(collectorEndpoint)))
|
||||
if options.Collector != "" {
|
||||
exp, err = jaeger.New(jaeger.WithCollectorEndpoint(jaeger.WithEndpoint(options.Collector)))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@@ -137,7 +158,7 @@ func GetTracerProvider(enabled bool, collectorEndpoint string, agentEndpoint, se
|
||||
sdktrace.WithBatcher(exp),
|
||||
sdktrace.WithResource(resource.NewWithAttributes(
|
||||
semconv.SchemaURL,
|
||||
semconv.ServiceNameKey.String(serviceName),
|
||||
semconv.ServiceNameKey.String(options.ServiceName),
|
||||
semconv.HostNameKey.String(hostname),
|
||||
)),
|
||||
)
|
||||
@@ -166,3 +187,43 @@ func parseAgentConfig(ae string) (string, string, error) {
|
||||
}
|
||||
return p[0], p[1], nil
|
||||
}
|
||||
|
||||
// getOtelTracerProvider returns a new TracerProvider, configure for the specified service
|
||||
func getOtlpTracerProvider(options Options) trace.TracerProvider {
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
||||
defer cancel()
|
||||
conn, err := grpc.DialContext(ctx, options.Endpoint,
|
||||
// Note the use of insecure transport here. TLS is recommended in production.
|
||||
grpc.WithTransportCredentials(insecure.NewCredentials()),
|
||||
grpc.WithBlock(),
|
||||
)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf("failed to create gRPC connection to collector: %w", err))
|
||||
}
|
||||
exporter, err := otlptracegrpc.New(
|
||||
context.Background(),
|
||||
otlptracegrpc.WithGRPCConn(conn),
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
resources, err := resource.New(
|
||||
context.Background(),
|
||||
resource.WithAttributes(
|
||||
attribute.String("service.name", options.ServiceName),
|
||||
attribute.String("library.language", "go"),
|
||||
),
|
||||
)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return sdktrace.NewTracerProvider(
|
||||
sdktrace.WithSampler(sdktrace.AlwaysSample()),
|
||||
sdktrace.WithBatcher(exporter),
|
||||
sdktrace.WithResource(resources),
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user