Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Create SDK LogRecord #5

Draft
wants to merge 2 commits into
base: logger-provider-sdk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions logs_api/lib/opentelemetry/logs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
require_relative 'logs/log_record'
require_relative 'logs/logger'
require_relative 'logs/logger_provider'
require_relative 'logs/severity_number'

module OpenTelemetry
# The Logs API records a timestamped record with metadata.
Expand Down
51 changes: 51 additions & 0 deletions logs_api/lib/opentelemetry/logs/severity_number.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# frozen_string_literal: true

# Copyright The OpenTelemetry Authors
#
# SPDX-License-Identifier: Apache-2.0

module OpenTelemetry
module Logs
# Class with constants representing the numerical value of the log severity
# as defined in the OpenTelemetry specification.
class SeverityNumber
# TRACE: A fine-grained debugging event. Typically disabled in
# default configurations.
TRACE = 1
TRACE2 = 2
TRACE3 = 3
TRACE4 = 4

# DEBUG: Used for debugging events.
DEBUG = 5
DEBUG2 = 6
DEBUG3 = 7
DEBUG4 = 8

# INFO: An informational event. Indicates that an event happened.
INFO = 9
INFO2 = 10
INFO3 = 11
INFO4 = 12

# WARN: A warning event. Not an error but is likely more important than an
# informational event.
WARN = 13
WARN2 = 14
WARN3 = 15
WARN4 = 16

# ERROR: An error event. Something went wrong.
ERROR = 17
ERROR2 = 18
ERROR3 = 19
ERROR4 = 20

# FATAL: A fatal error such as application or system crash.
FATAL = 21
FATAL2 = 22
FATAL3 = 23
FATAL4 = 24
end
end
end
1 change: 1 addition & 0 deletions logs_sdk/lib/opentelemetry/sdk/logs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
require_relative 'logs/logger_provider'
require_relative 'logs/log_record_processor'
require_relative 'logs/export'
require_relative 'logs/log_record'

module OpenTelemetry
module SDK
Expand Down
71 changes: 71 additions & 0 deletions logs_sdk/lib/opentelemetry/sdk/logs/log_record.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# frozen_string_literal: true

# Copyright The OpenTelemetry Authors
#
# SPDX-License-Identifier: Apache-2.0

module OpenTelemetry
module SDK
module Logs
# Implementation of OpenTelemetry::Logs::LogRecord that records log events.
class LogRecord < OpenTelemetry::Logs::LogRecord
attr_accessor :timestamp,
:observed_timestamp,
:span_context,
:severity_text,
:severity_number,
:body,
:resource,
:instrumentation_scope,
:attributes

# Creates a new {LogRecord}.
#
# @param [optional Float, Time] timestamp Time when the event occurred.
# @param [optional Float, Time] observed_timestamp Time when the event
# was observed by the collection system. If nil, will first attempt
# to set to `timestamp`. If `timestamp` is nil, will set to
# `Process.clock_gettime(Process::CLOCK_REALTIME)`.
# @param [optional OpenTelemetry::Trace::SpanContext] span_context The
# OpenTelemetry::Trace::SpanContext to associate with the
# {LogRecord}.
# @param [optional String] severity_text The log severity, also known as
# log level.
# @param [optional Integer] severity_number The numerical value of the
# log severity. See OpenTelemetry::Logs::SeverityNumber.
# @param [optional String, Numeric, Boolean, Array<String, Numeric,
# Boolean>, Hash{String => String, Numeric, Boolean, Array<String,
# Numeric, Boolean>}] body The body of the {LogRecord}.
# @param [optional Hash{String => String, Numeric, Boolean,
# Array<String, Numeric, Boolean>}] attributes Attributes to associate
# with the {LogRecord}.
# @param [optional OpenTelemetry::SDK::Logs::Logger] logger The logger that
# created the {LogRecord}. Used to set `resource` and
# `instrumentation_scope`.
#
# @return [LogRecord]
def initialize(
timestamp: nil,
observed_timestamp: nil,
span_context: nil,
severity_text: nil,
severity_number: nil,
body: nil,
attributes: nil,
logger: nil
)
@timestamp = timestamp
@observed_timestamp = observed_timestamp || timestamp || Process.clock_gettime(Process::CLOCK_REALTIME)
@span_context = span_context
@severity_text = severity_text
@severity_number = severity_number
@body = body
@resource = logger&.resource
@instrumentation_scope = logger&.instrumentation_scope
# TODO: Give attributes more love when working on limits, Issue #1516
@attributes = attributes
end
end
end
end
end
4 changes: 4 additions & 0 deletions logs_sdk/lib/opentelemetry/sdk/logs/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ def initialize(name, version, logger_provider)
@instrumentation_scope = InstrumentationScope.new(name, version)
@logger_provider = logger_provider
end

