Skip to content

Update Dependencies #219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Handler, Context } from "aws-lambda";

import { Context, Handler } from "aws-lambda";
import {
incrementErrorsMetric,
incrementInvocationsMetric,
Expand All @@ -8,19 +7,20 @@ import {
MetricsListener,
} from "./metrics";
import { TraceConfig, TraceHeaders, TraceListener } from "./trace";
import { extractHTTPStatusCodeTag } from "./trace/trigger";
import {
logDebug,
logError,
LogLevel,
Logger,
LogLevel,
promisifiedHandler,
setColdStart,
setLogLevel,
setLogger,
promisifiedHandler,
logDebug,
setLogLevel,
tagObject,
} from "./utils";

export { TraceHeaders } from "./trace";
import { extractHTTPStatusCodeTag } from "./trace/trigger";

export const apiKeyEnvVar = "DD_API_KEY";
export const apiKeyKMSEnvVar = "DD_KMS_API_KEY";
Expand Down Expand Up @@ -123,7 +123,9 @@ export function datadog<TEvent, TResult>(
incrementInvocationsMetric(metricsListener, context);
}
} catch (err) {
logDebug("Failed to start listeners", err);
if (err instanceof Error) {
logDebug("Failed to start listeners", err);
}
}

let result: TResult | undefined;
Expand Down Expand Up @@ -164,7 +166,9 @@ export function datadog<TEvent, TResult>(
incrementErrorsMetric(metricsListener, context);
}
} catch (err) {
logDebug("Failed to complete listeners", err);
if (err instanceof Error) {
logDebug("Failed to complete listeners", err);
}
}
currentMetricsListener = undefined;
currentTraceListener = undefined;
Expand Down
6 changes: 4 additions & 2 deletions src/metrics/kms-service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { KMSService } from "./kms-service";
import nock from "nock";
import { KMSService } from "./kms-service";

describe("KMSService", () => {
const ENCRYPTED_KEY = "BQICAHj0djbIQaGrIfSD2gstvRF3h8YGMeEvO5rRHNiuWwSeegEFl57KxNejRn"; // random fake key
Expand Down Expand Up @@ -79,7 +79,9 @@ describe("KMSService", () => {
await kmsService.decrypt(ENCRYPTED_KEY);
fail();
} catch (e) {
expect(e.message).toEqual(EXPECTED_ERROR_MESSAGE);
if (e instanceof Error) {
expect((e as Error).message).toEqual(EXPECTED_ERROR_MESSAGE);
}
}
fakeKmsCall.done();
});
Expand Down
2 changes: 1 addition & 1 deletion src/metrics/kms-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export class KMSService {
}
return result.Plaintext.toString("ascii");
} catch (err) {
if (err.code === "MODULE_NOT_FOUND") {
if ((err as any).code === "MODULE_NOT_FOUND") {
const errorMsg = "optional dependency aws-sdk not installed. KMS key decryption will not work";
logError(errorMsg);
throw Error(errorMsg);
Expand Down
17 changes: 11 additions & 6 deletions src/metrics/listener.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { StatsD } from "hot-shots";
import { promisify } from "util";

import { logDebug, logError } from "../utils";
import { APIClient } from "./api";
import { flushExtension, isAgentRunning } from "./extension";
import { KMSService } from "./kms-service";
import { writeMetricToStdout } from "./metric-log";
import { Distribution } from "./model";
import { Processor } from "./processor";
import { StatsD } from "hot-shots";
import { isAgentRunning, flushExtension } from "./extension";

const metricsBatchSendIntervalMS = 10000; // 10 seconds

Expand Down Expand Up @@ -109,15 +108,19 @@ export class MetricsListener {
} catch (error) {
// This can fail for a variety of reasons, from the API not being reachable,
// to KMS key decryption failing.
logError("failed to flush metrics", error);
if (error instanceof Error) {
logError("failed to flush metrics", error as Error);
}
}
try {
if (this.isAgentRunning) {
logDebug(`Flushing Extension`);
await flushExtension();
}
} catch (error) {
logError("failed to flush extension", error);
if (error instanceof Error) {
logError("failed to flush extension", error as Error);
}
}
this.currentProcessor = undefined;
}
Expand Down Expand Up @@ -172,7 +175,9 @@ export class MetricsListener {
try {
return await this.kmsClient.decrypt(config.apiKeyKMS);
} catch (error) {
logError("couldn't decrypt kms api key", error);
if (error instanceof Error) {
logError("couldn't decrypt kms api key", error as Error);
}
}
} else {
const errorMessage = "api key not configured, see https://dtdg.co/sls-node-metrics";
Expand Down
28 changes: 18 additions & 10 deletions src/trace/context.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { Context } from "aws-lambda";
import { Context, SQSEvent } from "aws-lambda";
import { BigNumber } from "bignumber.js";
import { randomBytes } from "crypto";
import { createSocket, Socket } from "dgram";
import { SQSEvent } from "aws-lambda";

import { logDebug, logError } from "../utils";
import { isSQSEvent } from "../utils/event-type-guards";
import {
awsXrayDaemonAddressEnvVar,
parentIDHeader,
SampleMode,
samplingPriorityHeader,
Expand All @@ -18,7 +17,6 @@ import {
xraySubsegmentName,
xraySubsegmentNamespace,
xrayTraceEnvVar,
awsXrayDaemonAddressEnvVar,
} from "./constants";
import { TraceExtractor } from "./listener";

Expand Down Expand Up @@ -59,7 +57,9 @@ export function extractTraceContext(
trace = extractor(event, context);
logDebug(`extracted trace context from the custom extractor`, { trace });
} catch (error) {
logError("custom extractor function failed", error);
if (error instanceof Error) {
logError("custom extractor function failed", error as Error);
}
}
}

Expand All @@ -76,7 +76,9 @@ export function extractTraceContext(
try {
addStepFunctionContextToXray(stepFuncContext);
} catch (error) {
logError("couldn't add step function metadata to xray", error);
if (error instanceof Error) {
logError("couldn't add step function metadata to xray", error as Error);
}
}
}

Expand All @@ -86,7 +88,9 @@ export function extractTraceContext(
logDebug(`added trace context to xray metadata`, { trace });
} catch (error) {
// This might fail if running in an environment where xray isn't set up, (like for local development).
logError("couldn't add trace context to xray metadata", error);
if (error instanceof Error) {
logError("couldn't add trace context to xray metadata", error as Error);
}
}
return trace;
}
Expand Down Expand Up @@ -172,8 +176,10 @@ export function sendXraySubsegment(segment: string) {
logDebug(`Xray daemon received metadata payload`, { error, bytes });
});
} catch (error) {
client?.close();
logDebug("Error occurred submitting to xray daemon", error);
if (error instanceof Error) {
client?.close();
logDebug("Error occurred submitting to xray daemon", error);
}
}
}

