Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jun 2, 2025

The trackRequest method was not correctly handling HTTP method extraction from request names and was ignoring user-provided request IDs. This caused issues where:

  1. HTTP Method Issue: The method was hardcoded to "HTTP" instead of extracting the actual HTTP method from the request name
  2. ID Loss: User-provided request IDs were completely ignored

Example of the problem:

client.trackRequest({
  id: '7d2b68c6-5b3d-479d-92f9-ab680847acfd',
  name: 'GET /',
  duration: 6,
  success: false,
  resultCode: '304',
  url: 'http://localhost:4001/',
  time: new Date('2025-05-29T07:50:13.306Z'),
  properties: { }
});

Before this fix:

  • HTTP method attribute: "HTTP" (hardcoded)
  • Request ID: Lost/ignored, span generates its own ID

After this fix:

  • HTTP method attribute: "GET" (extracted from name)
  • Request ID: Preserved as request.id attribute with value "7d2b68c6-5b3d-479d-92f9-ab680847acfd"

Changes Made

  • Added _extractHttpMethod() private method that parses request names following "METHOD path" pattern
  • Modified trackRequest() to use extracted HTTP method instead of hardcoding "HTTP"
  • Added preservation of user-provided request ID as a request.id attribute
  • Maintains backward compatibility with fallback to "HTTP" when no method pattern is found
  • Added comprehensive tests covering various HTTP methods and edge cases

Test Coverage

Added 4 new test cases:

  • HTTP method extraction from request names (GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS)
  • Request ID preservation
  • Fallback behavior for non-HTTP method names
  • Proper handling when no ID is provided

All 25 existing tests continue to pass, ensuring no regression.

Fixes #1432.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • global.livediagnostics.monitor.azure.com
    • Triggering command: node /home/REDACTED/work/ApplicationInsights-node.js/ApplicationInsights-node.js/node_modules/.bin/mocha ./out/test/unitTests --recursive (dns block)
    • Triggering command: node /home/REDACTED/work/ApplicationInsights-node.js/ApplicationInsights-node.js/node_modules/.bin/mocha ./out/test/unitTests/shim/telemetryClient.tests.js (dns block)
    • Triggering command: node /home/REDACTED/work/ApplicationInsights-node.js/ApplicationInsights-node.js/node_modules/.bin/mocha ./out/test/unitTests/shim/telemetryClient.tests.js --grep trackRequest with HTTP method in name (dns block)
  • westus-0.in.applicationinsights.azure.com
    • Triggering command: node /home/REDACTED/work/ApplicationInsights-node.js/ApplicationInsights-node.js/node_modules/.bin/mocha ./out/test/unitTests --recursive (dns block)
    • Triggering command: node /home/REDACTED/work/ApplicationInsights-node.js/ApplicationInsights-node.js/node_modules/.bin/mocha ./out/test/unitTests/shim/telemetryClient.tests.js (dns block)
    • Triggering command: node /home/REDACTED/work/ApplicationInsights-node.js/ApplicationInsights-node.js/node_modules/.bin/mocha ./out/test/unitTests/shim/telemetryClient.tests.js --grep trackRequest with HTTP method in name (dns block)

If you need me to access, download, or install something from one of these locations, you can either:


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits June 2, 2025 17:33
Co-authored-by: JacksonWeber <47067795+JacksonWeber@users.noreply.github.com>
Copilot AI changed the title [WIP] trackRequest messes up the values Fix trackRequest HTTP method extraction and ID preservation Jun 2, 2025
Copilot AI requested a review from JacksonWeber June 2, 2025 17:35
@JacksonWeber JacksonWeber marked this pull request as ready for review June 6, 2025 22:41

