Skip to content

Commit 436650b

Browse files
Update Dependencies (#219)
Update dependencies and address breaking changes
1 parent 57f8e9c commit 436650b

File tree

8 files changed

+643
-619
lines changed

8 files changed

+643
-619
lines changed

src/index.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { Handler, Context } from "aws-lambda";
2-
1+
import { Context, Handler } from "aws-lambda";
32
import {
43
incrementErrorsMetric,
54
incrementInvocationsMetric,
@@ -8,19 +7,20 @@ import {
87
MetricsListener,
98
} from "./metrics";
109
import { TraceConfig, TraceHeaders, TraceListener } from "./trace";
10+
import { extractHTTPStatusCodeTag } from "./trace/trigger";
1111
import {
12+
logDebug,
1213
logError,
13-
LogLevel,
1414
Logger,
15+
LogLevel,
16+
promisifiedHandler,
1517
setColdStart,
16-
setLogLevel,
1718
setLogger,
18-
promisifiedHandler,
19-
logDebug,
19+
setLogLevel,
2020
tagObject,
2121
} from "./utils";
22+
2223
export { TraceHeaders } from "./trace";
23-
import { extractHTTPStatusCodeTag } from "./trace/trigger";
2424

2525
export const apiKeyEnvVar = "DD_API_KEY";
2626
export const apiKeyKMSEnvVar = "DD_KMS_API_KEY";
@@ -123,7 +123,9 @@ export function datadog<TEvent, TResult>(
123123
incrementInvocationsMetric(metricsListener, context);
124124
}
125125
} catch (err) {
126-
logDebug("Failed to start listeners", err);
126+
if (err instanceof Error) {
127+
logDebug("Failed to start listeners", err);
128+
}
127129
}
128130

129131
let result: TResult | undefined;
@@ -164,7 +166,9 @@ export function datadog<TEvent, TResult>(
164166
incrementErrorsMetric(metricsListener, context);
165167
}
166168
} catch (err) {
167-
logDebug("Failed to complete listeners", err);
169+
if (err instanceof Error) {
170+
logDebug("Failed to complete listeners", err);
171+
}
168172
}
169173
currentMetricsListener = undefined;
170174
currentTraceListener = undefined;

src/metrics/kms-service.spec.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { KMSService } from "./kms-service";
21
import nock from "nock";
2+
import { KMSService } from "./kms-service";
33

