-
Notifications
You must be signed in to change notification settings - Fork 251
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Add examples for logs (#1774)
* 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
1 parent
bb51595
commit 035c32a
Showing
3 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |