-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rename integration namespace to instrumentation
- Loading branch information
Showing
22 changed files
with
287 additions
and
256 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
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,72 @@ | ||
package OpenTelemetry::Instrumentation; | ||
# ABSTRACT: Top-level interface for OpenTelemetry instrumentations | ||
|
||
our $VERSION = '0.026'; | ||
|
||
use experimental 'signatures'; | ||
|
||
use Feature::Compat::Try; | ||
use List::Util 'uniqstr'; | ||
use Module::Load (); | ||
use Module::Pluggable search_path => ['OpenTelemetry::Instrumentation']; | ||
use Ref::Util 'is_hashref'; | ||
|
||
use Log::Any; | ||
my $logger = Log::Any->get_logger( category => 'OpenTelemetry' ); | ||
|
||
# To be overriden by instrumentations | ||
sub dependencies { } | ||
sub uninstall { } # experimental | ||
|
||
my @installed; | ||
sub import ( $class, @args ) { | ||
return unless @args; | ||
|
||
my $all = $args[0] =~ /^[:-]all$/ && shift @args; | ||
|
||
my %configuration; | ||
while ( my $key = shift @args ) { | ||
my $options = is_hashref $args[0] ? shift @args : {}; | ||
$configuration{ __PACKAGE__ . '::' . $key } = $options; | ||
} | ||
|
||
if ($all) { | ||
$configuration{$_} //= {} for $class->plugins | ||
} | ||
|
||
for my $package ( keys %configuration ) { | ||
try { | ||
$logger->tracef('Loading %s', $package); | ||
|
||
Module::Load::load($package); | ||
|
||
# We only load dependencies if we are not loading every module | ||
unless ($all) { | ||
Module::Load::load($_) for $package->dependencies; | ||
} | ||
|
||
my $ok = $package->install( %{ $configuration{ $package } } ); | ||
|
||
if ($ok) { | ||
push @installed, $package; | ||
} | ||
else { | ||
$logger->tracef("$package did not install itself"); | ||
} | ||
|
||
} catch ($e) { | ||
# Just a warning, if we're loading everything then | ||
# we shouldn't cause chaos just because something | ||
# doesn't happen to be available. | ||
$logger->warnf('Unable to load %s: %s', $package, $e); | ||
} | ||
} | ||
} | ||
|
||
sub unimport ( $class, @args ) { | ||
@args = @installed unless @args; | ||
$_->uninstall for @args; | ||
return; | ||
} | ||
|
||
1; |
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,117 @@ | ||
=encoding utf8 | ||
|
||
=head1 NAME | ||
|
||
OpenTelemetry::Instrumentation - Top-level interface for OpenTelemetry instrumentations | ||
|
||
=head1 SYNOPSIS | ||
|
||
# Load instrumentations for specific modules | ||
use OpenTelemetry::Instrumentation qw( HTTP::Tiny DBI ); | ||
|
||
# Some instrumentations can take additional options | ||
use OpenTelemetry::Instrumentation 'HTTP::Tiny' => \%options; | ||
|
||
# Load every available instrumentation | ||
use OpenTelemetry::Instrumentation -all; | ||
|
||
=head1 DESCRIPTION | ||
|
||
This is the base class for handling tracing instrumentation with other CPAN | ||
modules. | ||
|
||
It provides functionality for loading available instrumentations | ||
via the import list on a C<use> statement: | ||
|
||
=over 4 | ||
|
||
=item * | ||
|
||
with C<-all>, all available OpenTelemetry instrumentations will be loaded, | ||
if the module the instrumentation is for has already been loaded | ||
|
||
=item * | ||
|
||
with a specific list of modules, they will be loaded (even if they haven't | ||
been before) and instrumentations for them will be applied if available | ||
|
||
=back | ||
|
||
This means that you can expect L<HTTP::Tiny> to be traced if you have the | ||
L<OpenTelemetry::Instrumentation::HTTP::Tiny> module installed and you do this: | ||
|
||
use OpenTelemetry::Instrumentation 'HTTP::Tiny'; | ||
|
||
or this: | ||
|
||
use HTTP::Tiny; | ||
use OpenTelemetry::Instrumentation -all; | ||
|
||
but it will B<not> be traced if you do this: | ||
|
||
use OpenTelemetry::Instrumentation -all; | ||
use HTTP::Tiny; | ||
|
||
The rationale behind this apparently inconsistent behaviour is that, with a | ||
large install, C<:all> might load unexpected instrumentations. This behaviour | ||
allows you instead to add this line after your module imports, and any | ||
functionality that is actively being used in the code (for which an | ||
instrumentation module is available) would gain tracing. | ||
|
||
=head2 Configuring instrumentations | ||
|
||
=head1 WRITING INTEGRATIONS | ||
|
||
Additional instrumentations can be written and used as long as they are in the | ||
the OpenTelemetry::Instrumentation namespace. They should subclass this module | ||
(OpenTelemetry::Instrumentation) and should implement an C<install> class method | ||
as described in detail below. Other methods described in this section are | ||
optional, but can be used to provide additional features. | ||
|
||
=head2 install | ||
|
||
$bool = $class->install(%configuration); | ||
|
||
Installs the instrumentation. Will be called with a (possibly empty) list of | ||
key/value pairs with configuration options, and is expected to return a | ||
true value if the instrumentation was installed, or false otherwise. | ||
|
||
An example implementation of this method might look like the following: | ||
|
||
package OpenTelemetry::Instrumentation::Foo::Bar; | ||
|
||
use experimental 'signatures'; | ||
use Class::Inspector; | ||
use Class::Method::Modifiers 'install_modifier'; | ||
|
||
my $loaded; | ||
sub install ( $class, %config ) { | ||
return if $loaded++; | ||
return unless Class::Inspector->loaded('Foo::Bar'); | ||
|
||
install_modifier 'Foo::Bar' => around => reticulate_splines => sub { | ||
my ( $orig, $self, @args ) = @_; | ||
... | ||
my $value = $self->$orig(@args); | ||
... | ||
return $value; | ||
}; | ||
|
||
return 1; | ||
} | ||
|
||
=head2 dependencies | ||
|
||
@package_names = $class->dependencies; | ||
|
||
Should return the names of the packages that should be loaded to install | ||
this instrumentation. This method will be called in list context when loading | ||
dependencies automatically (ie. not when using the C<all> flag) to load | ||
the dependencies before calling L</install>. | ||
|
||
=head1 COPYRIGHT | ||
|
||
This software is copyright (c) 2023 by José Joaquín Atria. | ||
|
||
This is free software; you can redistribute it and/or modify it under the same | ||
terms as the Perl 5 programming language system itself. |
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
Oops, something went wrong.