Skip to content

Commit

Permalink
refactor(instrumentation-http): fix eslint warnings
Browse files Browse the repository at this point in the history
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
  • Loading branch information
chancancode committed Jan 29, 2025
1 parent 732bf01 commit de724a6
Showing 1 changed file with 30 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -462,9 +462,7 @@ export class HttpInstrumentation extends InstrumentationBase<HttpInstrumentation
oldMetricAttributes: Attributes,
stableMetricAttributes: Attributes
): http.ClientRequest {
if (this.getConfig().requestHook) {
this._callRequestHook(span, request);
}
this._callRequestHook(span, request);

/**
* Determines if the request has errored or the response has ended/errored.
Expand Down Expand Up @@ -493,9 +491,7 @@ export class HttpInstrumentation extends InstrumentationBase<HttpInstrumentation
getOutgoingRequestMetricAttributesOnResponse(responseAttributes)
);

if (this.getConfig().responseHook) {
this._callResponseHook(span, response);
}
this._callResponseHook(span, response);

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

span.setStatus(status);

if (this.getConfig().applyCustomAttributesOnSpan) {
const { applyCustomAttributesOnSpan } = this.getConfig();

if (applyCustomAttributesOnSpan) {
safeExecuteInTheMiddle(
() =>
this.getConfig().applyCustomAttributesOnSpan!(
span,
request,
response
),
() => applyCustomAttributesOnSpan(span, request, response),
() => {},
true
);
Expand Down Expand Up @@ -699,12 +692,8 @@ export class HttpInstrumentation extends InstrumentationBase<HttpInstrumentation
context.bind(context.active(), request);
context.bind(context.active(), response);

if (instrumentation.getConfig().requestHook) {
instrumentation._callRequestHook(span, request);
}
if (instrumentation.getConfig().responseHook) {
instrumentation._callResponseHook(span, response);
}
instrumentation._callRequestHook(span, request);
instrumentation._callResponseHook(span, response);

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

if (this.getConfig().applyCustomAttributesOnSpan) {
const { applyCustomAttributesOnSpan } = this.getConfig();

if (applyCustomAttributesOnSpan) {
safeExecuteInTheMiddle(
() =>
this.getConfig().applyCustomAttributesOnSpan!(
span,
request,
response
),
() => applyCustomAttributesOnSpan(span, request, response),
() => {},
true
);
Expand Down Expand Up @@ -1052,22 +1038,30 @@ export class HttpInstrumentation extends InstrumentationBase<HttpInstrumentation
span: Span,
response: http.IncomingMessage | http.ServerResponse
) {
safeExecuteInTheMiddle(
() => this.getConfig().responseHook!(span, response),
() => {},
true
);
const { responseHook } = this.getConfig();

if (responseHook) {
safeExecuteInTheMiddle(
() => responseHook(span, response),
() => {},
true
);
}
}

private _callRequestHook(
span: Span,
request: http.ClientRequest | http.IncomingMessage
) {
safeExecuteInTheMiddle(
() => this.getConfig().requestHook!(span, request),
() => {},
true
);
const { requestHook } = this.getConfig();

if (requestHook) {
safeExecuteInTheMiddle(
() => requestHook(span, request),
() => {},
true
);
}
}

private _callStartSpanHook(
Expand Down

0 comments on commit de724a6

Please sign in to comment.