Integrations
Golang

InfraStack Golang Integration Steps

InfraStack AI provides a comprehensive toolkit for collecting telemetry data within your Golang applications, leveraging OpenTelemetry. This documentation will guide you through the setup process to integrate InfraStack with your Golang applications.

Step 1: Install OpenTelemetry SDKs

First, install the required OpenTelemetry packages for Golang:

go get go.opentelemetry.io/otel
go get go.opentelemetry.io/otel/sdk
go get go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp
go get go.opentelemetry.io/otel/semconv/v1.17.0

Step 2: Configure Environment Variables

Set the necessary environment variables for InfraStack and OpenTelemetry configuration. These variables help secure and authenticate the data transmission to InfraStack. Replace <YOUR_API_KEY> with your actual API key.

Step 3: Initialize OpenTelemetry in Your Application

Create a new file main.go and initialize the OpenTelemetry SDK. You can configure the endpoint and API key either by using environment variables or by hardcoding them within your application.

Import Required Packages

import (
    "context"
    "go.opentelemetry.io/otel"
    "go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
    "go.opentelemetry.io/otel/sdk/resource"
    "go.opentelemetry.io/otel/sdk/trace"
    "go.opentelemetry.io/otel/semconv/v1.4.0"
    "os"
    "strings"
)

Initialize the Exporter and Tracer Provider

func main() {
    ctx := context.Background()
 
    // Configure exporter using environment variables
    endpoint := os.Getenv("OTEL_EXPORTER_OTLP_ENDPOINT")
    headers := os.Getenv("OTEL_EXPORTER_OTLP_HEADERS")
    serviceName := os.Getenv("OTEL_SERVICE_NAME")
 
    // Split the headers string into key and value
    headerParts := strings.SplitN(headers, ":", 2)
    headerKey := headerParts[0]
    headerValue := headerParts[1]
 
    // Create OTLP exporter using environment variables
    exporter, _ := otlptracehttp.New(ctx, otlptracehttp.WithEndpoint(endpoint), otlptracehttp.WithHeaders(map[string]string{headerKey: headerValue}))
 
    // Create resource
    res := resource.NewWithAttributes(
        semconv.SchemaURL,
        semconv.ServiceNameKey.String(serviceName),
        semconv.ServiceVersion("v0.0.1"),
    )
 
    // Create trace provider
    tp := trace.NewTracerProvider(
        trace.WithBatcher(exporter),
        trace.WithResource(res),
    )
    defer tp.Shutdown(ctx)
 
    // Set global trace provider
    otel.SetTracerProvider(tp)
 
    // Your application code goes here
}

Step 4: Run Your Application

Build and run your Golang application:

go run main.go

Step 5: Check Your Application Trace Data

You can monitor and analyze the telemetry data collected by visiting the InfraStack Dashboard (opens in a new tab), providing a comprehensive view of your application's performance and health.

Example Trace

To generate and view an example trace, you can instrument specific parts of your application code. Here is how you can do it:

import (
    "context"
    "fmt"
    "go.opentelemetry.io/otel"
    "go.opentelemetry.io/otel/trace"
    "time"
)
 
func main() {
    // Initialize OpenTelemetry as shown in Step 3...
 
    // Create a tracer
    tracer := otel.Tracer("example-tracer")
 
    // Start a span
    ctx, span := tracer.Start(context.Background(), "example-span")
    defer span.End()
 
    // Simulate work
    fmt.Println("Doing some work...")
    time.Sleep(2 * time.Second)
 
    // End the span
    span.End()
 
    // Continue with the rest of your application code...
}

This example creates a trace with a single span named "example-span". You can add more spans around different parts of your application to trace various operations.

Additional Instrumentation Options

OpenTelemetry provides specialized instrumentation options for various libraries. For example:

  • HTTP requests: To monitor client and server HTTP requests, use go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp.
  • SQL database queries: To monitor interactions with SQL databases, use go.opentelemetry.io/contrib/instrumentation/database/sql/otel.
  • gRPC calls: To track gRPC requests, use go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc.

[!NOTE] You can find all the information about OpenTelemetry and SDK (opens in a new tab).

These instrumentations allow you to collect detailed data from different components of your application, which is critical for performance analysis and troubleshooting.


By following these steps, you can successfully integrate InfraStack AI with your Golang application, enabling powerful observability and monitoring capabilities with minimal setup.