Skip to content

Commit

Permalink
chore: Add examples for logs (#1774)
Browse files Browse the repository at this point in the history
* Example with the SDK, demonstrating the env var w/console export
* Example without the SDK using OTLP
* Example without the SDK using the console
  • Loading branch information
kaylareopelle authored Dec 13, 2024
1 parent bb51595 commit 035c32a
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
31 changes: 31 additions & 0 deletions examples/logs_sdk/logs_console.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

require 'bundler/inline'

gemfile(true) do
source 'https://rubygems.org'

gem 'opentelemetry-logs-api', path: '../../logs_api'
gem 'opentelemetry-logs-sdk', path: '../../logs_sdk'
end

require 'opentelemetry-logs-sdk'

# Create a LoggerProvider
logger_provider = OpenTelemetry::SDK::Logs::LoggerProvider.new
# Create a batching processor configured to export to the OTLP exporter
processor = OpenTelemetry::SDK::Logs::Export::SimpleLogRecordProcessor.new(OpenTelemetry::SDK::Logs::Export::ConsoleLogRecordExporter.new)
# Add the processor to the LoggerProvider
logger_provider.add_log_record_processor(processor)
# Access a Logger for your library from your LoggerProvider
logger = logger_provider.logger(name: 'my_app_or_gem', version: '0.1.0')

# Use your Logger to emit a log record
logger.on_emit(
timestamp: Time.now,
severity_text: 'INFO',
body: 'Thuja plicata',
attributes: { 'cedar' => true },
)

logger_provider.shutdown
31 changes: 31 additions & 0 deletions examples/logs_sdk/logs_console_with_sdk.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

require 'bundler/inline'

gemfile(true) do
source 'https://rubygems.org'

gem 'opentelemetry-sdk', path: '../../sdk'
gem 'opentelemetry-logs-api', path: '../../logs_api'
gem 'opentelemetry-logs-sdk', path: '../../logs_sdk'
end

require 'opentelemetry-sdk'
require 'opentelemetry-logs-sdk'

# Export logs to the console
ENV['OTEL_LOGS_EXPORTER'] = 'console'

# Configure SDK with defaults, this will apply the OTEL_LOGS_EXPORTER env var
OpenTelemetry::SDK.configure

# Access a Logger for your library from the LoggerProvider configured by the OpenTelemetry API
logger = OpenTelemetry.logger_provider.logger(name: 'my_app_or_gem', version: '0.1.0')

# Use your Logger to emit a log record
logger.on_emit(
timestamp: Time.now,
severity_text: 'INFO',
body: 'Thuja plicata',
attributes: { 'cedar' => true },
)
33 changes: 33 additions & 0 deletions examples/logs_sdk/logs_otlp.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# frozen_string_literal: true

require 'bundler/inline'

gemfile(true) do
source 'https://rubygems.org'

gem 'opentelemetry-logs-api', path: '../../logs_api'
gem 'opentelemetry-logs-sdk', path: '../../logs_sdk'
gem 'opentelemetry-exporter-otlp-logs', path: '../../exporter/otlp-logs'
end

require 'opentelemetry-logs-sdk'
require 'opentelemetry/exporter/otlp_logs'

# Create a LoggerProvider
logger_provider = OpenTelemetry::SDK::Logs::LoggerProvider.new
# Create a batching processor configured to export to the OTLP exporter
processor = OpenTelemetry::SDK::Logs::Export::BatchLogRecordProcessor.new(OpenTelemetry::Exporter::OTLP::Logs::LogsExporter.new)
# Add the processor to the LoggerProvider
logger_provider.add_log_record_processor(processor)
# Access a Logger for your library from your LoggerProvider
logger = logger_provider.logger(name: 'my_app_or_gem', version: '0.1.0')

# Use your Logger to emit a log record
logger.on_emit(
timestamp: Time.now,
severity_text: 'INFO',
body: 'Thuja plicata',
attributes: { 'cedar' => true },
)

logger_provider.shutdown

0 comments on commit 035c32a

Please sign in to comment.