# opentelemetry_config.rb
require 'opentelemetry/sdk'
require 'opentelemetry/exporter/otlp'
require 'opentelemetry/instrumentation/all'

module OpenTelemetryConfig
  def self.configure

    # This is the url for the Opik API endpoint for Otel traces
    opik_endpoint = ENV.fetch('OPIK_OTEL_TRACES_ENDPOINT', 'https://www.comet.com/opik/api/v1/private/otel/v1/traces')
    # Here you can set the Opik API key, project name and workspace name
    # Set any default values but make sure to set them correctly in your environment variables
    opik_api_key = ENV.fetch('OPIK_API_KEY', '<any-api-key-value>')
    opik_project_name = ENV.fetch('OPIK_PROJECT_NAME', '<any-project-name-value>')
    opik_workspace_name = ENV.fetch('OPIK_COMET_WORKSPACE', '<default-workspace>')

    # This is very important, you need to set the headers for the Opik API calls to be able to authenticate against Opik
    otlp_headers = {
      'Authorization' => opik_api_key,
      'projectName' => opik_project_name,
      'Comet-Workspace' => opik_workspace_name
    }

    # Configure the exporter to use the Opik API endpoint and headers
    ENV['OTEL_EXPORTER_OTLP_PROTOCOL'] = 'http/protobuf' # Use the OTLP HTTP/Protobuf protocol
    ENV['OTEL_EXPORTER_OTLP_TRACES_ENDPOINT'] = opik_endpoint
    ENV['OTEL_EXPORTER_OTLP_TRACES_HEADERS'] = otlp_headers.map { |k, v| "#{k}=#{v}" }.join(',')

    OpenTelemetry::SDK.configure do |c|
      c.service_name = opik_project_name
      c.use_all # Enable automatic instrumentation for common libraries
    end
  end
end

OpenTelemetryConfig.configure
```

## Usage

Once configured, you can start using the OpenTelemetry SDK in your application. The following example demonstrates how to create a simple trace and span:

```ruby