// Preserve user-provided request ID
if (telemetry.id) {
attributes["request.id"] = telemetry.id;
Copy link
Member

Choose a reason for hiding this comment

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

Where is the code that is auto generating this ID when is not provided?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The OpenTelemetry SDK automatically generates a unique span ID when creating spans - this happens in the OpenTelemetry library itself, not in our code. The request.id attribute is separate from the span ID and preserves the user-provided request ID as additional metadata. When no ID is provided by the user, we simply don't set the request.id attribute (line 183-185 has the conditional check).

Copy link
Member

Choose a reason for hiding this comment

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

Even if this code actually add the information to the Span, this value will end in a different place in the actual telemetry when the span is exported in Azure Monitor OpenTelemetry Exporter package, so this is not exactly the correct fix

Copy link
Member

Choose a reason for hiding this comment

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

Is it possible to pass the span id in OpenTelemetry code?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're absolutely right about the attribute approach not working correctly with Azure Monitor. I've updated the implementation to use OpenTelemetry's span context instead. For UUID-format IDs, I now convert them to valid span IDs and use trace.setSpanContext() to set a custom span context before creating the span. For non-UUID IDs, I fall back to the attribute approach for backward compatibility. This should properly preserve the request ID in the span's context where Azure Monitor can access it correctly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The telemetry name is not being overridden - it's preserved as-is on line 193 where telemetry.name is passed to startSpan(). We only extract the HTTP method from the name to correctly set the http.method attribute (previously hardcoded as "HTTP"). The span name stays exactly as the user provided it. The original issue was that the HTTP method attribute was always "HTTP" instead of the actual method like "GET", "POST", etc.

Regarding request IDs: After trying the span context approach, I've simplified the implementation to focus on the HTTP method extraction fix, which was the primary issue. The ID preservation requires coordination with the Azure Monitor exporter and would be better addressed in a separate effort.

attributes[SEMATTRS_HTTP_METHOD] = "HTTP";

// Extract HTTP method from request name if it follows "METHOD path" pattern
const httpMethod = this._extractHttpMethod(telemetry.name);
Copy link
Member

Choose a reason for hiding this comment

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

telemetry name could be anything that customer provide, not necessary an HTTP method, issue associated is complaining about the code changing the value they provided, where is the actual name being overridden?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The telemetry name is not being overridden - it's preserved as-is on line 193 where telemetry.name is passed to startSpan(). We only extract the HTTP method from the name to correctly set the http.method attribute (previously hardcoded as "HTTP"). The span name stays exactly as the user provided it. The original issue was that the HTTP method attribute was always "HTTP" instead of the actual method like "GET", "POST", etc.

Copy link
Member

Choose a reason for hiding this comment

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

is name passed in startSpan method changed at some point in OpenTelemetry code?

Copy link
Contributor

Choose a reason for hiding this comment

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

@copilot answer the question above

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, the name passed to startSpan() is not changed by OpenTelemetry. The user-provided telemetry.name becomes the span name and remains unchanged throughout the OpenTelemetry processing pipeline. The Azure Monitor exporter reads span.name directly and uses it for telemetry without modification. Our implementation correctly preserves the original request name while only extracting the HTTP method from it to set the http.method attribute.

Copy link
Contributor Author

Copilot AI commented Jun 11, 2025

Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • global.livediagnostics.monitor.azure.com
    • Triggering command: node /home/REDACTED/work/ApplicationInsights-node.js/ApplicationInsights-node.js/node_modules/.bin/mocha ./out/test/unitTests/shim/telemetryClient.tests.js --grep trackRequest (dns block)
    • Triggering command: node /home/REDACTED/work/ApplicationInsights-node.js/ApplicationInsights-node.js/node_modules/.bin/mocha ./out/test/unitTests/shim/telemetryClient.tests.js (dns block)
  • westus-0.in.applicationinsights.azure.com
    • Triggering command: node /home/REDACTED/work/ApplicationInsights-node.js/ApplicationInsights-node.js/node_modules/.bin/mocha ./out/test/unitTests/shim/telemetryClient.tests.js --grep trackRequest (dns block)
    • Triggering command: node /home/REDACTED/work/ApplicationInsights-node.js/ApplicationInsights-node.js/node_modules/.bin/mocha ./out/test/unitTests/shim/telemetryClient.tests.js (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

… request ID handling

Co-authored-by: hectorhdzg <39923391+hectorhdzg@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jun 11, 2025

Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • global.livediagnostics.monitor.azure.com
    • Triggering command: node /home/REDACTED/work/ApplicationInsights-node.js/ApplicationInsights-node.js/node_modules/.bin/mocha ./out/test/unitTests --recursive (dns block)
    • Triggering command: node /home/REDACTED/work/ApplicationInsights-node.js/ApplicationInsights-node.js/node_modules/.bin/mocha ./out/test/unitTests/shim/telemetryClient.tests.js --grep trackRequest (dns block)
    • Triggering command: node /home/REDACTED/work/ApplicationInsights-node.js/ApplicationInsights-node.js/node_modules/.bin/mocha ./out/test/unitTests/shim/telemetryClient.tests.js --grep trackRequest with HTTP method in name (dns block)
  • westus-0.in.applicationinsights.azure.com
    • Triggering command: node /home/REDACTED/work/ApplicationInsights-node.js/ApplicationInsights-node.js/node_modules/.bin/mocha ./out/test/unitTests/shim/telemetryClient.tests.js --grep trackRequest (dns block)
    • Triggering command: node /home/REDACTED/work/ApplicationInsights-node.js/ApplicationInsights-node.js/node_modules/.bin/mocha ./out/test/unitTests/shim/telemetryClient.tests.js --grep trackRequest with HTTP method in name (dns block)
    • Triggering command: node /home/REDACTED/work/ApplicationInsights-node.js/ApplicationInsights-node.js/node_modules/.bin/mocha ./out/test/unitTests --recursive (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Copy link
Contributor Author

Copilot AI commented Jun 11, 2025

Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details.

Warning

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • global.livediagnostics.monitor.azure.com
    • Triggering command: node /home/REDACTED/work/ApplicationInsights-node.js/ApplicationInsights-node.js/node_modules/.bin/mocha ./out/test/unitTests --recursive (dns block)
  • westus-0.in.applicationinsights.azure.com
    • Triggering command: node /home/REDACTED/work/ApplicationInsights-node.js/ApplicationInsights-node.js/node_modules/.bin/mocha ./out/test/unitTests --recursive (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

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.

trackRequest messes up the values

3 participants