def resource
logger_provider.resource
end
end
end
end
Expand Down
94 changes: 94 additions & 0 deletions logs_sdk/test/opentelemetry/sdk/logs/log_record_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# frozen_string_literal: true

# Copyright The OpenTelemetry Authors
#
# SPDX-License-Identifier: Apache-2.0

require 'test_helper'

describe OpenTelemetry::SDK::Logs::LogRecord do
let(:log_record) { OpenTelemetry::SDK::Logs::LogRecord.new(**args) }
let(:args) { {} }

describe '#initialize' do
describe 'observed_timestamp' do
describe 'when observed_timestamp is present' do
let(:observed_timestamp) { '1692661486.2841358' }
let(:args) { { observed_timestamp: observed_timestamp } }

it 'is equal to observed_timestamp' do
assert_equal(observed_timestamp, log_record.observed_timestamp)
end

it 'is not equal to timestamp' do
refute_equal(log_record.timestamp, log_record.observed_timestamp)
end

# Process.clock_gettime is used to set the current time
# That method returns a Float. Since the stubbed value of
# observed_timestamp is a String, we can know the the
# observed_timestamp was not set to the value of Process.clock_gettime
# by making sure its value is not a Float.
it 'is not equal to the current time' do
refute_instance_of(Float, log_record.observed_timestamp)
end
end

describe 'when timestamp is present' do
let(:timestamp) { Process.clock_gettime(Process::CLOCK_REALTIME) }
let(:args) { { timestamp: timestamp } }

it 'is equal to timestamp' do
assert_equal(timestamp, log_record.observed_timestamp)
end
end

describe 'when observed_timestamp and timestamp are nil' do
let(:args) { { timestamp: nil, observed_timestamp: nil } }

it 'is not nil' do
refute_nil(log_record.observed_timestamp)
end

it 'is equal to the current time' do
# Since I can't get the current time when the test was run
# I'm going to assert it's a Float, which is the Process.clock_gettime
# return value class.
assert_instance_of(Float, log_record.observed_timestamp)
end
end
end

describe 'attributes set through logger' do
let(:logger_provider) { OpenTelemetry::SDK::Logs::LoggerProvider.new }
let(:resource) { OpenTelemetry::SDK::Resources::Resource.create }
let(:instrumentation_scope) { OpenTelemetry::SDK::InstrumentationScope.new('name', 'version') }
let(:logger) { OpenTelemetry::SDK::Logs::Logger.new(resource, instrumentation_scope, logger_provider) }
let(:args) { { logger: logger } }

describe 'resource' do
it 'is set to the resource of the logger given on initialization' do
assert_equal(logger.resource, log_record.resource)
end
end

describe 'instrumentation_scope' do
it 'is set to the instrumentation_scope of the logger given on initialization' do
assert_equal(logger.instrumentation_scope, log_record.instrumentation_scope)
end
end

describe 'when logger is nil' do
let(:logger) { nil }

it 'sets the resource to nil' do
assert_nil(log_record.resource)
end

it 'sets the instrumentation_scope to nil' do
assert_nil(log_record.instrumentation_scope)
end
end
end
end
end
18 changes: 18 additions & 0 deletions logs_sdk/test/opentelemetry/sdk/logs/logger_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

# Copyright The OpenTelemetry Authors
#
# SPDX-License-Identifier: Apache-2.0

require 'test_helper'

describe OpenTelemetry::SDK::Logs::Logger do
let(:logger_provider) { OpenTelemetry::SDK::Logs::LoggerProvider.new }
let(:logger) { logger_provider.logger }

describe '#resource' do
it 'returns the resource associated with the logger_provider' do
assert_equal(logger.resource, logger_provider.resource)
end
end
end