| 1 | package OpenTracing::Implementation::DataDog::SpanContext; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | OpenTracing::Implementation::DataDog::SpanContext - A DataDog Implementation | ||
| 6 | |||
| 7 | =cut | ||
| 8 | |||
| 9 | 26 | our $VERSION = 'v0.47.2'; | |
| 10 | |||
| 11 | |||
| 12 | =head1 SYNOPSIS | ||
| 13 | |||
| 14 | use aliased OpenTracing::Implementation::DataDog::SpanContext; | ||
| 15 | |||
| 16 | my $span_context = SpanContext->new( | ||
| 17 | service_name => "MyFancyService", | ||
| 18 | service_type => "web", | ||
| 19 | resource_name => "/clients/{client_id}/contactdetails", | ||
| 20 | environment => 'staging', | ||
| 21 | hostname => 'web.01.host', | ||
| 22 | ); | ||
| 23 | # | ||
| 24 | # please do not add parameter values in the resource, | ||
| 25 | # use tags instead, like: | ||
| 26 | # $span->set_tag( client_id => $request->query_params('client_id') ) | ||
| 27 | |||
| 28 | =cut | ||
| 29 | |||
| 30 | 26 | ▶ | use Moo; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 31 | 26 | ▶ | use MooX::Enumeration; # do needs to be the first eXtension |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 32 | 26 | ▶ | use MooX::Attribute::ENV; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 33 | 26 | ▶ | use MooX::Should; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 34 | |||
| 35 | 26 | with 'OpenTracing::Role::SpanContext'; | |
| 36 | |||
| 37 | 26 | ▶ | use OpenTracing::Implementation::DataDog::Utils qw/random_bigint/; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 38 | |||
| 39 | 26 | ▶ | use Sub::Trigger::Lock; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 40 | 26 | ▶ | use Types::Common::String qw/NonEmptyStr/; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 41 | 26 | ▶ | use Types::Standard qw/Any InstanceOf Maybe/; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 42 | |||
| 43 | |||
| 44 | my $BigInt = (InstanceOf['Math::BigInt'])->plus_coercions( | ||
| 45 | 35 | ▶ | Any, sub { Math::BigInt->new($_) }, |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 46 | 26 | ); | |
| 47 | |||
| 48 | =head1 DESCRIPTION | ||
| 49 | |||
| 50 | This is a L<OpenTracing SpanContext|OpenTracing::Interface::SpanContext> | ||
| 51 | compliant implementation with DataDog specific extentions | ||
| 52 | |||
| 53 | =cut | ||
| 54 | |||
| 55 | |||
| 56 | |||
| 57 | =head1 EXTENDED ATTRIBUTES | ||
| 58 | |||
| 59 | =cut | ||
| 60 | |||
| 61 | |||
| 62 | |||
| 63 | =head2 C<trace_id> | ||
| 64 | |||
| 65 | DataDog requires this to be an unsigned 64-bit integer | ||
| 66 | |||
| 67 | =cut | ||
| 68 | |||
| 69 | has '+trace_id' => ( | ||
| 70 | is =>'ro', | ||
| 71 | isa => $BigInt, | ||
| 72 | coerce => 1, | ||
| 73 | 55 | ▶ | default => sub { random_bigint() }, |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 74 | 26 | ); | |
| 75 | |||
| 76 | 24 | ▶ | around with_trace_id => sub { |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 77 | 24 | my ($orig, $self, $id) = @_; | |
| 78 | 24 | $orig->($self, $BigInt->coerce($id)); | |
| 79 | 26 | }; | |
| 80 | |||
| 81 | |||
| 82 | =head2 C<span_id> | ||
| 83 | |||
| 84 | DataDog requires this to be an unsigned 64-bit integer | ||
| 85 | |||
| 86 | =cut | ||
| 87 | |||
| 88 | has '+span_id' => ( | ||
| 89 | is =>'ro', | ||
| 90 | isa => $BigInt, | ||
| 91 | coerce => 1, | ||
| 92 | 55 | ▶ | default => sub{ random_bigint() } |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 93 | 26 | ); | |
| 94 | |||
| 95 | 19 | ▶ | around with_span_id => sub { |
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||
| 96 | 19 | my ($orig, $self, $id) = @_; | |
| 97 | 19 | $orig->($self, $BigInt->coerce($id)); | |
| 98 | 26 | }; | |
| 99 | |||
| 100 | |||
| 101 | =head1 DATADOG SPECIFIC ATTRIBUTES | ||
| 102 | |||
| 103 | =cut | ||
| 104 | |||
| 105 | |||
| 106 | |||
| 107 | =head2 C<service_name> | ||
| 108 | |||
| 109 | A required C<NonEmptyString> where C<length <= 100>. | ||
| 110 | |||
| 111 | Defaults to the value of the C<DD_SERVICE_NAME> environment variable if set. | ||
| 112 | |||
| 113 | The service-name will usually be the name of the application and could easilly | ||
| 114 | be set by a intergration solution. | ||
| 115 | |||
| 116 | =cut | ||
| 117 | |||
| 118 | 26 | has service_name => ( | |
| 119 | is => 'ro', | ||
| 120 | env_key => [ 'DD_SERVICE', 'DD_SERVICE_NAME' ], # only DD_SERVICE!! | ||
| 121 | required => 1, | ||
| 122 | should => NonEmptyStr->where( 'length($_) <= 100' ), | ||
| 123 | reader => 'get_service_name', | ||
| 124 | trigger => Lock, | ||
| 125 | ); | ||
| 126 | |||
| 127 | |||
| 128 | |||
| 129 | =head2 C<service_type> | ||
| 130 | |||
| 131 | An enumeration on C<web>, C<db>, C<cache>, and C<custom>, which is the default. | ||
| 132 | |||
| 133 | DataDog has four different service types to make it more easy to visualize the | ||
| 134 | data. See L<Service List at DataDog HQ| | ||
| 135 | https://docs.datadoghq.com/tracing/visualization/services_list/#services-types> | ||
| 136 | |||
| 137 | =cut | ||
| 138 | |||
| 139 | 26 | has service_type => ( | |
| 140 | is => 'ro', | ||
| 141 | default => 'custom', | ||
| 142 | enum => [qw/web db cache custom/], | ||
| 143 | handles => 2, # such that we have `service_type_is_...` | ||
| 144 | reader => 'get_service_type', | ||
| 145 | trigger => Lock, | ||
| 146 | ); | ||
| 147 | |||
| 148 | |||
| 149 | |||
| 150 | =head2 C<resource_name> | ||
| 151 | |||
| 152 | A required C<NonEmptyString> where C<length <= 5000>. | ||
| 153 | |||
| 154 | Good candidates for resource names are URL paths or databasenames and or tables. | ||
| 155 | |||
| 156 | =cut | ||
| 157 | |||
| 158 | 26 | has resource_name => ( | |
| 159 | is => 'ro', | ||
| 160 | should => NonEmptyStr->where( 'length($_) <= 5000' ), | ||
| 161 | required => 1, | ||
| 162 | reader => 'get_resource_name', | ||
| 163 | trigger => Lock, | ||
| 164 | ); | ||
| 165 | |||
| 166 | |||
| 167 | |||
| 168 | =head2 C<environment> | ||
| 169 | |||
| 170 | An optional C<NonEmptyString> where C<length <= 5000>. | ||
| 171 | |||
| 172 | =cut | ||
| 173 | |||
| 174 | 26 | has environment => ( | |
| 175 | is => 'ro', | ||
| 176 | should => Maybe[NonEmptyStr->where( 'length($_) <= 5000' )], | ||
| 177 | required => 0, | ||
| 178 | env_key => 'DD_ENV', | ||
| 179 | reader => 'get_environment', | ||
| 180 | trigger => Lock, | ||
| 181 | ); | ||
| 182 | |||
| 183 | |||
| 184 | |||
| 185 | =head2 C<hostname> | ||
| 186 | |||
| 187 | An optional C<NonEmptyString> where C<length <= 5000>. | ||
| 188 | |||
| 189 | =cut | ||
| 190 | |||
| 191 | 26 | has hostname => ( | |
| 192 | is => 'ro', | ||
| 193 | should => Maybe[NonEmptyStr->where( 'length($_) <= 5000' )], | ||
| 194 | required => 0, | ||
| 195 | env_key => 'DD_HOSTNAME', | ||
| 196 | reader => 'get_hostname', | ||
| 197 | trigger => Lock, | ||
| 198 | ); | ||
| 199 | |||
| 200 | |||
| 201 | |||
| 202 | =head2 C<version> | ||
| 203 | |||
| 204 | An optional C<NonEmptyString> where C<length <= 5000>. | ||
| 205 | |||
| 206 | =cut | ||
| 207 | |||
| 208 | 26 | has version => ( | |
| 209 | is => 'ro', | ||
| 210 | should => Maybe[NonEmptyStr->where( 'length($_) <= 5000' )], | ||
| 211 | required => 0, | ||
| 212 | env_key => 'DD_VERSION', | ||
| 213 | reader => 'get_version', | ||
| 214 | trigger => Lock, | ||
| 215 | ); | ||
| 216 | |||
| 217 | |||
| 218 | |||
| 219 | =head1 CONSTRUCTORS | ||
| 220 | |||
| 221 | =head2 Warning: | ||
| 222 | |||
| 223 | Constructors are not to be used outside an implementation, they are not part of | ||
| 224 | the L<OpenTracing API|OpenTracing::Interface>. | ||
| 225 | |||
| 226 | Only an integration solution should be bothered creating a 'root context'. | ||
| 227 | |||
| 228 | =head2 new | ||
| 229 | |||
| 230 | my $span_context = SpanContext->new( | ||
| 231 | service_name => "MyFancyService", | ||
| 232 | resource_name => "/clients/{client_id}/contactdetails", | ||
| 233 | baggage_items => { $key => $value, .. }, | ||
| 234 | ); | ||
| 235 | |||
| 236 | Creates a new SpanContext object; | ||
| 237 | |||
| 238 | |||
| 239 | |||
| 240 | =head1 INSTANCE METHODS | ||
| 241 | |||
| 242 | Besides all methods available from the L<OpenTracing::Roles::SpanContext>, the | ||
| 243 | following are DataDog specific added methods. | ||
| 244 | |||
| 245 | =cut | ||
| 246 | |||
| 247 | |||
| 248 | |||
| 249 | =head2 C<service_type_is_web> | ||
| 250 | |||
| 251 | Returns a C<Bool> wether or not the L<service_type> is set to C<web>. | ||
| 252 | |||
| 253 | =head2 C<service_type_is_db> | ||
| 254 | |||
| 255 | Returns a C<Bool> wether or not the L<service_type> is set to C<db>. | ||
| 256 | |||
| 257 | =head2 C<service_type_is_cache> | ||
| 258 | |||
| 259 | Returns a C<Bool> wether or not the L<service_type> is set to C<cache>. | ||
| 260 | |||
| 261 | =head2 C<service_type_is_ciustom> | ||
| 262 | |||
| 263 | Returns a C<Bool> wether or not the L<service_type> is set to C<custom>. | ||
| 264 | |||
| 265 | =cut | ||
| 266 | |||
| 267 | |||
| 268 | |||
| 269 | =head1 CLONE METHODS | ||
| 270 | |||
| 271 | Since C<SpanContext> is supposed to be an in-mutable object, and there are some | ||
| 272 | occasions that DataDog settings need to added (i.e. a root span), there are a | ||
| 273 | few cloning methods provided: | ||
| 274 | |||
| 275 | =cut | ||
| 276 | |||
| 277 | |||
| 278 | |||
| 279 | =head2 C<with_service_name> | ||
| 280 | |||
| 281 | Creates a cloned object, with a new value for C<service_name>. | ||
| 282 | |||
| 283 | $span_context_new = $root_span->with_service_name( 'MyAwesomeApp' ); | ||
| 284 | |||
| 285 | =head3 Required Positional Parameter(s) | ||
| 286 | |||
| 287 | =over | ||
| 288 | |||
| 289 | =item C<service_name> | ||
| 290 | |||
| 291 | A C<NonEmptyString> where C<length <= 100>. | ||
| 292 | |||
| 293 | =back | ||
| 294 | |||
| 295 | =head3 Returns | ||
| 296 | |||
| 297 | A cloned C<DataDog::SpanContext> | ||
| 298 | |||
| 299 | =cut | ||
| 300 | |||
| 301 | 1 | ▶ | sub with_service_name { $_[0]->clone_with( service_name => $_[1] ) } |
|
Subroutine
with_service_name: called CC=1 SCAR=0.0
Pod
documented
| |||
| 302 | |||
| 303 | |||
| 304 | |||
| 305 | =head2 C<with_service_type> | ||
| 306 | |||
| 307 | Creates a cloned object, with a new value for C<service_type>. | ||
| 308 | |||
| 309 | $span_context_new = $root_span->with_service_type( 'cache' ); | ||
| 310 | |||
| 311 | =head3 Required Positional Parameter(s) | ||
| 312 | |||
| 313 | =over | ||
| 314 | |||
| 315 | =item C<service_type> | ||
| 316 | |||
| 317 | An enumeration on C<web>, C<db>, C<cache>, and C<custom> | ||
| 318 | |||
| 319 | =back | ||
| 320 | |||
| 321 | =head3 Returns | ||
| 322 | |||
| 323 | A cloned C<DataDog::SpanContext> | ||
| 324 | |||
| 325 | =cut | ||
| 326 | |||
| 327 | 1 | ▶ | sub with_service_type { $_[0]->clone_with( service_type => $_[1] ) } |
|
Subroutine
with_service_type: called CC=1 SCAR=0.0
Pod
documented
| |||
| 328 | |||
| 329 | |||
| 330 | |||
| 331 | =head2 C<with_resource_name> | ||
| 332 | |||
| 333 | Creates a cloned object, with a new value for C<resource_name>. | ||
| 334 | |||
| 335 | $span_context_new = $root_span->with_resource_name( 'clients/?/index.cgi' ); | ||
| 336 | |||
| 337 | =head3 Required Positional Parameter(s) | ||
| 338 | |||
| 339 | =over | ||
| 340 | |||
| 341 | =item C<resource_name> | ||
| 342 | |||
| 343 | A C<NonEmptyString> where C<length <= 5000>. | ||
| 344 | |||
| 345 | =back | ||
| 346 | |||
| 347 | =head3 Returns | ||
| 348 | |||
| 349 | A cloned C<DataDog::SpanContext> | ||
| 350 | |||
| 351 | =cut | ||
| 352 | |||
| 353 | 1 | ▶ | sub with_resource_name { $_[0]->clone_with( resource_name => $_[1] ) } |
|
Subroutine
with_resource_name: called CC=1 SCAR=0.0
Pod
documented
| |||
| 354 | |||
| 355 | |||
| 356 | |||
| 357 | =head2 C<with_environment> | ||
| 358 | |||
| 359 | Creates a cloned object, with a new value for C<environment>. | ||
| 360 | |||
| 361 | $span_context_new = $root_span->with_environment( 'development' ); | ||
| 362 | |||
| 363 | =head3 Required Positional Parameter(s) | ||
| 364 | |||
| 365 | =over | ||
| 366 | |||
| 367 | =item C<environment> | ||
| 368 | |||
| 369 | A C<NonEmptyString> where C<length <= 5000>. | ||
| 370 | |||
| 371 | =back | ||
| 372 | |||
| 373 | =head3 Returns | ||
| 374 | |||
| 375 | A cloned C<DataDog::SpanContext> | ||
| 376 | |||
| 377 | =cut | ||
| 378 | |||
| 379 | 1 | ▶ | sub with_environment { $_[0]->clone_with( environment => $_[1] ) } |
|
Subroutine
with_environment: called CC=1 SCAR=0.0
Pod
documented
| |||
| 380 | |||
| 381 | |||
| 382 | |||
| 383 | =head2 C<with_hostname> | ||
| 384 | |||
| 385 | Creates a cloned object, with a new value for C<hostname>. | ||
| 386 | |||
| 387 | $span_context_new = $root_span->with_hostname( 'web.01.host' ); | ||
| 388 | |||
| 389 | =head3 Required Positional Parameter(s) | ||
| 390 | |||
| 391 | =over | ||
| 392 | |||
| 393 | =item C<hostname> | ||
| 394 | |||
| 395 | A C<NonEmptyString> where C<length <= 5000>. | ||
| 396 | |||
| 397 | =back | ||
| 398 | |||
| 399 | =head3 Returns | ||
| 400 | |||
| 401 | A cloned C<DataDog::SpanContext> | ||
| 402 | |||
| 403 | =cut | ||
| 404 | |||
| 405 | 1 | ▶ | sub with_hostname { $_[0]->clone_with( hostname => $_[1] ) } |
|
Subroutine
with_hostname: called CC=1 SCAR=0.0
Pod
documented
| |||
| 406 | |||
| 407 | |||
| 408 | |||
| 409 | =head2 C<with_version> | ||
| 410 | |||
| 411 | Creates a cloned object, with a new value for C<hostname>. | ||
| 412 | |||
| 413 | $span_context_new = $root_span->with_version( 'v0.123.456' ); | ||
| 414 | |||
| 415 | =head3 Required Positional Parameter(s) | ||
| 416 | |||
| 417 | =over | ||
| 418 | |||
| 419 | =item C<version> | ||
| 420 | |||
| 421 | A C<NonEmptyString> where C<length <= 5000>. | ||
| 422 | |||
| 423 | =back | ||
| 424 | |||
| 425 | =head3 Returns | ||
| 426 | |||
| 427 | A cloned C<DataDog::SpanContext> | ||
| 428 | |||
| 429 | =cut | ||
| 430 | |||
| 431 | 1 | ▶ | sub with_version { $_[0]->clone_with( version => $_[1] ) } |
|
Subroutine
with_version: called CC=1 SCAR=0.0
Pod
documented
| |||
| 432 | |||
| 433 | |||
| 434 | |||
| 435 | =head1 ENVIRONMENT VARIABLES | ||
| 436 | |||
| 437 | For configuring DataDog Tracing there is support for the folllowing environment | ||
| 438 | variables: | ||
| 439 | |||
| 440 | |||
| 441 | |||
| 442 | =head2 C<DD_SERVICE_NAME> | ||
| 443 | |||
| 444 | The name of a set of processes that do the same job. Used for grouping stats for | ||
| 445 | your application. | ||
| 446 | |||
| 447 | B<default:> I<none> | ||
| 448 | |||
| 449 | |||
| 450 | |||
| 451 | =head2 C<DD_ENV> | ||
| 452 | |||
| 453 | Your application environment (for example, production, staging). | ||
| 454 | |||
| 455 | B<default:> I<none> | ||
| 456 | |||
| 457 | |||
| 458 | |||
| 459 | =head2 C<DD_HOSTNAME> | ||
| 460 | |||
| 461 | Manually set the hostname to use for metrics if autodetection fails, or when | ||
| 462 | running the Datadog Cluster Agent. | ||
| 463 | |||
| 464 | |||
| 465 | |||
| 466 | =head2 C<DD_VERSION> | ||
| 467 | |||
| 468 | Your application version (for example, 2.5, 202003181415, 1.3-alpha). | ||
| 469 | |||
| 470 | B<default:> I<none> | ||
| 471 | |||
| 472 | |||
| 473 | |||
| 474 | =head1 SEE ALSO | ||
| 475 | |||
| 476 | =over | ||
| 477 | |||
| 478 | =item L<OpenTracing::Implementation::DataDog> | ||
| 479 | |||
| 480 | Sending traces to DataDog using Agent. | ||
| 481 | |||
| 482 | =item L<OpenTracing::Role::SpanContext> | ||
| 483 | |||
| 484 | Role for OpenTracing Implementations. | ||
| 485 | |||
| 486 | =back | ||
| 487 | |||
| 488 | |||
| 489 | |||
| 490 | =head1 AUTHOR | ||
| 491 | |||
| 492 | Theo van Hoesel <tvanhoesel@perceptyx.com> | ||
| 493 | |||
| 494 | |||
| 495 | |||
| 496 | =head1 COPYRIGHT AND LICENSE | ||
| 497 | |||
| 498 | 'OpenTracing::Implementation::DataDog' | ||
| 499 | is Copyright (C) 2019 .. 2021, Perceptyx Inc | ||
| 500 | |||
| 501 | This library is free software; you can redistribute it and/or modify it under | ||
| 502 | the terms of the Artistic License 2.0. | ||
| 503 | |||
| 504 | This package is distributed in the hope that it will be useful, but it is | ||
| 505 | provided "as is" and without any express or implied warranties. | ||
| 506 | |||
| 507 | For details, see the full text of the license in the file LICENSE. | ||
| 508 | |||
| 509 | |||
| 510 | =cut | ||
| 511 | |||
| 512 | 26 | 1; | |