Overview

Integration with Opik

As of today, Opik does not provide a Ruby SDK. However, you can use the Opik OpenTelemetry integration to send telemetry data to Opik.

Opik Configuration

You can configure your application to use OpenTelemetry SDK by setting up the following dependencies:

1# gemfile
2# ... other gems
3
4gem 'opentelemetry-sdk'
5gem 'opentelemetry-exporter-otlp'
6gem 'opentelemetry-instrumentation-all'

Next, you need to set up the OpenTelemetry SDK and configure it to use the Opik as the traces backend.

1# opentelemetry_config.rb
2require 'opentelemetry/sdk'
3require 'opentelemetry/exporter/otlp'
4require 'opentelemetry/instrumentation/all'
5
6module OpenTelemetryConfig
7 def self.configure
8
9 # This is the url for the Opik API endpoint for Otel traces
10 opik_endpoint = ENV.fetch('OPIK_OTEL_TRACES_ENDPOINT', 'https://www.comet.com/opik/api/v1/private/otel/v1/traces')
11 # Here you can set the Opik API key, project name and workspace name
12 # Set any default values but make sure to set them correctly in your environment variables
13 opik_api_key = ENV.fetch('OPIK_API_KEY', '<any-api-key-value>')
14 opik_project_name = ENV.fetch('OPIK_PROJECT_NAME', '<any-project-name-value>')
15 opik_workspace_name = ENV.fetch('OPIK_COMET_WORKSPACE', '<default-workspace>')
16
17 # This is very important, you need to set the headers for the Opik API calls to be able to authenticate against Opik
18 otlp_headers = {
19 'Authorization' => opik_api_key,
20 'projectName' => opik_project_name,
21 'Comet-Workspace' => opik_workspace_name
22 }
23
24 # Configure the exporter to use the Opik API endpoint and headers
25 ENV['OTEL_EXPORTER_OTLP_PROTOCOL'] = 'http/protobuf' # Use the OTLP HTTP/Protobuf protocol
26 ENV['OTEL_EXPORTER_OTLP_TRACES_ENDPOINT'] = opik_endpoint
27 ENV['OTEL_EXPORTER_OTLP_TRACES_HEADERS'] = otlp_headers.map { |k, v| "#{k}=#{v}" }.join(',')
28
29 OpenTelemetry::SDK.configure do |c|
30 c.service_name = opik_project_name
31 c.use_all # Enable automatic instrumentation for common libraries
32 end
33 end
34end
35
36OpenTelemetryConfig.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:

1# app.rb
2require_relative './opentelemetry_config'
3
4require 'net/http'
5require 'uri'
6require 'json'
7
8TRACER = OpenTelemetry.tracer_provider.tracer('opik-tracer', '1.0.0')
9
10uri = URI("https://jsonplaceholder.typicode.com/posts/1")
11
12# Create a trace and spans via OpenTelemetry instrumentation
13TRACER.in_span('JSONPlaceholder API Call', kind: :client, attributes: {
14 'http.url' => uri.to_s,
15 'http.method' => 'GET',
16}) do |span|
17 # Your application logic ...
18
19 # For example, making an HTTP request to JSONPlaceholder
20 http = Net::HTTP.new(uri.host, uri.port)
21 http.use_ssl = true
22
23 request = Net::HTTP::Get.new(uri)
24 request["Content-Type"] = "application/json"
25
26 response = http.request(request)
27 span.set_attribute('http.status_code', response.code.to_i)
28
29 puts "Response from JSONPlaceholder: #{response.body}"
30
31 span.set_attribute('output', response.body)
32end

It’s important to note that OpenTelemetry (otel) auto-instrumentations don’t automatically align with Opik’s schema, so you may need to manually set certain attributes, like the output attribute in the example above.

Opik supports mapping of attributes to Opik traces, for the following libraries:

  • python/OpenInference
  • python/Pydantic
  • python/GenAI

In the future we may add more integrations to Opik. If you have any suggestions on what else could be integrated into Opik, please let us know!

If your application is a short-lived process, you may need to call OpenTelemetry.tracer_provider.force_flush(timeout: <timeout>) or similar method at the end of your application to ensure all telemetry data is flushed before exiting.

Conclusion

This guide provided an overview of how to integrate Opik with the OpenTelemetry SDK in Ruby. By following the steps outlined above, you can easily set up your application to send telemetry data to Opik for monitoring and analysis.

Feel free to reach out to us if you have any questions or issues with the integration, via the GitHub repository, or join our Slack channel.