github.com/thanos-io/thanos@v0.32.5/pkg/tracing/google_cloud/google_cloud.go (about)

     1  // Copyright (c) The Thanos Authors.
     2  // Licensed under the Apache License 2.0.
     3  
     4  package google_cloud
     5  
     6  import (
     7  	"context"
     8  	"os"
     9  
    10  	"github.com/prometheus/common/version"
    11  	"github.com/thanos-io/thanos/pkg/tracing/migration"
    12  
    13  	cloudtrace "github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/trace"
    14  	"github.com/go-kit/log"
    15  	"github.com/go-kit/log/level"
    16  	"go.opentelemetry.io/otel/attribute"
    17  	"go.opentelemetry.io/otel/sdk/resource"
    18  	tracesdk "go.opentelemetry.io/otel/sdk/trace"
    19  	semconv "go.opentelemetry.io/otel/semconv/v1.10.0"
    20  	"gopkg.in/yaml.v2"
    21  )
    22  
    23  // Config - YAML configuration.
    24  type Config struct {
    25  	ServiceName  string `yaml:"service_name"`
    26  	ProjectId    string `yaml:"project_id"`
    27  	SampleFactor uint64 `yaml:"sample_factor"`
    28  }
    29  
    30  // NewTracerProvider create tracer provider from YAML.
    31  func NewTracerProvider(ctx context.Context, logger log.Logger, conf []byte) (*tracesdk.TracerProvider, error) {
    32  	config := Config{}
    33  	if err := yaml.Unmarshal(conf, &config); err != nil {
    34  		return nil, err
    35  	}
    36  
    37  	exporter, err := cloudtrace.New(
    38  		cloudtrace.WithContext(ctx),
    39  		cloudtrace.WithProjectID(config.ProjectId),
    40  	)
    41  	if err != nil {
    42  		return nil, err
    43  	}
    44  
    45  	return newTracerProvider(ctx, logger, tracesdk.NewBatchSpanProcessor(exporter),
    46  		config.SampleFactor, config.ServiceName), nil
    47  }
    48  
    49  func newTracerProvider(ctx context.Context, logger log.Logger, processor tracesdk.SpanProcessor, sampleFactor uint64, serviceName string) *tracesdk.TracerProvider {
    50  	// Even if resource.New returns error, the resource will be valid - log the error and continue.
    51  	resource, err := resource.New(ctx, resource.WithAttributes(collectAttributes(serviceName)...))
    52  	if err != nil {
    53  		level.Warn(logger).Log("msg", "detecting resources for tracing provider failed", "err", err)
    54  	}
    55  
    56  	var fraction float64
    57  	if sampleFactor == 0 {
    58  		fraction = 0
    59  	} else {
    60  		fraction = 1 / float64(sampleFactor)
    61  	}
    62  
    63  	tp := tracesdk.NewTracerProvider(
    64  		tracesdk.WithSpanProcessor(processor),
    65  		tracesdk.WithSampler(
    66  			migration.SamplerWithOverride(
    67  				tracesdk.ParentBased(tracesdk.TraceIDRatioBased(fraction)),
    68  				migration.ForceTracingAttributeKey,
    69  			),
    70  		),
    71  		tracesdk.WithResource(resource),
    72  	)
    73  
    74  	return tp
    75  }
    76  
    77  func collectAttributes(serviceName string) []attribute.KeyValue {
    78  	attr := []attribute.KeyValue{
    79  		semconv.ServiceNameKey.String(serviceName),
    80  		attribute.String("binary_revision", version.Revision),
    81  	}
    82  
    83  	if len(os.Args) > 1 {
    84  		attr = append(attr, attribute.String("binary_cmd", os.Args[1]))
    85  	}
    86  
    87  	return attr
    88  }