44
describe("KMSService", () => {
55
const ENCRYPTED_KEY = "BQICAHj0djbIQaGrIfSD2gstvRF3h8YGMeEvO5rRHNiuWwSeegEFl57KxNejRn"; // random fake key
@@ -79,7 +79,9 @@ describe("KMSService", () => {
7979
await kmsService.decrypt(ENCRYPTED_KEY);
8080
fail();
8181
} catch (e) {
82-
expect(e.message).toEqual(EXPECTED_ERROR_MESSAGE);
82+
if (e instanceof Error) {
83+
expect((e as Error).message).toEqual(EXPECTED_ERROR_MESSAGE);
84+
}
8385
}
8486
fakeKmsCall.done();
8587
});

src/metrics/kms-service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export class KMSService {
2828
}
2929
return result.Plaintext.toString("ascii");
3030
} catch (err) {
31-
if (err.code === "MODULE_NOT_FOUND") {
31+
if ((err as any).code === "MODULE_NOT_FOUND") {
3232
const errorMsg = "optional dependency aws-sdk not installed. KMS key decryption will not work";
3333
logError(errorMsg);
3434
throw Error(errorMsg);

src/metrics/listener.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1+
import { StatsD } from "hot-shots";
12
import { promisify } from "util";
2-
33
import { logDebug, logError } from "../utils";
44
import { APIClient } from "./api";
5+
import { flushExtension, isAgentRunning } from "./extension";
56
import { KMSService } from "./kms-service";
67
import { writeMetricToStdout } from "./metric-log";
78
import { Distribution } from "./model";
89
import { Processor } from "./processor";
9-
import { StatsD } from "hot-shots";
10-
import { isAgentRunning, flushExtension } from "./extension";
1110

1211
const metricsBatchSendIntervalMS = 10000; // 10 seconds
1312

@@ -109,15 +108,19 @@ export class MetricsListener {
109108
} catch (error) {
110109
// This can fail for a variety of reasons, from the API not being reachable,
111110
// to KMS key decryption failing.
112-
logError("failed to flush metrics", error);
111+
if (error instanceof Error) {
112+
logError("failed to flush metrics", error as Error);
113+
}
113114
}
114115
try {
115116
if (this.isAgentRunning) {
116117
logDebug(`Flushing Extension`);
117118
await flushExtension();
118119
}
119120
} catch (error) {
120-
logError("failed to flush extension", error);
121+
if (error instanceof Error) {
122+
logError("failed to flush extension", error as Error);
123+
}
121124
}
122125
this.currentProcessor = undefined;
123126
}
@@ -172,7 +175,9 @@ export class MetricsListener {
172175
try {
173176
return await this.kmsClient.decrypt(config.apiKeyKMS);
174177
} catch (error) {
175-
logError("couldn't decrypt kms api key", error);
178+
if (error instanceof Error) {
179+
logError("couldn't decrypt kms api key", error as Error);
180+
}
176181
}
177182
} else {
178183
const errorMessage = "api key not configured, see https://dtdg.co/sls-node-metrics";

src/trace/context.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
import { Context } from "aws-lambda";
1+
import { Context, SQSEvent } from "aws-lambda";
22
import { BigNumber } from "bignumber.js";
33
import { randomBytes } from "crypto";
44
import { createSocket, Socket } from "dgram";
5-
import { SQSEvent } from "aws-lambda";
6-
75
import { logDebug, logError } from "../utils";
86
import { isSQSEvent } from "../utils/event-type-guards";
97
import {
8+
awsXrayDaemonAddressEnvVar,
109
parentIDHeader,
1110
SampleMode,
1211
samplingPriorityHeader,
@@ -18,7 +17,6 @@ import {
1817
xraySubsegmentName,
1918
xraySubsegmentNamespace,
2019
xrayTraceEnvVar,
21-
awsXrayDaemonAddressEnvVar,
2220
} from "./constants";
2321
import { TraceExtractor } from "./listener";
2422

@@ -59,7 +57,9 @@ export function extractTraceContext(
5957
trace = extractor(event, context);
6058
logDebug(`extracted trace context from the custom extractor`, { trace });
6159
} catch (error) {
62-
logError("custom extractor function failed", error);
60+
if (error instanceof Error) {
61+
logError("custom extractor function failed", error as Error);
62+
}
6363
}
6464
}
6565

@@ -76,7 +76,9 @@ export function extractTraceContext(
7676
try {
7777
addStepFunctionContextToXray(stepFuncContext);
7878
} catch (error) {
79-
logError("couldn't add step function metadata to xray", error);
79+
if (error instanceof Error) {
80+
logError("couldn't add step function metadata to xray", error as Error);
81+
}
8082
}
8183
}
8284

@@ -86,7 +88,9 @@ export function extractTraceContext(
8688
logDebug(`added trace context to xray metadata`, { trace });
8789
} catch (error) {
8890
// This might fail if running in an environment where xray isn't set up, (like for local development).
89-
logError("couldn't add trace context to xray metadata", error);
91+
if (error instanceof Error) {
92+
logError("couldn't add trace context to xray metadata", error as Error);
93+
}
9094
}
9195
return trace;
9296
}
@@ -172,8 +176,10 @@ export function sendXraySubsegment(segment: string) {
172176
logDebug(`Xray daemon received metadata payload`, { error, bytes });
173177
});
174178
} catch (error) {
175-
client?.close();
176-
logDebug("Error occurred submitting to xray daemon", error);
179+
if (error instanceof Error) {
180+
client?.close();
181+
logDebug("Error occurred submitting to xray daemon", error);
182+
}
177183
}
178184
}
179185

@@ -210,7 +216,9 @@ export function readTraceFromSQSEvent(event: SQSEvent): TraceContext | undefined
210216
logDebug(`extracted trace context from sqs event`, { trace, event });
211217
return trace;
212218
} catch (err) {
213-
logError("Error parsing SQS message trace data", err);
219+
if (err instanceof Error) {
220+
logError("Error parsing SQS message trace data", err as Error);
221+
}
214222
return;
215223
}
216224
}

src/trace/tracer-wrapper.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ export class TracerWrapper {
3030
this.tracer = require(path);
3131
return;
3232
} catch (err) {
33-
logDebug("Couldn't require dd-trace from main", err);
33+
if (err instanceof Object || err instanceof Error) {
34+
logDebug("Couldn't require dd-trace from main", err);
35+
}
3436
}
3537
}
3638

src/utils/handler.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { Callback, Context, Handler } from "aws-lambda";
2-
32
import { logError } from "./log";
43

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

@@ -31,13 +32,17 @@ export function wrap<TEvent, TResult>(
3132
try {
3233
wrappedHandler = onWrap !== undefined ? onWrap(promHandler) : promHandler;
3334
} catch (error) {
34-
logError("Failed to apply wrap to handler function", error);
35+
if (error instanceof Error) {
36+
logError("Failed to apply wrap to handler function", error as Error);
37+
}
3538
}
3639

3740
try {
3841
result = await wrappedHandler(event, context);
3942
} catch (error) {
40-
handlerError = error;
43+
if (error instanceof Error) {
44+
handlerError = error;
45+
}
4146
throw error;
4247
} finally {
4348
try {
@@ -47,8 +52,10 @@ export function wrap<TEvent, TResult>(
4752
await onComplete(event, context);
4853
}
4954
} catch (error) {
50-
// Swallow the error and continue processing.
51-
logError("Post-lambda hook threw error", error);
55+
if (error instanceof Error) {
56+
// Swallow the error and continue processing.
57+
logError("Post-lambda hook threw error", error as Error);
58+
}
5259
}
5360
}
5461

0 commit comments

Comments
 (0)