Skip to content

Commit

Permalink
Force ID fields to be numeric when JSON-encoded
Browse files Browse the repository at this point in the history
  • Loading branch information
s-nez committed Jul 29, 2024
1 parent 9d1368c commit b8ab0a8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
23 changes: 19 additions & 4 deletions lib/OpenTracing/Implementation/DataDog/Client.pm
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ use Carp;
use HTTP::Request ();
use JSON::MaybeXS qw(JSON);
use LWP::UserAgent;
use Math::BigInt;
use PerlX::Maybe qw/maybe provided/;
use Regexp::Common qw/URI/;
use Scalar::Util qw/blessed/;
use Types::Standard qw/ArrayRef Bool Enum HasMethods Maybe Str/;
use Types::Common::Numeric qw/IntRange/;

Expand Down Expand Up @@ -437,7 +439,7 @@ sub to_struct {
my $span = shift;

my $context = $span->get_context();

my %meta_data = (
_fixup_span_tags( $span->get_tags ),
$context->get_baggage_items,
Expand All @@ -451,8 +453,8 @@ sub to_struct {
if %meta_data;

my $data = {
trace_id => $context->trace_id,
span_id => $context->span_id,
trace_id => _cast_to_bigint($context->trace_id),
span_id => _cast_to_bigint($context->span_id),
resource => $context->get_resource_name,
service => $context->get_service_name,

Expand All @@ -473,7 +475,7 @@ sub to_struct {
duration => nano_seconds( $span->duration() ),

maybe
parent_id => $span->get_parent_span_id(),
parent_id => _cast_to_bigint($span->get_parent_span_id()),

provided _is_with_errors( $span ),
error => 1,
Expand Down Expand Up @@ -769,6 +771,19 @@ sub _span_buffer_threshold_reached {
return $self->_span_buffer_size >= $self->span_buffer_threshold
}

# _cast_to_bigint
#
# Returns the argument as a Math::BigInt object unless it is undef or already an object
#
sub _cast_to_bigint {
my $number = shift;

return unless defined $number;
return $number if blessed($number);

return Math::BigInt->new($number);
}



# DEMOLISH
Expand Down
6 changes: 3 additions & 3 deletions t/OpenTracing/Implementation/DataDog/Client/11_to_struct.t
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ my $struct = Client->new->to_struct( $test_span );

cmp_deeply(
$struct => {
trace_id => 87359,
span_id => 49603,
trace_id => Math::BigInt->new(87359),
span_id => Math::BigInt->new(49603),
type => "custom",
service => "srvc name",
resource => "rsrc name",
env => "test envr",
hostname => 'test host',
version => 'test vers',
parent_id => 54365,
parent_id => Math::BigInt->new(54365),
name => "oprt name",
start => 52750000000, # nano seconds
duration => 30750000000, # nano seconds
Expand Down

0 comments on commit b8ab0a8

Please sign in to comment.