Skip to content

Instantly share code, notes, and snippets.

@loftwah
Created May 7, 2024 03:43
Show Gist options
  • Save loftwah/61fdd42539dbb54b4542b49584cb4b0d to your computer and use it in GitHub Desktop.
Save loftwah/61fdd42539dbb54b4542b49584cb4b0d to your computer and use it in GitHub Desktop.
Open Telemetry Rails

Tola at Axiom support@axiom.co Tue, Apr 23, 7:31 PM to me

Hi Dean,

You can send your logs to Axiom using the Faraday HTTP Ruby library.

# app/services/axiom_logger.rb
require 'faraday'
require 'json'

class AxiomLogger
  def self.send_log(log_data)
    dataset_name = "$DATASET"
    axiom_ingest_api_url = "https://api.axiom.co/v1/datasets/#{dataset_name}/ingest"
    ingest_token = "$API_TOKEN"

    conn = Faraday.new(url: axiom_ingest_api_url) do |faraday|
      faraday.request :url_encoded
      faraday.adapter Faraday.default_adapter
    end

    # Wrap the log_data in an array
    wrapped_log_data = [log_data]

    response = conn.post do |req|
      req.headers['Content-Type'] = 'application/json'
      req.headers['Authorization'] = "Bearer #{ingest_token}"
      req.body = wrapped_log_data.to_json
    end

    puts "AxiomLogger Response status: #{response.status}, body: #{response.body}"

    if response.status != 200
      Rails.logger.error "Failed to send log to Axiom: #{response.body}"
    end
  end
end

You also have to install this in your Gemfile:

gem 'faraday'
gem 'dotenv-rails', groups: [:development, :test]

Here is a sample code configuration example to test it out. We are working on the documentation and guide for this, which will be ready this week.

You can also send traces from your Ruby application to Axiom using:

Your Gemfile should also include this:

gem 'opentelemetry-api'
gem 'opentelemetry-sdk'
gem 'opentelemetry-exporter-otlp'
gem 'opentelemetry-instrumentation-rails'
gem 'opentelemetry-instrumentation-http'
gem 'opentelemetry-instrumentation-active_record', require: false
gem 'opentelemetry-instrumentation-all'

In your initializers directory in your ruby app, create your

opentelemetry.rb

file and include:

require 'opentelemetry/sdk'
require 'opentelemetry/exporter/otlp'
require 'opentelemetry/instrumentation/all'

OpenTelemetry::SDK.configure do |c|
  c.service_name = 'ruby-traces' # Set your service name

  c.use_all # or specify individual instrumentation you need

  c.add_span_processor(
    OpenTelemetry::SDK::Trace::Export::BatchSpanProcessor.new(
      OpenTelemetry::Exporter::OTLP::Exporter.new(
        endpoint: 'https://api.axiom.co/v1/traces',
        headers: {
          'Authorization' => 'Bearer $API_TOKEN',
          'X-AXIOM-DATASET' => '$DATASET'
        }
      )
    )
  )
end

Your API token should have both ingesting and querying permissions

Replace $DATASET with your dataset name.

You will start receiving traces from your application in your dataset.

We are currently working on extensive guides for this, which will be merged this week to production.

Let me know if you have any questions.

Thanks,

Tola

Support Team

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment