Skip to content

Commit

Permalink
feat: add applyCustomAttributesOnMetric
Browse files Browse the repository at this point in the history
  • Loading branch information
evan361425 committed Jul 29, 2024
1 parent 34003c9 commit da2206a
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,19 @@ export class HttpInstrumentation extends InstrumentationBase<HttpInstrumentation

// Record metrics
const duration = hrTimeToMilliseconds(hrTimeDuration(startTime, hrTime()));
if (this.getConfig().applyCustomAttributesOnMetric) {
safeExecuteInTheMiddle(
() =>
this.getConfig().applyCustomAttributesOnMetric!(
span,
spanKind,
metricAttributes
),
() => {},
true
);
}

if (spanKind === SpanKind.SERVER) {
this._httpServerDurationHistogram.record(duration, metricAttributes);
} else if (spanKind === SpanKind.CLIENT) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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))
*/
Expand All @@ -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 */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
trace,
SpanAttributes,
DiagConsoleLogger,
MetricAttributes,
} from '@opentelemetry/api';
import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';
import {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -177,6 +182,9 @@ describe('HttpInstrumentation', () => {
applyCustomAttributesOnSpan: () => {
throw new Error(applyCustomAttributesOnSpanErrorMessage);
},
applyCustomAttributesOnMetric: () => {
throw new Error('bad applyCustomAttributesOnMetric function');
},
};
instrumentation.setConfig(config);
instrumentation.enable();
Expand Down Expand Up @@ -308,6 +316,7 @@ describe('HttpInstrumentation', () => {
return false;
},
applyCustomAttributesOnSpan: customAttributeFunction,
applyCustomAttributesOnMetric: customMetricAttributeFunction,
requestHook: requestHookFunction,
responseHook: responseHookFunction,
startIncomingSpanHook: startIncomingSpanHookFunction,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -130,6 +135,9 @@ describe('HttpsInstrumentation', () => {
applyCustomAttributesOnSpan: () => {
throw new Error(applyCustomAttributesOnSpanErrorMessage);
},
applyCustomAttributesOnMetric: () => {
throw new Error('bad applyCustomAttributesOnMetric function');
},
});
instrumentation.enable();
server = https.createServer(
Expand Down Expand Up @@ -217,6 +225,7 @@ describe('HttpsInstrumentation', () => {
return false;
},
applyCustomAttributesOnSpan: customAttributeFunction,
applyCustomAttributesOnMetric: customMetricAttributeFunction,
serverName,
});
instrumentation.enable();
Expand Down

0 comments on commit da2206a

Please sign in to comment.