-
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.
feat: Create Logs SDK LoggerProvider (#1517)
* feat: Create Logs SDK LoggerProvider * Add OpenTelemetry::SDK::Logs::Export constants * Create OpenTelemetry::SDK::Logs::LoggerProvider * Implement LoggerProvider#logger * Implement LoggerProvider#shutdown * Implement LoggerProvider#force_flush * Create no-op LogRecordProcessor * Update Export module description * Update #on_emit context arg description * Remove public reader for log_record_processors * LoggerProvider#logger: keyword args, require name * Remove attr_readers for scope and provider * Apply suggestions from code review Co-authored-by: Francis Bogsanyi <[email protected]> * Remove unncessary error handling * Warn #add_log_record_processor calls when stopped * Remove "optional" marker from type description Co-authored-by: Olle Jonsson <[email protected]> * Remove "optional" from type documentation * Apply suggestions from code review Co-authored-by: Francis Bogsanyi <[email protected]> * Apply suggestions from code review Co-authored-by: Francis Bogsanyi <[email protected]> --------- Co-authored-by: Francis Bogsanyi <[email protected]> Co-authored-by: Olle Jonsson <[email protected]>
- Loading branch information
1 parent
9da08e4
commit cff311c
Showing
11 changed files
with
424 additions
and
26 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
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
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
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,23 @@ | ||
# frozen_string_literal: true | ||
|
||
# Copyright The OpenTelemetry Authors | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
module OpenTelemetry | ||
module SDK | ||
module Logs | ||
# The Export module contains result codes for exporters | ||
module Export | ||
# The operation finished successfully. | ||
SUCCESS = 0 | ||
|
||
# The operation finished with an error. | ||
FAILURE = 1 | ||
|
||
# The operation timed out. | ||
TIMEOUT = 2 | ||
end | ||
end | ||
end | ||
end |
47 changes: 47 additions & 0 deletions
47
logs_sdk/lib/opentelemetry/sdk/logs/log_record_processor.rb
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,47 @@ | ||
# frozen_string_literal: true | ||
|
||
# Copyright The OpenTelemetry Authors | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
module OpenTelemetry | ||
module SDK | ||
module Logs | ||
# LogRecordProcessor describes a duck type and provides a synchronous no-op hook for when a | ||
# {LogRecord} is emitted. It is not required to subclass this | ||
# class to provide an implementation of LogRecordProcessor, provided the interface is | ||
# satisfied. | ||
class LogRecordProcessor | ||
# Called when a {LogRecord} is emitted. Subsequent calls are not | ||
# permitted after shutdown is called. | ||
# @param [LogRecord] log_record The emitted {LogRecord} | ||
# @param [Context] context The {Context} | ||
def on_emit(log_record, context); end | ||
|
||
# Export all log records to the configured `Exporter` that have not yet | ||
# been exported. | ||
# | ||
# This method should only be called in cases where it is absolutely | ||
# necessary, such as when using some FaaS providers that may suspend | ||
# the process after an invocation, but before the `Processor` exports | ||
# the completed spans. | ||
# | ||
# @param [Numeric] timeout An optional timeout in seconds. | ||
# @return [Integer] Export::SUCCESS if no error occurred, Export::FAILURE if | ||
# a non-specific failure occurred, Export::TIMEOUT if a timeout occurred. | ||
def force_flush(timeout: nil) | ||
Export::SUCCESS | ||
end | ||
|
||
# Called when {LoggerProvider#shutdown} is called. | ||
# | ||
# @param [Numeric] timeout An optional timeout in seconds. | ||
# @return [Integer] Export::SUCCESS if no error occurred, Export::FAILURE if | ||
# a non-specific failure occurred, Export::TIMEOUT if a timeout occurred. | ||
def shutdown(timeout: nil) | ||
Export::SUCCESS | ||
end | ||
end | ||
end | ||
end | ||
end |
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 | ||
|
||
# Copyright The OpenTelemetry Authors | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
module OpenTelemetry | ||
module SDK | ||
module Logs | ||
# The SDK implementation of OpenTelemetry::Logs::Logger | ||
class Logger < OpenTelemetry::Logs::Logger | ||
# @api private | ||
# | ||
# Returns a new {OpenTelemetry::SDK::Logs::Logger} instance. This should | ||
# not be called directly. New loggers should be created using | ||
# {LoggerProvider#logger}. | ||
# | ||
# @param [String] name Instrumentation package name | ||
# @param [String] version Instrumentation package version | ||
# @param [LoggerProvider] logger_provider The {LoggerProvider} that | ||
# initialized the logger | ||
# | ||
# @return [OpenTelemetry::SDK::Logs::Logger] | ||
def initialize(name, version, logger_provider) | ||
@instrumentation_scope = InstrumentationScope.new(name, version) | ||
@logger_provider = logger_provider | ||
end | ||
end | ||
end | ||
end | ||
end |
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,126 @@ | ||
# frozen_string_literal: true | ||
|
||
# Copyright The OpenTelemetry Authors | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
module OpenTelemetry | ||
module SDK | ||
module Logs | ||
# The SDK implementation of OpenTelemetry::Logs::LoggerProvider. | ||
class LoggerProvider < OpenTelemetry::Logs::LoggerProvider | ||
attr_reader :resource | ||
|
||
UNEXPECTED_ERROR_MESSAGE = 'unexpected error in ' \ | ||
'OpenTelemetry::SDK::Logs::LoggerProvider#%s' | ||
|
||
private_constant :UNEXPECTED_ERROR_MESSAGE | ||
|
||
# Returns a new LoggerProvider instance. | ||
# | ||
# @param [optional Resource] resource The resource to associate with | ||
# new LogRecords created by {Logger}s created by this LoggerProvider. | ||
# | ||
# @return [OpenTelemetry::SDK::Logs::LoggerProvider] | ||
def initialize(resource: OpenTelemetry::SDK::Resources::Resource.create) | ||
@log_record_processors = [] | ||
@mutex = Mutex.new | ||
@resource = resource | ||
@stopped = false | ||
end | ||
|
||
# Returns an {OpenTelemetry::SDK::Logs::Logger} instance. | ||
# | ||
# @param [String] name Instrumentation package name | ||
# @param [optional String] version Instrumentation package version | ||
# | ||
# @return [OpenTelemetry::SDK::Logs::Logger] | ||
def logger(name:, version: nil) | ||
version ||= '' | ||
|
||
if !name.is_a?(String) || name.empty? | ||
OpenTelemetry.logger.warn('LoggerProvider#logger called with an ' \ | ||
"invalid name. Name provided: #{name.inspect}") | ||
end | ||
|
||
Logger.new(name, version, self) | ||
end | ||
|
||
# Adds a new log record processor to this LoggerProvider's | ||
# log_record_processors. | ||
# | ||
# @param [LogRecordProcessor] log_record_processor The | ||
# {LogRecordProcessor} to add to this LoggerProvider. | ||
def add_log_record_processor(log_record_processor) | ||
@mutex.synchronize do | ||
if @stopped | ||
OpenTelemetry.logger.warn('calling LoggerProvider#' \ | ||
'add_log_record_processor after shutdown.') | ||
return | ||
end | ||
@log_record_processors = @log_record_processors.dup.push(log_record_processor) | ||
end | ||
end | ||
|
||
# Attempts to stop all the activity for this LoggerProvider. Calls | ||
# {LogRecordProcessor#shutdown} for all registered {LogRecordProcessor}s. | ||
# | ||
# This operation may block until all log records are processed. Must | ||
# be called before turning off the main application to ensure all data | ||
# are processed and exported. | ||
# | ||
# After this is called all newly created {LogRecord}s will be no-op. | ||
# | ||
# @param [optional Numeric] timeout An optional timeout in seconds. | ||
# @return [Integer] Export::SUCCESS if no error occurred, Export::FAILURE if | ||
# a non-specific failure occurred, Export::TIMEOUT if a timeout occurred. | ||
def shutdown(timeout: nil) | ||
@mutex.synchronize do | ||
if @stopped | ||
OpenTelemetry.logger.warn('LoggerProvider#shutdown called multiple times.') | ||
return Export::FAILURE | ||
end | ||
|
||
start_time = OpenTelemetry::Common::Utilities.timeout_timestamp | ||
results = @log_record_processors.map do |processor| | ||
remaining_timeout = OpenTelemetry::Common::Utilities.maybe_timeout(timeout, start_time) | ||
break [Export::TIMEOUT] if remaining_timeout&.zero? | ||
|
||
processor.shutdown(timeout: remaining_timeout) | ||
end | ||
|
||
@stopped = true | ||
results.max || Export::SUCCESS | ||
end | ||
end | ||
|
||
# Immediately export all {LogRecord}s that have not yet been exported | ||
# for all the registered {LogRecordProcessor}s. | ||
# | ||
# This method should only be called in cases where it is absolutely | ||
# necessary, such as when using some FaaS providers that may suspend | ||
# the process after an invocation, but before the {LogRecordProcessor} | ||
# exports the completed {LogRecord}s. | ||
# | ||
# @param [optional Numeric] timeout An optional timeout in seconds. | ||
# @return [Integer] Export::SUCCESS if no error occurred, Export::FAILURE if | ||
# a non-specific failure occurred, Export::TIMEOUT if a timeout occurred. | ||
def force_flush(timeout: nil) | ||
@mutex.synchronize do | ||
return Export::SUCCESS if @stopped | ||
|
||
start_time = OpenTelemetry::Common::Utilities.timeout_timestamp | ||
results = @log_record_processors.map do |processor| | ||
remaining_timeout = OpenTelemetry::Common::Utilities.maybe_timeout(timeout, start_time) | ||
return Export::TIMEOUT if remaining_timeout&.zero? | ||
|
||
processor.force_flush(timeout: remaining_timeout) | ||
end | ||
|
||
results.max || Export::SUCCESS | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
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
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 |
---|---|---|
@@ -1,24 +1,8 @@ | ||
# inherit_from: .rubocop_todo.yml | ||
inherit_from: ../.rubocop.yml | ||
|
||
AllCops: | ||
TargetRubyVersion: '3.0' | ||
|
||
Lint/UnusedMethodArgument: | ||
Enabled: false | ||
Metrics/AbcSize: | ||
Metrics/BlockLength: | ||
Enabled: false | ||
Metrics/LineLength: | ||
Enabled: false | ||
Metrics/MethodLength: | ||
Max: 50 | ||
Metrics/PerceivedComplexity: | ||
Max: 30 | ||
Metrics/CyclomaticComplexity: | ||
Max: 20 | ||
Metrics/ParameterLists: | ||
Enabled: false | ||
Naming/FileName: | ||
Exclude: | ||
- 'lib/opentelemetry-logs-sdk.rb' | ||
Style/ModuleFunction: | ||
Metrics/AbcSize: | ||
Enabled: false |
Oops, something went wrong.