Expand Down Expand Up @@ -210,7 +216,9 @@ export function readTraceFromSQSEvent(event: SQSEvent): TraceContext | undefined
logDebug(`extracted trace context from sqs event`, { trace, event });
return trace;
} catch (err) {
logError("Error parsing SQS message trace data", err);
if (err instanceof Error) {
logError("Error parsing SQS message trace data", err as Error);
}
return;
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/trace/tracer-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ export class TracerWrapper {
this.tracer = require(path);
return;
} catch (err) {
logDebug("Couldn't require dd-trace from main", err);
if (err instanceof Object || err instanceof Error) {
logDebug("Couldn't require dd-trace from main", err);
}
}
}

Expand Down
21 changes: 14 additions & 7 deletions src/utils/handler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Callback, Context, Handler } from "aws-lambda";

import { logError } from "./log";

export type OnWrapFunc<T = (...args: any[]) => any> = (fn: T) => T;
Expand All @@ -19,8 +18,10 @@ export function wrap<TEvent, TResult>(
try {
await onStart(event, context);
} catch (error) {
// Swallow the error and continue processing.
logError("Pre-lambda hook threw error", error);
if (error instanceof Error) {
// Swallow the error and continue processing.
logError("Pre-lambda hook threw error", error as Error);
}
}
let result: TResult | undefined;

Expand All @@ -31,13 +32,17 @@ export function wrap<TEvent, TResult>(
try {
wrappedHandler = onWrap !== undefined ? onWrap(promHandler) : promHandler;
} catch (error) {
logError("Failed to apply wrap to handler function", error);
if (error instanceof Error) {
logError("Failed to apply wrap to handler function", error as Error);
}
}

try {
result = await wrappedHandler(event, context);
} catch (error) {
handlerError = error;
if (error instanceof Error) {
handlerError = error;
}
throw error;
} finally {
try {
Expand All @@ -47,8 +52,10 @@ export function wrap<TEvent, TResult>(
await onComplete(event, context);
}
} catch (error) {
// Swallow the error and continue processing.
logError("Post-lambda hook threw error", error);
if (error instanceof Error) {
// Swallow the error and continue processing.
logError("Post-lambda hook threw error", error as Error);
}
}
}

Expand Down
Loading