Get a DemoStart Free TrialSign In

OpenTelemetry Go Configuration

Ship traces from Go to Opensearch with OpenTelemetry (via https)

Use OpenTelemetry to easily send Go traces to your Logit.io Stack.

Send Your DataAPMLanguages & LibrariesOpenTelemetry Go Configuration Guide

Follow this step by step guide to get 'traces' from your system to Logit.io:

Step 1 - Getting Started

This guide uses the OpenTelemetry Go collector example application as its starting point with some modifications to allow the traces to be sent to your Logit.io Stack.

This code is added to main.go and when run will send the example traces to your Logit.io Stack to be viewed in Jaeger

// Copyright The OpenTelemetry Authors
//
// 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.

// Example using OTLP exporters + collector + third-party backends. For
// information about using the exporter, see:
// https://pkg.go.dev/go.opentelemetry.io/otel/exporters/otlp?tab=doc#example-package-Insecure
  package main

  import (
    "context"
    "encoding/base64"
    "fmt"
    "log"
    "os"
    "os/signal"
    "time"

    "go.opentelemetry.io/otel"
    "go.opentelemetry.io/otel/attribute"
    "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptrace<your-opentelemetry-protocol>"
    "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.21.0"
    "go.opentelemetry.io/otel/trace"
  )

  // Initializes an OTLP exporter, and configures the corresponding trace and
  // metric providers.
  func initProvider() (func(context.Context) error, error) {
    ctx := context.Background()

    res, err := resource.New(ctx,
      resource.WithAttributes(
        // the service name used to display traces in backends
        semconv.ServiceName("test-service"),
      ),
    )
    if err != nil {
      return nil, fmt.Errorf("failed to create resource: %w", err)
    }

    ctx, cancel := context.WithTimeout(ctx, time.Second)
    defer cancel()

    creds := &basicAuthCreds{
      username: "<your-opentelemetry-username>",
      password: "<your-opentelemetry-password>",
    }

    headers, err := creds.GetRequestMetadata(ctx)
    if err != nil {
      log.Fatalf("failed to get request metadata: %v", err)
    }
    
    // Set up a trace exporter
    traceExporter, err := otlptrace<your-opentelemetry-protocol>.New(ctx, otlptrace<your-opentelemetry-protocol>.WithEndpoint("<your-opentelemetry-endpoint-address>:<your-opentelemetry-endpoint-port>"),
      otlptrace<your-opentelemetry-protocol>.WithHeaders(headers))
    if err != nil {
      return nil, fmt.Errorf("failed to create trace exporter: %w", err)
    }

    // Register the trace exporter with a TracerProvider, using a batch
    // span processor to aggregate spans before export.
    bsp := sdktrace.NewBatchSpanProcessor(traceExporter)
    tracerProvider := sdktrace.NewTracerProvider(
      sdktrace.WithSampler(sdktrace.AlwaysSample()),
      sdktrace.WithResource(res),
      sdktrace.WithSpanProcessor(bsp),
    )
    otel.SetTracerProvider(tracerProvider)

    // set global propagator to tracecontext (the default is no-op).
    otel.SetTextMapPropagator(propagation.TraceContext{})

    // Shutdown will flush any remaining spans and shut down the exporter.
    return tracerProvider.Shutdown, nil
  }

  func main() {
    log.Printf("Waiting for connection...")

    ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
    defer cancel()

    shutdown, err := initProvider()
    if err != nil {
      log.Fatal(err)
    }
    defer func() {
      if err := shutdown(ctx); err != nil {
        log.Fatal("failed to shutdown TracerProvider: %w", err)
      }
    }()

    tracer := otel.Tracer("test-tracer")

    // Attributes represent additional key-value descriptors that can be bound
    // to a metric observer or recorder.
    commonAttrs := []attribute.KeyValue{
      attribute.String("attrA", "chocolate"),
      attribute.String("attrB", "raspberry"),
      attribute.String("attrC", "vanilla"),
    }

    // work begins
    ctx, span := tracer.Start(
      ctx,
      "CollectorExporter-Example",
      trace.WithAttributes(commonAttrs...))
    defer span.End()
    for i := 0; i < 10; i++ {
      _, iSpan := tracer.Start(ctx, fmt.Sprintf("Sample-%d", i))
      log.Printf("Doing really hard work (%d / 10)\n", i+1)

      <-time.After(time.Second)
      iSpan.End()
    }

    log.Printf("Done!")
  }

  type basicAuthCreds struct {
    username, password string
  }

  func (c *basicAuthCreds) GetRequestMetadata(context.Context, ...string) (map[string]string, error) {
    return map[string]string{
      "authorization": "Basic " + base64.StdEncoding.EncodeToString([]byte(c.username+":"+c.password)),
    }, nil
  }

Step 2 - Check Logit.io for your traces

Data should now have been sent to your Stack.

View my data

If you don't see traces please take a look at How to diagnose no data in Stack below for how to diagnose common issues.

Step 3 - how to diagnose no data in Stack

If you don't see data appearing in your Stack after following the steps, visit the Help Centre guide for steps to diagnose no data appearing in your Stack or Chat to support now.

Return to Search
Sign Up

© 2024 Logit.io Ltd, All rights reserved.