Skip to content

Commit

Permalink
feat: add HTTP_ROUTE attribute to http incoming metrics if present (#…
Browse files Browse the repository at this point in the history
…3581)

* feat: add HTTP_ROUTE attribute to http incoming metrics if present

* fix: lint errors

* docs: add changelog entry

* fix: test name

Co-authored-by: Marc Pichler <[email protected]>

---------

Co-authored-by: Marc Pichler <[email protected]>
  • Loading branch information
hermogenes and pichlermarc authored Feb 9, 2023
1 parent 65e83d4 commit e0d6e14
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
2 changes: 2 additions & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ All notable changes to experimental packages in this project will be documented

### :rocket: (Enhancement)

* feat: add HTTP_ROUTE attribute to http incoming metrics if present [#3581](https://github.com/open-telemetry/opentelemetry-js/pull/3581) @hermogenes

### :bug: (Bug Fix)

* fix(prometheus-exporter): add possibility to respond to errors returned by `server.listen()` [#3552](https://github.com/open-telemetry/opentelemetry-js/pull/3402) @pichlermarc
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,10 @@ export const getIncomingRequestMetricAttributesOnResponse = (
spanAttributes[SemanticAttributes.HTTP_STATUS_CODE];
metricAttributes[SemanticAttributes.NET_HOST_PORT] =
spanAttributes[SemanticAttributes.NET_HOST_PORT];
if (spanAttributes[SemanticAttributes.HTTP_ROUTE] !== undefined) {
metricAttributes[SemanticAttributes.HTTP_ROUTE] =
spanAttributes[SemanticAttributes.HTTP_ROUTE];
}
return metricAttributes;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
SpanKind,
TraceFlags,
context,
Attributes,
} from '@opentelemetry/api';
import { BasicTracerProvider, Span } from '@opentelemetry/sdk-trace-base';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
Expand Down Expand Up @@ -320,6 +321,31 @@ describe('Utility', () => {
assert.deepEqual(attributes[SemanticAttributes.HTTP_ROUTE], undefined);
});
});

describe('getIncomingRequestMetricAttributesOnResponse()', () => {
it('should correctly add http_route if span has it', () => {
const spanAttributes: Attributes = {
[SemanticAttributes.HTTP_ROUTE]: '/user/:id',
};
const metricAttributes =
utils.getIncomingRequestMetricAttributesOnResponse(spanAttributes);

assert.deepStrictEqual(
metricAttributes[SemanticAttributes.HTTP_ROUTE],
'/user/:id'
);
});

it('should skip http_route if span does not have it', () => {
const spanAttributes: Attributes = {};
const metricAttributes =
utils.getIncomingRequestMetricAttributesOnResponse(spanAttributes);
assert.deepEqual(
metricAttributes[SemanticAttributes.HTTP_ROUTE],
undefined
);
});
});
// Verify the key in the given attributes is set to the given value,
// and that no other HTTP Content Length attributes are set.
function verifyValueInAttributes(
Expand Down

0 comments on commit e0d6e14

Please sign in to comment.