Skip to content

[bug-fix] Make it possible to grant xray traces with an http request in Nuxt3 #616

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

hibira
Copy link

@hibira hibira commented Mar 19, 2025


issue #615

Resolves an issue where X_AMZN_TRACE_ID is not assigned to the http request header in nuxt3 (ofetch).
The cause is that in AddamznTraceIdHeaderToInit, a trace has been added as a property like init.headers [X_AMZN_TRACE_ID], but it is actually a Header object, so it doesn't work properly.
In contrast to this, in the case of an object that has a set method, set is used to assign a trace ID.

Before fix
Image

After fix
image

After build

export var addAmznTraceIdHeaderToInit = function (init, traceId, segmentId) {
    if (!init.headers) {
        init.headers = {};
    }
    if (init.headers.set) {
        init.headers.set(X_AMZN_TRACE_ID, getAmznTraceIdHeaderValue(traceId, segmentId));
    } else {
        init.headers[X_AMZN_TRACE_ID] = getAmznTraceIdHeaderValue(traceId, segmentId);
    }
};

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

Copy link
Contributor

@limhjgrace limhjgrace left a comment

Choose a reason for hiding this comment

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

Thanks for the contribution! The changes LGTM but I'd like to increase test coverage for this change since it could affect all RUM events sent by the client. Can we ensure unit, integ, smoke test cover this change?

@hibira
Copy link
Author

hibira commented Mar 30, 2025

Hi @limhjgrace .
Thanks for confirming.
I've added tests, so check them out.

Unit Tests

Added tests in src/plugins/utils/tests/http-utils.test.ts to verify the addAmznTraceIdHeaderToInit function correctly handles both types of headers objects:

  1. Test for headers with set method (Nuxt3 case):
    • Verifies that when a headers object has a set method, the function uses this method to add the trace header

  2. Test for headers without set method (standard case):
    • Verifies that when a headers object doesn't have a set method, the function adds the trace header as a property

Integration Tests

Added a test in src/plugins/event-plugins/tests/FetchPlugin.test.ts that simulates the Nuxt3 environment:
• Tests that when fetch is called with headers that have a set method, the trace header is correctly added
• Verifies that the X-Ray trace event is properly recorded

HTML Test Page Update

Added a new button and handler to app/http_fetch_event.html that:
• Creates a Headers object (which has a set method by default)
• Makes a fetch request using this Headers object
• Allows manual verification of the trace header addition

Smoke Test

Added a test in src/smoke-test/dataplane-integ.spec.ts that mocks the Nuxt3 environment by:
• Overriding the fetch API to use Headers objects
• Triggering a fetch request
• Verifying the X-Ray trace ID header is correctly added

All tests pass successfully, confirming that the fix works correctly in all scenarios, particularly in Nuxt3 applications where headers objects have a set method. The implementation is robust and doesn't
affect existing functionality.

@hibira hibira requested a review from limhjgrace April 11, 2025 01:42
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