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

Force ID fields to be numeric when JSON-encoded #38

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

s-nez
Copy link
Contributor

@s-nez s-nez commented Jul 29, 2024

The DataDog agent requires all ID fields to be properly numeric (they can't be quoted as strings, even if the content is only numbers). This change converts the ID fields into Math::BigInt unless they are already objects (if they are objects, they either are already Math::BigInt or should handle serialization on their own).

@vanHoesel
Copy link
Contributor

Right, let me think a bit...

these ID's SHOULD already be a InstanceOf['Math::BigInt'] object, as stated in SpanContext. And those are default => sub{ random_bigint() } (which is a wrapper)

if that is not the case (thanks to should) and not tested during runtime, 'someone' must have passed in the incorrect ID... which is afaik only possible at propagation time.

I rather see this fixed there than fix it afterwards with this bandage.

In other words, if Perl would have been a strongly typed language, this would never had occurred and raise other sorts of exceptions.

@vanHoesel
Copy link
Contributor

vanHoesel commented Jul 29, 2024

Another, possible better place to fix this, is inside SpanContext and coerce normal integers to Math::BigInt. That way, any other that might want to extract ID's from a hash, will get the right result too.

inside SpanContext:

has '+trace_id' => (
    is =>'ro',
    isa => InstanceOf['Math::BigInt']->plus_coercions(
        Any , sub { Math::BigInt->new($_) },
    ),
    coerce => 1,
    default => sub{ random_bigint() }
);

@s-nez
Copy link
Contributor Author

s-nez commented Jan 17, 2025

Yes, this approach is cleaner. I updated the attributes to coerce into BigInt and added modifiers for the clone methods to make sure their ids are also the right type (cloning doesn't go through constructors in OpenTracing::Role::SpanContext).

@vanHoesel
Copy link
Contributor

Good catch!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants