Skip to content

Feature request: Add isTraceSampled method to Tracer #1378

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

Closed
2 tasks done
icholy opened this issue Mar 21, 2023 · 12 comments · Fixed by #1435
Closed
2 tasks done

Feature request: Add isTraceSampled method to Tracer #1378

icholy opened this issue Mar 21, 2023 · 12 comments · Fixed by #1435
Assignees
Labels
completed This item is complete and has been merged/shipped feature-request This item refers to a feature request for an existing or new utility tracer This item relates to the Tracer Utility

Comments

@icholy
Copy link
Contributor

icholy commented Mar 21, 2023

Use case

I'd like a attribute in my logs to indicate whether or not a request has been sampled. Something like this:

{
  "level": "ERROR",
  "msg": "something went wrong",
  "trace_id": "1-6419d889-20172e50462fac2c7c481454",
  "trace_sampled": true
}

I can populate the trace_id using Tracer#getRootXrayTraceId , but there's no way to find out if the trace is sampled.

Solution/User Experience

A new isTraceSampled method on the Tracer class.

log({
  level: "ERROR",
  msg: "something went wrong",
  trace_id: tracer.getRootXrayTraceId(),
  trace_sampled: tracer.isTraceSampled(),
})

Alternative solutions

Implement my own isTraceSampled function which parses the _X_AMZN_TRACE_ID header value.

Acknowledgment

Future readers

Please react with 👍 and your use case to help us understand customer demand.

@icholy icholy added triage This item has not been triaged by a maintainer, please wait feature-request This item refers to a feature request for an existing or new utility labels Mar 21, 2023
@icholy
Copy link
Contributor Author

icholy commented Mar 21, 2023

I'd be willing to implement this if it's accepted.

@dreamorosi dreamorosi added logger This item relates to the Logger Utility discussing The issue needs to be discussed, elaborated, or refined and removed triage This item has not been triaged by a maintainer, please wait labels Mar 22, 2023
@dreamorosi dreamorosi added tracer This item relates to the Tracer Utility and removed logger This item relates to the Logger Utility labels Mar 22, 2023
@dreamorosi
Copy link
Contributor

Hi @icholy, thank you for taking the time to open this feature request.

I think the idea is valuable and I'm open to consider it.

Before going to the implementation I'd like us to spend some time here in this issue discussing a potential implementation.

Those are some points we should discuss and agree on before moving to a PR:

  • Return type of the method, considering that the Trace data might be undefined
  • Where/how this new method should be implemented (I see some common logic with this existing method)
  • Agree on a name for the method that conveys its meaning
  • What type of changes should be made to the documentation to articulate the feature/method

If you're still interested in contributing this function, I'd be happy to collaborate on a proposal.

@icholy
Copy link
Contributor Author

icholy commented Mar 22, 2023

  • Return type of the method, considering that the Trace data might be undefined:
    • boolean | undefined
      • I think we could get away with just boolean. If there's no trace, then it's not sampled.
        • Doesn't matter much to me since if (tracer.isTraceSampled()) { will work either way.
  • Where/how this new method should be implemented (I see some common logic with this existing method)
    • Add a EnvironmentVariableService#getXrayTraceSampled method.
    • Use that method inside the Tracer#isTraceSampled
  • Agree on a name for the method that conveys its meaning:
    • I feel like isTraceSampled is pretty clear. If you disagree, here are some other options:
      • isSampled
      • isTraceSampled
      • getTraceSampled
      • getRootXrayTraceSampled
  • What type of changes should be made to the documentation to articulate the feature/method
    • Not sure

@dreamorosi
Copy link
Contributor

  • Return type of the method, considering that the Trace data might be undefined:

    • boolean | undefined

      • I think we could get away with just boolean. If there's no trace, then it's not sampled.
        • Doesn't matter much to me since if (tracer.isTraceSampled()) { will work either way.

I agree, let's stick to boolean only and assume that a missing value equals to false as it means no tracing can happen.

  • Where/how this new method should be implemented (I see some common logic with this existing method)
    • Add a EnvironmentVariableService#getXrayTraceSampled method.
    • Use that method inside the Tracer#isTraceSampled

I agree with the second point.

Regarding the first one, we are in a good direction but I would try to make an effort to not have duplicate logic between the existing getXrayTraceSampled and new isXrayTraceSampled method.

The current implementation for the former is:

public getXrayTraceId(): string | undefined {
  const xRayTraceId = this.get(this.xRayTraceIdVariable);

  if (xRayTraceId === '') return undefined;

  return xRayTraceId.split(';')[0].replace('Root=', '');
}

Given that both methods now need to access the tracing header, I would consider extracting part of the logic and reuse it for both, kind of like:

private getXrayTraceData: string[] {
  const xRayTraceData = this.get(this.xRayTraceIdVariable);
  if (xRayTraceData === '') return undefined;

  return xRayTraceData.split(';');
}

