Skip to content

Commit dfaacee

Browse files
committed
editing ruby logs
1 parent 4a7529f commit dfaacee

File tree

1 file changed

+42
-59
lines changed

1 file changed

+42
-59
lines changed

docs/shipping/Code/ruby.md

Lines changed: 42 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -27,124 +27,107 @@ Ruby OpenTelemetry logging support is experimental. This guide provides the most
2727

2828
* Ruby 3.1 or later
2929
* Bundler for dependency management
30-
* A Logz.io account with a shipping token
30+
* A Logz.io account with a log shipping token
3131

3232

3333
### Install dependencies (gems)
3434

35+
_If your Gemfile already contains `opentelemetry-exporter-otlp` (for tracing or metrics), leave that gem in your Gemfile and run `bundle update opentelemetry-exporter-otlp` to upgrade it._
36+
37+
Run a single `bundle add` command from your project root:
38+
3539
```bash
3640
bundle add opentelemetry-sdk \
37-
opentelemetry-exporter-otlp \
3841
opentelemetry-logs-api \
3942
opentelemetry-logs-sdk \
4043
opentelemetry-exporter-otlp-logs
4144
```
42-
4345
The `opentelemetry-exporter-otlp-logs` gem provides experimental OTLP-over-HTTP exporter for logs.
4446

47+
Finish the install by running:
48+
49+
```bash
50+
bundle install
51+
```
52+
4553
### Set OpenTelemetry environment variables
4654

4755
```bash
48-
export OTEL_LOGS_EXPORTER=otlp
49-
export OTEL_EXPORTER_OTLP_LOGS_PROTOCOL=http/protobuf
50-
export OTEL_EXPORTER_OTLP_LOGS_ENDPOINT=https://otlp-listener.logz.io/v1/logs
51-
export OTEL_EXPORTER_OTLP_LOGS_HEADERS="Authorization=Bearer <LOG-SHIPPING-TOKEN>,user-agent=logzio-ruby-logs-otlp"
52-
export OTEL_RESOURCE_ATTRIBUTES="service.name=<YOUR-SERVICE-NAME>"
56+
export OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
57+
export OTEL_EXPORTER_OTLP_ENDPOINT=https://otlp-listener.logz.io/v1/logs
58+
export OTEL_EXPORTER_OTLP_HEADERS="Authorization=Bearer <LOG-SHIPPING-TOKEN>,user-agent=logzio-ruby-logs-otlp"
59+
export OTEL_SERVICE_NAME=<YOUR-SERVICE-NAME>
5360
```
5461

5562
Replace `<LOG-SHIPPING-TOKEN>` with your Logz.io shipping token and `<YOUR-SERVICE-NAME>` with your service name.
5663

57-
### Initialize OpenTelemetry in Rails
64+
If the application already exports traces or metrics with a different OTLP endpoint, ensure that endpoint ends with `/v1/logs` so logs are routed correctly.
65+
66+
### Instrument the application
5867

59-
Add this configuration to your Ruby application (at the top of your main file):
68+
Add the block below at the top of your main script. For **Rails**, create `config/initializers/opentelemetry_logs.rb` with the code.
6069

6170
```ruby
6271
require 'opentelemetry/sdk'
63-
require 'opentelemetry/exporter/otlp/logs'
64-
require 'opentelemetry/logs/sdk'
72+
require 'opentelemetry-logs-sdk'
73+
require 'opentelemetry/exporter/otlp_logs'
6574

6675
OpenTelemetry::SDK.configure do |c|
6776
c.service_name = ENV['OTEL_SERVICE_NAME'] || 'ruby-service'
68-
69-
# Configure logs
77+
7078
c.logger_provider = OpenTelemetry::SDK::Logs::LoggerProvider.new.tap do |provider|
71-
exporter = OpenTelemetry::Exporter::OTLP::Logs::Exporter.new(
72-
endpoint: ENV['OTEL_EXPORTER_OTLP_LOGS_ENDPOINT'],
73-
headers: Hash[ENV['OTEL_EXPORTER_OTLP_LOGS_HEADERS'].split(',').map { |h| h.split('=', 2) }]
79+
exporter = OpenTelemetry::Exporter::OTLP::Logs::LogsExporter.new(
80+
endpoint: ENV['OTEL_EXPORTER_OTLP_ENDPOINT'],
81+
headers: Hash[ENV.fetch('OTEL_EXPORTER_OTLP_HEADERS', '').split(',').map { |h| h.split('=', 2) }]
7482
)
75-
7683
processor = OpenTelemetry::SDK::Logs::Export::BatchLogRecordProcessor.new(exporter)
7784
provider.add_log_record_processor(processor)
7885
end
7986
end
8087
```
8188

