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