Skip to content

Commit

Permalink
fix: Avoid hard datadog-lambda-js dependency (#197)
Browse files Browse the repository at this point in the history
  • Loading branch information
72636c authored Mar 20, 2023
1 parent 059b25e commit ec04e90
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/createLambdaExtensionClient.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
import type { Context } from 'aws-lambda';
import { datadog, sendDistributionMetric } from 'datadog-lambda-js';

import { LambdaExtensionMetricsClient } from './LambdaExtensionMetricsClient';

/**
* Vendored from `datadog-lambda-js`.
*/
abstract class DatadogLambdaJs {
abstract datadog: <T>(handler: T) => T;

abstract sendDistributionMetric: (
name: string,
value: number,
...tags: string[]
) => void;
}

interface DatadogMetric {
name: string;
tags?: string[];
Expand Down Expand Up @@ -47,6 +59,10 @@ const sanitiseTag = (tag: string): string => tag.replace(/\||@|,/g, '_');
export const createLambdaExtensionClient = (
config: DatadogConfig,
): LambdaExtensionClient => {
const { datadog, sendDistributionMetric } =
// eslint-disable-next-line @typescript-eslint/no-var-requires
require('datadog-lambda-js') as DatadogLambdaJs;

const send = (metric: DatadogMetric) => {
const { value } = metric;

Expand Down Expand Up @@ -82,8 +98,7 @@ export const createLambdaExtensionClient = (
return {
withLambdaExtension: <Event, Output = unknown>(
fn: Handler<Event, Output>,
): Handler<Event, Output> =>
config.metrics ? (datadog(fn) as Handler<Event, Output>) : fn,
): Handler<Event, Output> => (config.metrics ? datadog(fn) : fn),

metricsClient: {
increment: sendCount,
Expand Down

0 comments on commit ec04e90

Please sign in to comment.