Skip to main content

Open Telemetry SDK

Prerequisites

Ensure you have the following:

  • Go installed on your system.
  • The Nebula OTel package added to your project.
  • Viper for configuration management.
  • Cobra for command-line argument parsing.

Configuration

This package uses Viper to read configurations from:

  1. Environment Variables
  2. Command-Line Flags
  3. YAML Configuration File

The default configuration values are defined within the InitializeFlags function.

YAML Configuration File Example

Create a YAML configuration file (e.g., config.yaml) with the following structure to customize OTel settings:

otel:
traces:
enabled: false # Enable or disable tracing
exporter: "otel" # Tracing exporter (e.g., "otel" or "stdout")
metrics:
enabled: false # Enable or disable metrics collection
exporter: "otel" # Metrics exporter (e.g., "otel" or "stdout")
logs:
enabled: false # Enable or disable logging
exporter: "otel" # Logging exporter (e.g., "otel" or "stdout")
service_name: "cli" # Name of the service
endpoint: "http://localhost:4318" # Endpoint for the OTel collector
ignore: [] # Routes to ignore in OTel propagation middleware

Environment Variables

Environment variables can override values in the YAML file:

  • OTEL_TRACES_ENABLED
  • OTEL_TRACES_EXPORTER
  • OTEL_METRICS_ENABLED
  • OTEL_METRICS_EXPORTER
  • OTEL_LOGS_ENABLED
  • OTEL_LOGS_EXPORTER
  • OTEL_SERVICE_NAME
  • OTEL_EXPORTER_OTLP_ENDPOINT
  • OTEL_MW_IGNORE

Command-Line Flags

These flags can be set to override YAML and environment settings:

  • --otel-traces
  • --otel-traces-exporter
  • --otel-metrics
  • --otel-metrics-exporter
  • --otel-logs
  • --otel-logs-exporter
  • --otel-service-name
  • --otel-endpoint
  • --otel-ignore

Initialization

Add the following code to initialize the OTel configuration with default settings.

// rootCmd represents the base command when called without any subcommands
var rootCmd = &cobra.Command{
Use: "root command",
PersistentPostRun: func(cmd *cobra.Command, args []string) {
// Flush OpenTelemetry data before exiting
otel.Flush(cmd.Context())
},

}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}

func init() {
initConfig()
otel.InitializeFlags(rootCmd)
cobra.OnInitialize(otel.InitOtel)

fmt.Println("service name: " + viper.GetString("otel.service_name") )
fmt.Println("otel traces enabled: " + viper.GetString("otel.traces.enabled") )
fmt.Println("otel metrics enabled: " + viper.GetString("otel.traces.enabled") )
fmt.Println("otel logs enabled: " + viper.GetString("otel.traces.enabled") )
fmt.Println("exporting otel data to collector: " + viper.GetString("otel.endpoint") )

}

InitializeFlags Function

The InitializeFlags function binds environment variables, command-line flags, and default values.

InitOtel Function

The InitOtel function sets up tracing, metrics, and logging providers. It uses the settings defined in the YAML file, environment variables, or command-line flags.