otel

package module
v0.0.0-...-3bd2c62 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2023 License: Apache-2.0 Imports: 15 Imported by: 0

README

go-starter-otel

go-starter-otel is a Go library that simplifies the configuration of OpenTelemetry by relying on environment variables. It draws inspiration from the OpenTelemetry Autoconfigure Java SDK and allows users to configure various aspects of OpenTelemetry instrumentation seamlessly using environment variables.

Overview

OpenTelemetry is an observability framework that provides APIs, libraries, agents, and instrumentation to provide observability for applications. go-starter-otel aims to streamline the setup process by allowing users to configure OpenTelemetry using environment variables. This is particularly useful for projects where configuration through code or configuration files may be cumbersome or less flexible.

Features

  • Environment Variable Based Configuration: Configure OpenTelemetry settings using environment variables, following the conventions defined in the OpenTelemetry Autoconfigure Java SDK.

  • Seamless Integration: Easily integrate OpenTelemetry into your Go applications without the need for extensive code modifications.

Getting Started

Prerequisites

Make sure you have Go installed on your system.

Installation

To install go-starter-otel, use the following Go module command:

go get github.com/angelokurtis/go-starter-otel
Usage
  1. Import the go-starter-otel package into your Go code:

    import "github.com/angelokurtis/go-starter-otel"
    
  2. Initialize OpenTelemetry with environment variable-based configuration:

    package main
    
    import (
       "context"
       "log"
       "net/http"
    
       otel "github.com/angelokurtis/go-starter-otel"
    )
    
    func main() {
       // Initialize OpenTelemetry with environment variables
       _, shutdown, err := otel.SetupProviders(context.Background())
       if err != nil {
           log.Fatalf("Error initializing OpenTelemetry: %v", err)
       }
       defer shutdown()
    
       // Example HTTP server
       http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
           ctx := r.Context()
           ctx, span := otel.StartSpanFromContext(ctx)
           defer span.End()
    
           // Your application code here
    
           w.Write([]byte("Hello, World!"))
       })
    
       log.Fatal(http.ListenAndServe(":8080", nil))
    }
    
  3. Set the required environment variables based on the configuration options described in the OpenTelemetry Autoconfigure Java SDK README.

  4. Run your Go application, and OpenTelemetry will be configured based on the provided environment variables.

Configuration Options

The configuration options for go-starter-otel are aligned with the environment variables specified in the OpenTelemetry Autoconfigure Java SDK README. Refer to that documentation for a comprehensive list of available options.

Contributing

Contributions are welcome! Feel free to open issues or pull requests on the GitHub repository.

License

This project is licensed under the Apache License 2.0 - see the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func StartSpanFromContext

func StartSpanFromContext(ctx context.Context) (context.Context, trace.Span)

StartSpanFromContext starts a new OpenTelemetry span using the provided context. It returns a new context containing the started span and the started span itself.

Example:

ctx, span := otel.StartSpanFromContext(ctx)
defer span.End()

func WrapError

func WrapError(ctx context.Context, err error) error

WrapError wraps the given error and records it as an error in the current OpenTelemetry span. If the provided error is nil, it returns nil.

func WrapErrorWithStack

func WrapErrorWithStack(ctx context.Context, err error) error

WrapErrorWithStack wraps the given error, records it as an error in the current OpenTelemetry span, and includes the stack trace information in the recorded error. If the provided error is nil, it returns nil.

Types

type Manager

type Manager struct {
	TracerProvider *sdktrace.TracerProvider
	MeterProvider  *sdkmetric.MeterProvider
}

func NewManager

func NewManager(ctx context.Context) (*Manager, func(), error)

type Providers

type Providers struct {
	TracerProvider *sdktrace.TracerProvider
}

Providers struct holds the TracerProvider for OpenTelemetry.

func SetupProviders

func SetupProviders(ctx context.Context) (*Providers, func(), error)

SetupProviders initializes and configures OpenTelemetry providers. It returns a Providers struct containing the TracerProvider, a cleanup function, and an error if setup fails.

Directories

Path Synopsis
_examples
internal
env

Jump to

Keyboard shortcuts

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