Skip to content

feat(tracer): add isTraceSampled method #1435

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

Merged
merged 12 commits into from
May 4, 2023
Merged

Conversation

icholy
Copy link
Contributor

@icholy icholy commented May 4, 2023

Description of your changes

This change adds a new isTraceSampled method to Tracer. It lets users find out if the current trace is sampled or not. This information may be included in structured logs along with the Trace ID.

How to verify this change

Set the Sampled=1 flag in the _X_AMZN_TRACE_ID environment variable and assert that isTraceSampled returns true.
Example: Root=1-5759e988-bd862e3fe1be46a994272793;Parent=557abcec3ee5a047;Sampled=1

Related issues, RFCs

Issue number: #1378

Checklist

  • My changes meet the tenets criteria
  • I have performed a self-review of my own code
  • I have commented my code where necessary, particularly in areas that should be flagged with a TODO, or hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have made corresponding changes to the examples
  • My changes generate no new warnings
  • The code coverage hasn't decreased
  • I have added tests that prove my change is effective and works
  • New and existing unit tests pass locally and in Github Actions
  • Any dependent changes have been merged and published
  • The PR title follows the conventional commit semantics

Breaking change checklist

Is it a breaking change?: NO


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

@pull-request-size pull-request-size bot added the size/L PRs between 100-499 LOC label May 4, 2023
@dreamorosi dreamorosi linked an issue May 4, 2023 that may be closed by this pull request
2 tasks
@dreamorosi
Copy link
Contributor

Hi @icholy thank you for taking the time to open this PR!

Please allow us some time to go through the changes and review the PR.

@dreamorosi dreamorosi self-requested a review May 4, 2023 18:15
icholy and others added 2 commits May 4, 2023 15:31
@icholy
Copy link
Contributor Author

icholy commented May 4, 2023

@dreamorosi seems like you have a very specific implementation in mind. I think it would be easier for both of us if you just added your commits on top of mine.

@dreamorosi
Copy link
Contributor

Hey, sorry, my bad.

Should have left a comment instead of getting into the suggestions.

I'll delete the suggestion, and please allow me to elaborate why I made them.

At the moment, based on the discussion we had some time ago in the linked issue and also the existing implementation, we are relying on the order of the fields inside the environment variable.

In the past few weeks, especially after #1356, I think it'd be best to be more defensive when parsing the content of this environment variable and make sure we are not relying on the order of the fields or on the fact that some delimiter are present. Hence my suggestions.

My proposal would be to have the getXrayTraceData method return an object that has two fields root and sampled, both of undefined | string type.

In the body of the method, we could make a couple of additional checks while parsing:

  • Check that xRayTraceEnv has a value & that expected delimiter (;) is present, if not, return undefined.
  • Find the field that starts with Root=, if found: keep the value after =, if not consider it undefined
  • Find the field that starts with Sample=, if found: keep the value after =, if not consider it undefined
  • Return { root, sampled }

This way, the methods consuming the trace data can be more streamlined.

@icholy
Copy link
Contributor Author

icholy commented May 4, 2023

Your logic makes sense, but personally I think you're giving getXrayTraceData too much responsibility. It should only understand parsing the key/value syntax. IE: getXrayTraceData(): Record<string, string> | undefined .

So parsing Root=1-5759e988-bd862e3fe1be46a994272793;Parent=557abcec3ee5a047;Sampled=1 would result in an object like this:

{
  Root: "1-5759e988-bd862e3fe1be46a994272793",
  Parent: "557abcec3ee5a047",
  Sampled: "1"
}

For backwards compatibility, it would place the whole string in the Root key if it doesn't contain key/value pairs.

Then the getXrayTraceId and getXrayTraceSampled contain logic for interpreting the parsed data. So getXrayTraceData deals with syntax, and getXrayTraceId & getXrayTraceSampled deal with semantics.

@dreamorosi
Copy link
Contributor

You're right, and thanks for hearing me out.

Let's go with your proposal, after this I'm looking forward to merge it right away.

@icholy
Copy link
Contributor Author

icholy commented May 4, 2023

@dreamorosi ready for another review round.

@icholy

This comment was marked as resolved.

@dreamorosi
Copy link
Contributor

I think there's one line/code branch not being covered by the tests

@icholy icholy requested a review from dreamorosi May 4, 2023 21:18
Copy link
Contributor

@dreamorosi dreamorosi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for contributing the feature and for addressing the comments during the review!

@dreamorosi dreamorosi merged commit 194bbd3 into aws-powertools:main May 4, 2023
@github-actions github-actions bot added the feature PRs that introduce new features or minor changes label May 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature PRs that introduce new features or minor changes size/L PRs between 100-499 LOC
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feature request: Add isTraceSampled method to Tracer
2 participants