82-
For Rails specifically: Create `config/initializers/opentelemetry_logs.rb` with the above code.
83-
84-
85-
### Emit logs
89+
### Emit log records
8690

87-
Anywhere in your app:
91+
Anywhere in the codebase you can obtain a logger and emit log entries:
8892

8993
```ruby
90-
# Get the logger
91-
logger = OpenTelemetry.logger_provider.logger('your-app-logger')
94+
logger = OpenTelemetry.logger_provider.logger('app-logger')
9295

93-
# Emit different log levels
9496
logger.on_emit(
9597
body: 'User login successful',
9698
severity_text: 'INFO',
97-
attributes: {
98-
'user.id' => '123',
99-
'action' => 'login'
100-
}
99+
attributes: { 'user.id' => '123', 'action' => 'login' }
101100
)
102101

103102
logger.on_emit(
104103
body: 'Database connection failed',
105104
severity_text: 'ERROR',
106-
attributes: {
107-
'error.type' => 'ConnectionError',
108-
'database' => 'users_db'
109-
}
105+
attributes: { 'error.type' => 'ConnectionError', 'database' => 'users_db' }
110106
)
111107
```
112108

113-
### Rails integration example
109+
### Run and verify
114110

115-
Standalone Ruby script:
111+
Run the application as usual, for example:
116112

117-
```ruby
118-
#!/usr/bin/env ruby
119-
require_relative 'opentelemetry_config'
120-
121-
logger = OpenTelemetry.logger_provider.logger('my-script')
113+
```bash
114+
bundle exec ruby <<your_app.rb>>
115+
```
122116
123-
logger.on_emit(
124-
body: 'Script started',
125-
severity_text: 'INFO',
126-
attributes: { 'version' => '1.0.0' }
127-
)
117+
Generate activity that triggers log statements.
128118
129-
# Your application logic here
130-
puts "Processing data..."
119+
Open Explore in the Logz.io UI, filter by the service name you set, and confirm that the new log entries appear after a short ingestion delay.
131120
132-
logger.on_emit(
133-
body: 'Processing complete',
134-
severity_text: 'INFO',
135-
attributes: { 'records_processed' => '1000' }
136-
)
137-
```
121+
### Troubleshooting
138122
139-
### Run the application
123+
* A `Bundler::GemRequireError` about duplicate gems means `opentelemetry-exporter-otlp` appears twice in the Gemfile with different version constraints. Keep one line and run `bundle install` again.
140124
141-
`bundle exec ruby your_app.rb`
125+
* A `LoadError` for `opentelemetry/logs/sdk` indicates the require path is incorrect. Use `require 'opentelemetry-logs-sdk'`.
142126
143-
Generate some traffic; the logs will stream to Logz.io in OTLP format.
127+
* An `OpenTelemetry::SDK::ConfigurationError` mentioning an invalid URL usually means `OTEL_EXPORTER_OTLP_ENDPOINT` is unset or malformed. Confirm the full HTTPS URL ends with `/v1/logs`.
144128
145-
### Verify in Logz.io
129+
Once the endpoint and headers are valid, logs flow automatically through the exporter and become searchable in your Logz.io account.
146130
147-
Open Explore, select the service name you set, and confirm the new log entries are present. Allow a few minutea for ingestion.
148131
149132
## Traces
150133

0 commit comments

Comments
 (0)