Skip to content

Commit de724a6

Browse files
committed
refactor(instrumentation-http): fix eslint warnings
Fix the following eslint warnings: ``` /home/runner/work/opentelemetry-js/opentelemetry-js/experimental/packages/opentelemetry-instrumentation-http/src/http.ts 508:17 warning Forbidden non-null assertion @typescript-eslint/no-non-null-assertion 931:11 warning Forbidden non-null assertion @typescript-eslint/no-non-null-assertion 1032:13 warning Forbidden non-null assertion @typescript-eslint/no-non-null-assertion 1043:13 warning Forbidden non-null assertion @typescript-eslint/no-non-null-assertion ``` Because the expression we check is indirected through the a method call (`getConfig()`), TypeScript cannot assume the value would be the same across the two calls. By extracting the value and checking for that, TypeScript can narrow the type correctly and we can avoid the non-null assertion. Ref open-telemetry#5365
1 parent 732bf01 commit de724a6

File tree

1 file changed

+30
-36
lines changed
  • experimental/packages/opentelemetry-instrumentation-http/src

1 file changed

+30
-36
lines changed

experimental/packages/opentelemetry-instrumentation-http/src/http.ts

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -462,9 +462,7 @@ export class HttpInstrumentation extends InstrumentationBase<HttpInstrumentation
462462
oldMetricAttributes: Attributes,
463463
stableMetricAttributes: Attributes
464464
): http.ClientRequest {
465-
if (this.getConfig().requestHook) {
466-
this._callRequestHook(span, request);
467-
}
465+
this._callRequestHook(span, request);
468466

469467
/**
470468
* Determines if the request has errored or the response has ended/errored.
@@ -493,9 +491,7 @@ export class HttpInstrumentation extends InstrumentationBase<HttpInstrumentation
493491
getOutgoingRequestMetricAttributesOnResponse(responseAttributes)
494492
);
495493

496-
if (this.getConfig().responseHook) {
497-
this._callResponseHook(span, response);
498-
}
494+
this._callResponseHook(span, response);
499495

500496
this._headerCapture.client.captureRequestHeaders(span, header =>
501497
request.getHeader(header)
@@ -526,14 +522,11 @@ export class HttpInstrumentation extends InstrumentationBase<HttpInstrumentation
526522

527523
span.setStatus(status);
528524

529-
if (this.getConfig().applyCustomAttributesOnSpan) {
525+
const { applyCustomAttributesOnSpan } = this.getConfig();
526+
527+
if (applyCustomAttributesOnSpan) {
530528
safeExecuteInTheMiddle(
531-
() =>
532-
this.getConfig().applyCustomAttributesOnSpan!(
533-
span,
534-
request,
535-
response
536-
),
529+
() => applyCustomAttributesOnSpan(span, request, response),
537530
() => {},
538531
true
539532
);
@@ -699,12 +692,8 @@ export class HttpInstrumentation extends InstrumentationBase<HttpInstrumentation
699692
context.bind(context.active(), request);
700693
context.bind(context.active(), response);
701694

702-
if (instrumentation.getConfig().requestHook) {
703-
instrumentation._callRequestHook(span, request);
704-
}
705-
if (instrumentation.getConfig().responseHook) {
706-
instrumentation._callResponseHook(span, response);
707-
}
695+
instrumentation._callRequestHook(span, request);
696+
instrumentation._callResponseHook(span, response);
708697

709698
instrumentation._headerCapture.server.captureRequestHeaders(
710699
span,
@@ -949,14 +938,11 @@ export class HttpInstrumentation extends InstrumentationBase<HttpInstrumentation
949938
span.updateName(`${request.method || 'GET'} ${route}`);
950939
}
951940

952-
if (this.getConfig().applyCustomAttributesOnSpan) {
941+
const { applyCustomAttributesOnSpan } = this.getConfig();
942+
943+
if (applyCustomAttributesOnSpan) {
953944
safeExecuteInTheMiddle(
954-
() =>
955-
this.getConfig().applyCustomAttributesOnSpan!(
956-
span,
957-
request,
958-
response
959-
),
945+
() => applyCustomAttributesOnSpan(span, request, response),
960946
() => {},
961947
true
962948
);
@@ -1052,22 +1038,30 @@ export class HttpInstrumentation extends InstrumentationBase<HttpInstrumentation
10521038
span: Span,
10531039
response: http.IncomingMessage | http.ServerResponse
10541040
) {
1055-
safeExecuteInTheMiddle(
1056-
() => this.getConfig().responseHook!(span, response),
1057-
() => {},
1058-
true
1059-
);
1041+
const { responseHook } = this.getConfig();
1042+
1043+
if (responseHook) {
1044+
safeExecuteInTheMiddle(
1045+
() => responseHook(span, response),
1046+
() => {},
1047+
true
1048+
);
1049+
}
10601050
}
10611051

10621052
private _callRequestHook(
10631053
span: Span,
10641054
request: http.ClientRequest | http.IncomingMessage
10651055
) {
1066-
safeExecuteInTheMiddle(
1067-
() => this.getConfig().requestHook!(span, request),
1068-
() => {},
1069-
true
1070-
);
1056+
const { requestHook } = this.getConfig();
1057+
1058+
if (requestHook) {
1059+
safeExecuteInTheMiddle(
1060+
() => requestHook(span, request),
1061+
() => {},
1062+
true
1063+
);
1064+
}
10711065
}
10721066

10731067
private _callStartSpanHook(

0 commit comments

Comments
 (0)