| 1 | package OpenTracing::Implementation::DataDog::Tracer; | ||
| 2 | |||
| 3 | 3 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 3 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | =head1 NAME | ||
| 7 | |||
| 8 | OpenTracing::Implementation::DataDog::Tracer - Keep track of traces | ||
| 9 | |||
| 10 | =head1 SYNOPSIS | ||
| 11 | |||
| 12 | use aliased 'OpenTracing::Implementation::DataDog::Tracer'; | ||
| 13 | use aliased 'OpenTracing::Implementation::DataDog::Agent'; | ||
| 14 | use aliased 'OpenTracing::Implementation::DataDog::ScopeManager'; | ||
| 15 | |||
| 16 | my $TRACER = Tracer->new( | ||
| 17 | agent => Agent->new(), | ||
| 18 | scope_manager => ScopeManager->new(), | ||
| 19 | default_scope_builder => sub { | ||
| 20 | return { | ||
| 21 | service_name => 'Your Service name', | ||
| 22 | resource_name => 'Some Resource name', | ||
| 23 | } | ||
| 24 | }, | ||
| 25 | ); | ||
| 26 | |||
| 27 | and later | ||
| 28 | |||
| 29 | sub foo { | ||
| 30 | |||
| 31 | my $scope = $TRACER->start_active_span( 'Operation Name' => %options ); | ||
| 32 | |||
| 33 | ... | ||
| 34 | |||
| 35 | $scope->close; | ||
| 36 | |||
| 37 | return $foo | ||
| 38 | } | ||
| 39 | |||
| 40 | =cut | ||
| 41 | |||
| 42 | 3 | ▶ | use syntax 'maybe'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 43 | |||
| 44 | 3 | ▶ | use Moo; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 45 | |||
| 46 | with 'OpenTracing::Role::Tracer'; | ||
| 47 | |||
| 48 | 3 | ▶ | use aliased 'OpenTracing::Implementation::DataDog::Span'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 49 | use aliased 'OpenTracing::Implementation::DataDog::SpanContext'; | ||
| 50 | use aliased 'OpenTracing::Implementation::DataDog::Agent'; | ||
| 51 | use aliased 'OpenTracing::Implementation::DataDog::ScopeManager'; | ||
| 52 | |||
| 53 | use Carp; | ||
| 54 | use Ref::Util qw/is_plain_hashref/; | ||
| 55 | use Types::Standard qw/HashRef InstanceOf Maybe Object CodeRef/; | ||
| 56 | |||
| 57 | |||
| 58 | has agent => ( | ||
| 59 | is => 'lazy', | ||
| 60 | isa => Object, | ||
| 61 | handles => [qw/send_span/], | ||
| 62 | coerce | ||
| 63 | => sub { is_plain_hashref $_[0] ? Agent->new( %{$_[0]} ) : $_[0] }, | ||
| 64 | default => sub { {} }, | ||
| 65 | ); | ||
| 66 | |||
| 67 | |||
| 68 | has default_context => ( | ||
| 69 | is => 'lazy', | ||
| 70 | isa | ||
| 71 | => Maybe[InstanceOf['OpenTracing::Implementation::DataDog::SpanContext']], | ||
| 72 | coerce | ||
| 73 | => sub { is_plain_hashref $_[0] ? SpanContext->new( %{$_[0]} ) : $_[0] }, | ||
| 74 | default | ||
| 75 | => sub { croak "Can not construct a default SpanContext" }, | ||
| 76 | reader => 'get_default_context', | ||
| 77 | writer => 'set_default_context', | ||
| 78 | ); | ||
| 79 | |||
| 80 | |||
| 81 | has default_context_builder => ( | ||
| 82 | is => 'rw', | ||
| 83 | isa => CodeRef, | ||
| 84 | predicate => 1, | ||
| 85 | ); | ||
| 86 | |||
| 87 | sub _build_default_context_builder { | ||
| 88 | sub{ shift->get_default_context } | ||
| 89 | } | ||
| 90 | |||
| 91 | |||
| 92 | |||
| 93 | =head1 CAVEATS | ||
| 94 | |||
| 95 | C<extract_context> and C<inject_context> do not support any off the defined | ||
| 96 | methods at all. All that C<extract_context> does at this moment, is providing a | ||
| 97 | deafault C<SpanContext>, either given or cuntructed using a code-reference in | ||
| 98 | C<default_context_builder> | ||
| 99 | |||
| 100 | =cut | ||
| 101 | |||
| 102 | |||
| 103 | |||
| 104 | sub extract_context { | ||
| 105 | my $self = shift; | ||
| 106 | |||
| 107 | return $self->get_default_context | ||
| 108 | unless $self->has_default_context_builder; | ||
| 109 | |||
| 110 | my $builder_result = $self->default_context_builder->( @_ ); | ||
| 111 | |||
| 112 | return $builder_result | ||
| 113 | unless is_plain_hashref $builder_result; | ||
| 114 | |||
| 115 | return SpanContext->new( %{$builder_result} ); | ||
| 116 | } | ||
| 117 | |||
| 118 | |||
| 119 | |||
| 120 | sub inject_context { ... } | ||
| 121 | |||
| 122 | |||
| 123 | |||
| 124 | sub build_span { | ||
| 125 | my $self = shift; | ||
| 126 | my %opts = @_; | ||
| 127 | |||
| 128 | my $span = Span->new( | ||
| 129 | |||
| 130 | operation_name => $opts{ operation_name }, | ||
| 131 | |||
| 132 | child_of => $opts{ child_of }, | ||
| 133 | |||
| 134 | maybe | ||
| 135 | start_time => $opts{ start_time }, | ||
| 136 | |||
| 137 | maybe | ||
| 138 | tags => $opts{ tags }, | ||
| 139 | |||
| 140 | context => $opts{ context }, | ||
| 141 | |||
| 142 | on_DEMOLISH => sub { | ||
| 143 | my $span = shift; | ||
| 144 | $self->send_span( $span ) | ||
| 145 | }, | ||
| 146 | |||
| 147 | ); | ||
| 148 | |||
| 149 | return $span | ||
| 150 | } | ||
| 151 | |||
| 152 | |||
| 153 | |||
| 154 | sub _build_scope_manager { | ||
| 155 | my $self = shift; | ||
| 156 | |||
| 157 | return ScopeManager->new( @_ ) | ||
| 158 | } | ||
| 159 | |||
| 160 | 1; | ||
| 161 | |||