From 035c32ad9791f6200733e087f2ee49e0a615879a Mon Sep 17 00:00:00 2001 From: Kayla Reopelle <87386821+kaylareopelle@users.noreply.github.com> Date: Thu, 12 Dec 2024 17:03:41 -0800 Subject: [PATCH] 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 --- examples/logs_sdk/logs_console.rb | 31 ++++++++++++++++++++ examples/logs_sdk/logs_console_with_sdk.rb | 31 ++++++++++++++++++++ examples/logs_sdk/logs_otlp.rb | 33 ++++++++++++++++++++++ 3 files changed, 95 insertions(+) create mode 100644 examples/logs_sdk/logs_console.rb create mode 100644 examples/logs_sdk/logs_console_with_sdk.rb create mode 100644 examples/logs_sdk/logs_otlp.rb diff --git a/examples/logs_sdk/logs_console.rb b/examples/logs_sdk/logs_console.rb new file mode 100644 index 0000000000..b0e6abfe62 --- /dev/null +++ b/examples/logs_sdk/logs_console.rb @@ -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 diff --git a/examples/logs_sdk/logs_console_with_sdk.rb b/examples/logs_sdk/logs_console_with_sdk.rb new file mode 100644 index 0000000000..e118118bbe --- /dev/null +++ b/examples/logs_sdk/logs_console_with_sdk.rb @@ -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 }, +) diff --git a/examples/logs_sdk/logs_otlp.rb b/examples/logs_sdk/logs_otlp.rb new file mode 100644 index 0000000000..611bf8d049 --- /dev/null +++ b/examples/logs_sdk/logs_otlp.rb @@ -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