gcptrace

package module
v0.0.0-...-c8edbca Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 23, 2024 License: MIT Imports: 12 Imported by: 0

README

GCP Trace package

PkgGoDev

This package contains shared initialization code that exports collected spans via otlp exporter.

go get "github.com/MottoStreaming/gcptrace.go"

Initialization example:

Deployment example:

env:
  - name: POD_NAME
    valueFrom:
      fieldRef:
        fieldPath: metadata.name
  - name: NAMESPACE_NAME
    valueFrom:
      fieldRef:
        fieldPath: metadata.namespace
  - name: CONTAINER_NAME
    value: my-container-name
  - name: OTEL_RESOURCE_ATTRIBUTES
    value: k8s.pod.name=$(POD_NAME),k8s.namespace.name=$(NAMESPACE_NAME),k8s.container.name=$(CONTAINER_NAME),SampleRate=10
  - name: OTEL_SERVICE_NAME
    value: my-service-name
  - name: OTEL_SERVICE_VERSION
    value: xxyyzz
  - name: OTEL_TRACES_SAMPLER
    value: parentbased_traceidratio
  - name: OTEL_TRACES_SAMPLER_ARG
    value: 0.1 # value must match with SampleRate attribute

main.go:

func main() {
    ctx := context.Background()
    shutdown, err := gcptrace.InitTracing(ctx)
    if err != nil {
        log.Fatalf("unable to set up tracing: %v", err)
    }
    defer shutdown()
}

Tracing your own code (storage layer in this example):

import (
    "go.opentelemetry.io/otel"
    semconv "go.opentelemetry.io/otel/semconv/v1.21.0"
    "go.opentelemetry.io/otel/trace"
)

// Inside constructor initialize separate instance of tracer 
tracer:  otel.Tracer("")

func (s *storage) startSpan(ctx context.Context, name string) (context.Context, trace.Span) {
    spanCtx, span := s.tracer.Start(ctx,
        name,
        trace.WithSpanKind(trace.SpanKindInternal),
        trace.WithAttributes(
            semconv.DBSystemPostgreSQL,
        ),
    )
    return spanCtx, span
}

func (s *storage) GetDRMTechnologies(ctx context.Context, streamID string) ([]*bff_v1.Event, error) {
    spanCtx, span := s.startSpan(ctx, "GetDRMTechnologies")
    defer span.End()
    span.SetAttributes(attribute.String("stream_id", streamID))
    ...
    if err != nil {
        // Record error in the span
        span.RecordError(err)
        return nil, errors.Wrap(err, "failed to read playlist DRM info")
    }
}

Documentation

Overview

Example (InitTracer)
ctx := context.Background()
// For Kubernetes container you need to set some environment variables inside pod spec
// to get correct pod/container and namespace name:
// https://github.com/open-telemetry/opentelemetry-go-contrib/tree/main/detectors/gcp#setting-kubernetes-attributes
// We also set some other environment variables to configure exporter and sampler:
// env:
// - name: POD_NAME
//   valueFrom:
//     fieldRef:
//       fieldPath: metadata.name
// - name: NAMESPACE_NAME
//   valueFrom:
//     fieldRef:
//       fieldPath: metadata.namespace
// - name: CONTAINER_NAME
//   value: my-container-name
// - name: OTEL_RESOURCE_ATTRIBUTES
//   value: k8s.pod.name=$(POD_NAME),k8s.namespace.name=$(NAMESPACE_NAME),k8s.container.name=$(CONTAINER_NAME),SampleRate=10
// - name: OTEL_SERVICE_NAME
//   value: my-service-name
// - name: OTEL_SERVICE_VERSION
//   value: "xxyyzz"
// - name: OTEL_TRACES_SAMPLER
//   value: parentbased_traceidratio
// - name: OTEL_TRACES_SAMPLER_ARG
//   value: 0.1 # value must match with SampleRate attribute
shutdown, err := gcptrace.InitTracing(ctx)
if err != nil {
	log.Fatalf("unable to set up tracing: %v", err)
}
defer shutdown()
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func InitTracing

func InitTracing(ctx context.Context, tpOptions ...trace.TracerProviderOption) (func(), error)

Types

type SampleRateAnnotator

type SampleRateAnnotator struct{}

SampleRateAnnotator is a SpanProcessor that adds baggage SampleRate as attribute to all started spans.

func (SampleRateAnnotator) ForceFlush

func (a SampleRateAnnotator) ForceFlush(context.Context) error

func (SampleRateAnnotator) OnEnd

func (SampleRateAnnotator) OnStart

func (SampleRateAnnotator) Shutdown

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL