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

Die on errors during import #19

Merged
merged 1 commit into from
Dec 19, 2024
Merged
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
4 changes: 4 additions & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ Revision history for OpenTelemetry-SDK
initially because it depended on Google::ProtocolBuffers::Dynamic,
but now it doesn't: it will use that distribution if it is
available but use JSON otherwise.
* BREAKING CHANGE: the SDK will now die if it encounters an error
during initialisation. This will only affect the code executed
during import. Once the SDK has been loaded, it will never
intentionally raise an exception.

0.024 2024-08-02 15:28:11+01:00 Europe/London

Expand Down
25 changes: 15 additions & 10 deletions lib/OpenTelemetry/SDK.pm
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,19 @@ our $VERSION = '0.025';

use strict;
use warnings;
use experimental qw( signatures lexical_subs );
use experimental qw( isa signatures lexical_subs );
use feature 'state';

use Module::Runtime;
use Feature::Compat::Try;
use Log::Any;
use Module::Runtime;
use OpenTelemetry::Common 'config';
use OpenTelemetry::Propagator::Composite;
use OpenTelemetry::SDK::Trace::TracerProvider;
use OpenTelemetry;
use OpenTelemetry::X;

my sub configure_propagators {
my $logger = OpenTelemetry->logger;
my $logger = Log::Any->get_logger( category => 'OpenTelemetry' );

state %map = (
b3 => 'B3',
Expand Down Expand Up @@ -49,7 +50,9 @@ my sub configure_propagators {
push @propagators, $class->new;
}
catch ($e) {
$logger->warnf("Error configuring '%s' propagator: %s", $name, $e);
die OpenTelemetry::X->create(
Invalid => "Error configuring '$name' propagator: $e",
);
}
}

Expand All @@ -58,7 +61,7 @@ my sub configure_propagators {
}

my sub configure_span_processors {
my $logger = OpenTelemetry->logger;
my $logger = Log::Any->get_logger( category => 'OpenTelemetry' );

state %map = (
jaeger => '::Jaeger',
Expand Down Expand Up @@ -99,7 +102,9 @@ my sub configure_span_processors {
);
}
catch ($e) {
$logger->warnf("Error configuring '%s' span processor: %s", $name, $e);
die OpenTelemetry::X->create(
Invalid => "Error configuring '$name' span processor: $e",
);
}
}

Expand All @@ -117,9 +122,9 @@ sub import ( $class ) {
configure_span_processors();
}
catch ($e) {
OpenTelemetry->handle_error(
exception => $e,
message => 'Unexpected configuration error'
die $e if $e isa OpenTelemetry::X;
die OpenTelemetry::X->create(
Invalid => "Unexpected error initialising OpenTelemetry::SDK: $e",
);
}
}
Expand Down
13 changes: 11 additions & 2 deletions lib/OpenTelemetry/SDK.pod
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,17 @@ the environment.
=head1 CONFIGURATION

When loaded, the SDK will read its configuration from the environment and
automatically apply those settings. This section lists the environment
variables that are supported by the SDK and the way they are interpreted.
automatically apply those settings. Starting with version 0.025, if an error
is encountered during import, the SDK will raise an
L<OpenTelemetry::X::Invalid> exception and terminate. While the
L<specification|https://opentelemetry.io/docs/specs/otel/error-handling/#basic-error-handling-principles>
is clear that "OpenTelemetry implementations MUST NOT
throw unhandled exceptions at runtime" it explicitly states that the SDK
"MAY I<fail fast> and cause the application to fail on initialization". This
is the only scenario in which the SDK will potentially terminate a program.

The remainder of this section lists the environment variables that are
supported by the SDK and the way they are interpreted.

The OpenTelemetry specification has
L<a full list of variables|https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/configuration/sdk-environment-variables.md>,
Expand Down
40 changes: 21 additions & 19 deletions t/OpenTelemetry/SDK.t
Original file line number Diff line number Diff line change
Expand Up @@ -37,37 +37,39 @@ it 'Can be disabled' => sub {
is $tracer_provider, U, 'Leaves tracer provider unchanged';
};

it 'Handles broken modules' => sub {
it 'Dies on broken modules' => sub {
local %ENV = (
OTEL_TRACES_EXPORTER => 'console',
OTEL_PROPAGATORS => 'baggage',
);

my $orig = \&Module::Runtime::require_module;
$require_mock->override(
require_module => sub ($) { die 'oops' },
require_module => sub ($) {
goto $orig
if $_[0] !~ /^OpenTelemetry/
|| $_[0] =~ /^OpenTelemetry::X/;
die 'oops';
},
);

is messages { OpenTelemetry::SDK->import }, [
[ warning => OpenTelemetry => match qr/Error configuring 'baggage'/ ],
[ warning => OpenTelemetry => match qr/Error configuring 'console'/ ],
], 'Logged unknown propagator';

is $propagator, object {
prop isa => 'OpenTelemetry::Propagator::Composite';
call_list keys => [];
}, 'Ignored unknown propagator';
like dies { OpenTelemetry::SDK->import },
qr/^Error configuring 'baggage' propagator: oops/;

$require_mock->reset_all;
};

it 'Handles unexpected errors' => sub {
my $die = mock 'OpenTelemetry' => override => [ logger => sub { die 'boom' } ];
is messages { OpenTelemetry::SDK->import }, [
[
error => 'OpenTelemetry',
match qr/OpenTelemetry error: Unexpected configuration error - boom/,
],
], 'Logged unknown propagator';
it 'Dies on errors during import' => sub {
my $orig = \&OpenTelemetry::SDK::config;
my $die = mock 'OpenTelemetry::SDK' => override => [
config => sub {
goto $orig if $_[0] eq 'SDK_DISABLED';
die 'boom';
},
];

like dies { OpenTelemetry::SDK->import },
qr/^Unexpected error initialising OpenTelemetry::SDK: boom/;
};

describe 'Propagators' => sub {
Expand Down
Loading