diff --git a/experimental/packages/opentelemetry-instrumentation-http/README.md b/experimental/packages/opentelemetry-instrumentation-http/README.md index 530205ac775..c8965121f20 100644 --- a/experimental/packages/opentelemetry-instrumentation-http/README.md +++ b/experimental/packages/opentelemetry-instrumentation-http/README.md @@ -55,6 +55,7 @@ Http instrumentation has few options available to choose from. You can set the f | Options | Type | Description | | ------- | ---- | ----------- | | [`applyCustomAttributesOnSpan`](https://github.com/open-telemetry/opentelemetry-js/blob/main/experimental/packages/opentelemetry-instrumentation-http/src/types.ts#L91) | `HttpCustomAttributeFunction` | Function for adding custom attributes | +| [`applyCustomAttributesOnMetric`](https://github.com/open-telemetry/opentelemetry-js/blob/main/experimental/packages/opentelemetry-instrumentation-http/src/types.ts#L118) | `HttpCustomMetricAttributeFunction` | Function for adding custom attributes before metric is recorded | | [`requestHook`](https://github.com/open-telemetry/opentelemetry-js/blob/main/experimental/packages/opentelemetry-instrumentation-http/src/types.ts#93) | `HttpRequestCustomAttributeFunction` | Function for adding custom attributes before request is handled | | [`responseHook`](https://github.com/open-telemetry/opentelemetry-js/blob/main/experimental/packages/opentelemetry-instrumentation-http/src/types.ts#L95) | `HttpResponseCustomAttributeFunction` | Function for adding custom attributes before response is handled | | [`startIncomingSpanHook`](https://github.com/open-telemetry/opentelemetry-js/blob/main/experimental/packages/opentelemetry-instrumentation-http/src/types.ts#L97) | `StartIncomingSpanCustomAttributeFunction` | Function for adding custom attributes before a span is started in incomingRequest | diff --git a/experimental/packages/opentelemetry-instrumentation-http/src/http.ts b/experimental/packages/opentelemetry-instrumentation-http/src/http.ts index 42a4332718f..a578eab2c6b 100644 --- a/experimental/packages/opentelemetry-instrumentation-http/src/http.ts +++ b/experimental/packages/opentelemetry-instrumentation-http/src/http.ts @@ -807,6 +807,19 @@ export class HttpInstrumentation extends InstrumentationBase + this.getConfig().applyCustomAttributesOnMetric!( + span, + spanKind, + metricAttributes + ), + () => {}, + true + ); + } + if (spanKind === SpanKind.SERVER) { this._httpServerDurationHistogram.record(duration, metricAttributes); } else if (spanKind === SpanKind.CLIENT) { diff --git a/experimental/packages/opentelemetry-instrumentation-http/src/types.ts b/experimental/packages/opentelemetry-instrumentation-http/src/types.ts index a0e783a1bdb..5bdb14602ca 100644 --- a/experimental/packages/opentelemetry-instrumentation-http/src/types.ts +++ b/experimental/packages/opentelemetry-instrumentation-http/src/types.ts @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -import { Span, SpanAttributes } from '@opentelemetry/api'; +import { MetricAttributes, Span, SpanAttributes, SpanKind } from '@opentelemetry/api'; import type * as http from 'http'; import type * as https from 'https'; import { @@ -80,6 +80,10 @@ export interface StartOutgoingSpanCustomAttributeFunction { (request: RequestOptions): SpanAttributes; } +export interface HttpCustomMetricAttributeFunction { + (span: Span, spanKind: SpanKind, metricAttributes: MetricAttributes): void; +} + /** * Options available for the HTTP instrumentation (see [documentation](https://github.com/open-telemetry/opentelemetry-js/tree/main/packages/opentelemetry-instrumentation-http#http-instrumentation-options)) */ @@ -104,6 +108,8 @@ export interface HttpInstrumentationConfig extends InstrumentationConfig { disableOutgoingRequestInstrumentation?: boolean; /** Function for adding custom attributes after response is handled */ applyCustomAttributesOnSpan?: HttpCustomAttributeFunction; + /** Function for adding custom attributes before metric is recorded */ + applyCustomAttributesOnMetric?: HttpCustomMetricAttributeFunction; /** Function for adding custom attributes before request is handled */ requestHook?: HttpRequestCustomAttributeFunction; /** Function for adding custom attributes before response is handled */ diff --git a/experimental/packages/opentelemetry-instrumentation-http/test/functionals/http-enable.test.ts b/experimental/packages/opentelemetry-instrumentation-http/test/functionals/http-enable.test.ts index 77d90b4fa3f..6355bf49abf 100644 --- a/experimental/packages/opentelemetry-instrumentation-http/test/functionals/http-enable.test.ts +++ b/experimental/packages/opentelemetry-instrumentation-http/test/functionals/http-enable.test.ts @@ -23,6 +23,7 @@ import { trace, SpanAttributes, DiagConsoleLogger, + MetricAttributes, } from '@opentelemetry/api'; import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node'; import { @@ -96,6 +97,10 @@ export const customAttributeFunction = (span: ISpan): void => { span.setAttribute('span kind', SpanKind.CLIENT); }; +export const customMetricAttributeFunction = (span: ISpan, spanKind: SpanKind, attr: MetricAttributes): void => { + attr['custom metric attribute function'] = 'test'; +}; + export const requestHookFunction = ( span: ISpan, request: ClientRequest | IncomingMessage @@ -177,6 +182,9 @@ describe('HttpInstrumentation', () => { applyCustomAttributesOnSpan: () => { throw new Error(applyCustomAttributesOnSpanErrorMessage); }, + applyCustomAttributesOnMetric: () => { + throw new Error('bad applyCustomAttributesOnMetric function'); + }, }; instrumentation.setConfig(config); instrumentation.enable(); @@ -308,6 +316,7 @@ describe('HttpInstrumentation', () => { return false; }, applyCustomAttributesOnSpan: customAttributeFunction, + applyCustomAttributesOnMetric: customMetricAttributeFunction, requestHook: requestHookFunction, responseHook: responseHookFunction, startIncomingSpanHook: startIncomingSpanHookFunction, diff --git a/experimental/packages/opentelemetry-instrumentation-http/test/functionals/https-enable.test.ts b/experimental/packages/opentelemetry-instrumentation-http/test/functionals/https-enable.test.ts index 0f48a30ae72..30287eb5446 100644 --- a/experimental/packages/opentelemetry-instrumentation-http/test/functionals/https-enable.test.ts +++ b/experimental/packages/opentelemetry-instrumentation-http/test/functionals/https-enable.test.ts @@ -21,6 +21,7 @@ import { Span as ISpan, SpanKind, trace, + MetricAttributes, } from '@opentelemetry/api'; import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks'; import { ContextManager } from '@opentelemetry/api'; @@ -88,6 +89,10 @@ export const customAttributeFunction = (span: ISpan): void => { span.setAttribute('span kind', SpanKind.CLIENT); }; +export const customMetricAttributeFunction = (span: ISpan, spanKind: SpanKind, attr: MetricAttributes): void => { + attr['custom metric attribute function'] = 'test'; +}; + describe('HttpsInstrumentation', () => { let contextManager: ContextManager; @@ -130,6 +135,9 @@ describe('HttpsInstrumentation', () => { applyCustomAttributesOnSpan: () => { throw new Error(applyCustomAttributesOnSpanErrorMessage); }, + applyCustomAttributesOnMetric: () => { + throw new Error('bad applyCustomAttributesOnMetric function'); + }, }); instrumentation.enable(); server = https.createServer( @@ -217,6 +225,7 @@ describe('HttpsInstrumentation', () => { return false; }, applyCustomAttributesOnSpan: customAttributeFunction, + applyCustomAttributesOnMetric: customMetricAttributeFunction, serverName, }); instrumentation.enable();