Skip to content

Commit

Permalink
add ResponseError status codes and connection IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
shouples committed Nov 16, 2024
1 parent c988235 commit bc947b1
Showing 1 changed file with 37 additions and 13 deletions.
50 changes: 37 additions & 13 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { ResponseError as SchemaRegistryResponseError } from "./clients/schemaRe
import { ResponseError as SidecarResponseError } from "./clients/sidecar";
import { observabilityContext } from "./context/observability";
import { Logger } from "./logging";
import { SIDECAR_CONNECTION_ID_HEADER } from "./sidecar/constants";

const logger = new Logger("errors");

Expand Down Expand Up @@ -54,33 +55,56 @@ export async function logResponseError(

logger.error(errorMessage, { ...errorContext, ...extra });
if (sendTelemetry) {
Sentry.captureException(e, {
captureException(e, {
contexts: { response: { status_code: responseStatusCode } },
extra: { ...errorContext, ...extra },
});
}
}

/**
* Wrapper around the Sentry {@link Sentry.captureException} to include our extension instance's
* Wrapper around the {@link Sentry.captureException} to include our extension instance's
* {@link observabilityContext} in the event context, along with any additionally provided context.
*
* If a {@link ResponseError} is provided as the `error`, the response status code and any associated
* `x-connection-id` header will be included in the event context under `contexts.response`.
*
* See {@link ExclusiveEventHintOrCaptureContext} for more information on the `context` parameter.
*/
export function captureException(
error: unknown,
context: ExclusiveEventHintOrCaptureContext | undefined,
e: unknown,
context: ExclusiveEventHintOrCaptureContext = {},
): void {
// TODO: check telemetry settings here?
const obsContext: Record<string, any> = observabilityContext.toRecord();
const extraContext = context ? context : {};
// ensure observability context keys always make it into the Sentry event under `extra`, even if
// the caller provided their own `extra` data in `context`
const { extra: extraFromContext = {}, ...restExtraContext } = extraContext as any;
const errorContext: Record<string, any> = {
extra: { ...obsContext, ...extraFromContext },
...restExtraContext,
};
let errorContext: Record<string, any> = { extra: { ...obsContext } };

if (context && context.data.extra) {
// ensure observability context keys always make it into the Sentry event under `extra`, even if
// the caller provided their own `extra` data in `context`
const { extra: extraFromContext = {}, ...restExtraContext } = context as Record<string, any>;
errorContext = {
extra: { ...obsContext, ...extraFromContext },
...restExtraContext,
};
}

if ((e as any).response) {
const errorResponse = (e as ResponseError).response;
errorContext = {
...errorContext,
contexts: {
response: {
status_code: errorResponse.status,
headers: {
[SIDECAR_CONNECTION_ID_HEADER]:
errorResponse.headers.get(SIDECAR_CONNECTION_ID_HEADER) ?? "",
},
},
},
};
}

logger.debug("capturing exception before sending to Sentry", errorContext);
Sentry.captureException(error, errorContext);
Sentry.captureException(e, errorContext);
}

0 comments on commit bc947b1

Please sign in to comment.