public getXrayTraceId(): string | undefined {
  const xRayTraceData = this.getXrayTraceData();

  if (xRayTraceData.length === 0) return undefined;

  return xRayTraceData[0].replace('Root=', '');
}

public isXrayTraceSampled(): boolean {
  const xRayTraceData = this.getXrayTraceData();

  // TODO: implement
}
  • Agree on a name for the method that conveys its meaning:
    • I feel like isTraceSampled is pretty clear. If you disagree, here are some other options:
      • isSampled
      • isTraceSampled
      • getTraceSampled
      • getRootXrayTraceSampled

Okay. I agree that isTraceSampled is fairly clear and aligns with the fact that we are expecting a boolean value, so I think we can safely rule out the ones with get.

With that said, I think it would be more consistent to have it called isXrayTraceSampled instead.

  • What type of changes should be made to the documentation to articulate the feature/method

    • Not sure

I'm available to help with the editing during the review phase, but the PR for this feature will have to include changes to the corresponding docs as well.

Generally speaking, I think the existing section currently titled "Access AWS X-Ray Root Trace ID" should be changed to something like "Access AWS X-Ray Trace Data", and its content should reflect that:

  • Now there are two methods: getRootXrayTraceId() and isXrayTraceSampled()
  • Explain that these two methods can be used to access the X-Ray trace data present in the tracing header
  • Keep the existing content about getRootXrayTraceId()
  • Add a new piece that discusses isXrayTraceSampled(), touches on when it can be useful, shows a piece of code (i.e. similar to the one in your OP)

If you agree with the points/direction, let me know and I'll assign the issue to you and you can start working on a PR.

@dreamorosi dreamorosi moved this from Ideas to Backlog in AWS Lambda Powertools for TypeScript Mar 23, 2023
@dreamorosi dreamorosi added confirmed The scope is clear, ready for implementation and removed discussing The issue needs to be discussed, elaborated, or refined labels Mar 23, 2023
@icholy
Copy link
Contributor Author

icholy commented Mar 26, 2023

Sounds good to me.

@dreamorosi dreamorosi moved this from Backlog to Working on it in AWS Lambda Powertools for TypeScript Mar 27, 2023
@dreamorosi
Copy link
Contributor

Hi @icholy, I just wanted to check if you were still working on this feature.

If not, please let me know so I can put it back on the backlog.

@icholy
Copy link
Contributor Author

icholy commented Apr 14, 2023

@dreamorosi I'm on holiday. I'll be back next week.

@dreamorosi dreamorosi added the need-response This item requires a response from a customer and will considered stale after 2 weeks label Apr 27, 2023
@dreamorosi dreamorosi moved this from Working on it to Backlog in AWS Lambda Powertools for TypeScript Apr 27, 2023
@dreamorosi dreamorosi moved this from Backlog to Ideas in AWS Lambda Powertools for TypeScript Apr 27, 2023
@dreamorosi dreamorosi added on-hold This item is on-hold and will be revisited in the future discussing The issue needs to be discussed, elaborated, or refined and removed confirmed The scope is clear, ready for implementation on-hold This item is on-hold and will be revisited in the future labels Apr 27, 2023
@icholy
Copy link
Contributor Author

icholy commented May 4, 2023

Working on this now.

@dreamorosi dreamorosi moved this from Ideas to Working on it in AWS Lambda Powertools for TypeScript May 4, 2023
@dreamorosi dreamorosi removed the need-response This item requires a response from a customer and will considered stale after 2 weeks label May 4, 2023
@dreamorosi dreamorosi linked a pull request May 4, 2023 that will close this issue
11 tasks
@github-project-automation github-project-automation bot moved this from Working on it to Coming soon in AWS Lambda Powertools for TypeScript May 4, 2023
@github-actions
Copy link
Contributor

github-actions bot commented May 4, 2023

⚠️ COMMENT VISIBILITY WARNING ⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

@dreamorosi dreamorosi removed the discussing The issue needs to be discussed, elaborated, or refined label May 4, 2023
@github-actions github-actions bot added the pending-release This item has been merged and will be released soon label May 4, 2023
@icholy
Copy link
Contributor Author

icholy commented Jun 6, 2023

@dreamorosi will there be a release soon?

@dreamorosi
Copy link
Contributor

Yes, I'm planning a release this week. It'll include the feature

@github-actions
Copy link
Contributor

github-actions bot commented Jun 9, 2023

This is now released under v1.9.0 version!

@dreamorosi dreamorosi added completed This item is complete and has been merged/shipped and removed pending-release This item has been merged and will be released soon labels Jun 9, 2023
@dreamorosi dreamorosi moved this from Coming soon to Shipped in AWS Lambda Powertools for TypeScript Jun 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
completed This item is complete and has been merged/shipped feature-request This item refers to a feature request for an existing or new utility tracer This item relates to the Tracer Utility
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants