Skip to content
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,12 @@ Lumigo integration works out-of-the-box with Lumigo's [auto-trace feature](https

And if you don't use Lumigo, don't worry, their tracer will not be instantiated in your functions and no calls will be made to their servers unless `LUMIGO_TRACER_TOKEN` is set.

### Dash0

We also support sending telemetry to Dash0. To enable this, you can use Dash0's auto-instrumentation feature, or manually configure the [Dash0 Lambda Extension](https://github.com/dash0hq/dash0-lambda-extension) as directed in their docs.

If using Dash0, you must also add `@opentelemetry/api` to your project dependencies.

## Notes

Lambda Wrapper's dependency injection relies on class names being preserved. If your build process includes minifying or uglifying your code, you'll need to disable these transformations.
Expand Down
9 changes: 9 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,18 @@
"test:types": "tsc -p tsconfig-type-test.json",
"coverage": "yarn test --coverage"
},
"peerDependencies": {
"@opentelemetry/api": "^1.9.1"
},
"peerDependenciesMeta": {
"@opentelemetry/api": {
"optional": true
}
},
"devDependencies": {
"@comicrelief/eslint-config": "^2.0.3",
"@istanbuljs/nyc-config-typescript": "^1.0.2",
"@opentelemetry/api": "^1.9.1",
"@types/async": "^3.2.24",
"@types/jest": "^29.5.12",
"@types/node": "22",
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export {
} from './services/TimerService';

export { default as TelemetryProvider } from './telemetry/base';
export { default as Dash0Telemetry } from './telemetry/Dash0';
export { default as LumigoTelemetry } from './telemetry/Lumigo';
export { default as SentryTelemetry } from './telemetry/Sentry';

Expand Down
2 changes: 2 additions & 0 deletions src/services/LoggerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Winston from 'winston';

import DependencyAwareClass from '../core/DependencyAwareClass';
import DependencyInjection from '../core/DependencyInjection';
import Dash0Telemetry from '../telemetry/Dash0';
import LumigoTelemetry from '../telemetry/Lumigo';
import SentryTelemetry from '../telemetry/Sentry';
import TelemetryProvider from '../telemetry/base';
Expand All @@ -17,6 +18,7 @@ import TelemetryProvider from '../telemetry/base';
const TELEMETRY_PROVIDERS = [
SentryTelemetry,
LumigoTelemetry,
Dash0Telemetry,
];

/**
Expand Down
53 changes: 53 additions & 0 deletions src/telemetry/Dash0.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import type { TraceAPI } from '@opentelemetry/api';

import LoggerService from '../services/LoggerService';
import TelemetryProvider from './base';

/**
* [Dash0](https://www.dash0.com/) telemetry provider.
*
* This currently requires use of the Dash0 Lambda Extension Layer. Functions
* instead instrumented with the OpenTelemetry Lambda stack are not supported.
*
* Use of Dash0 requires the `@opentelemetry/api` package to be added to your
* project dependencies.
*/
export default class Dash0Telemetry extends TelemetryProvider {
/**
* `true` if we can send telemetry to Dash0.
*
* The `DASH0_*` env vars will be present in functions instrumented using the
* Dash0 Lambda Extension Layer.
*
* Docs at https://www.dash0.com/hub/integrations/int_aws_lambda/overview
*/
static get isEnabled(): boolean {
return process.env.AWS_LAMBDA_EXEC_WRAPPER === '/opt/wrapper' && !!(
process.env.DASH0_ENDPOINT
&& process.env.DASH0_TOKEN
&& process.env.DASH0_DATASET
);
}

readonly trace: TraceAPI;

constructor(logger: LoggerService) {
super(logger);

// eslint-disable-next-line global-require, @typescript-eslint/no-var-requires
const opentelemetry = require('@opentelemetry/api');
this.trace = opentelemetry.trace;
}

error(error: Error): void {
this.trace.getActiveSpan()?.recordException(error);
}

label(label: string): void {
this.tag(label, true);
}

tag(key: string, value: any): void {
this.trace.getActiveSpan()?.setAttribute(key, value);
}
}
41 changes: 41 additions & 0 deletions tests/unit/services/LoggerService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Winston from 'winston';

import {
Context,
Dash0Telemetry,
DependencyInjection,
LoggerService,
LumigoTelemetry,
Expand Down Expand Up @@ -115,6 +116,21 @@ describe('unit.services.LoggerService', () => {

expect(errorSpy).toHaveBeenCalledWith(error, 'message');
});

it('should send the error to Dash0, if enabled', () => {
const errorSpy = jest.spyOn(Dash0Telemetry.prototype, 'error').mockImplementation(jest.fn());
jest.spyOn(Dash0Telemetry.prototype, 'tag').mockImplementation(jest.fn());
jest.spyOn(Dash0Telemetry.prototype, 'label').mockImplementation(jest.fn());
jest.spyOn(Dash0Telemetry, 'isEnabled', 'get').mockReturnValue(true);
// suppress log messages generated by this test
jest.spyOn(Winston.Logger.prototype, 'log').mockImplementation(jest.fn());

const logger = getLogger();
const error = new Error('bad stuff');
logger.error(error, 'message');

expect(errorSpy).toHaveBeenCalledWith(error, 'message');
});
});

describe('label', () => {
Expand All @@ -130,6 +146,19 @@ describe('unit.services.LoggerService', () => {

expect(labelSpy).toHaveBeenCalledWith('needs-investigating');
});

it('should send the label to Dash0, if enabled', () => {
const labelSpy = jest.spyOn(Dash0Telemetry.prototype, 'label').mockImplementation(jest.fn());
jest.spyOn(Dash0Telemetry.prototype, 'tag').mockImplementation(jest.fn());
jest.spyOn(Dash0Telemetry, 'isEnabled', 'get').mockReturnValue(true);
// suppress log messages generated by this test
jest.spyOn(Winston.Logger.prototype, 'log').mockImplementation(jest.fn());

const logger = getLogger();
logger.label('needs-investigating');

expect(labelSpy).toHaveBeenCalledWith('needs-investigating');
});
});

describe('metric', () => {
Expand All @@ -144,6 +173,18 @@ describe('unit.services.LoggerService', () => {

expect(tagSpy).toHaveBeenCalledWith('source', 'unit-test');
});

it('should send the tag to Dash0, if enabled', () => {
const tagSpy = jest.spyOn(Dash0Telemetry.prototype, 'tag').mockImplementation(jest.fn());
jest.spyOn(Dash0Telemetry, 'isEnabled', 'get').mockReturnValue(true);
// suppress log messages generated by this test
jest.spyOn(Winston.Logger.prototype, 'log').mockImplementation(jest.fn());

const logger = getLogger();
logger.metric('source', 'unit-test');

expect(tagSpy).toHaveBeenCalledWith('source', 'unit-test');
});
});

describe('info', () => {
Expand Down
40 changes: 40 additions & 0 deletions tests/unit/telemetry/Dash0.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {
Dash0Telemetry,
} from '@/src';

describe('unit.telemetry.Dash0', () => {
describe('isEnabled', () => {
describe('when all Dash0 env vars are present', () => {
beforeAll(() => {
process.env.AWS_LAMBDA_EXEC_WRAPPER = '/opt/wrapper';
process.env.DASH0_ENDPOINT = 'test';
process.env.DASH0_TOKEN = 'test';
process.env.DASH0_DATASET = 'test';
});

afterAll(() => {
delete process.env.AWS_LAMBDA_EXEC_WRAPPER;
delete process.env.DASH0_ENDPOINT;
delete process.env.DASH0_TOKEN;
delete process.env.DASH0_DATASET;
});

it('should return true', () => {
expect(Dash0Telemetry.isEnabled).toBe(true);
});
});

describe('when Dash0 env vars not are present', () => {
beforeAll(() => {
delete process.env.AWS_LAMBDA_EXEC_WRAPPER;
delete process.env.DASH0_ENDPOINT;
delete process.env.DASH0_TOKEN;
delete process.env.DASH0_DATASET;
});

it('should return false', () => {
expect(Dash0Telemetry.isEnabled).toBe(false);
});
});
});
});
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1764,6 +1764,11 @@
dependencies:
"@octokit/openapi-types" "^27.0.0"

"@opentelemetry/api@^1.9.1":
version "1.9.1"
resolved "https://registry.yarnpkg.com/@opentelemetry/api/-/api-1.9.1.tgz#c1b0346de336ba55af2d5a7970882037baedec05"
integrity sha512-gLyJlPHPZYdAk1JENA9LeHejZe1Ti77/pTeFm/nMXmQH/HFZlcS/O2XJB+L8fkbrNSqhdtlvjBVjxwUYanNH5Q==

"@pnpm/config.env-replace@^1.1.0":
version "1.1.0"
resolved "https://registry.yarnpkg.com/@pnpm/config.env-replace/-/config.env-replace-1.1.0.tgz#ab29da53df41e8948a00f2433f085f54de8b3a4c"
Expand Down
Loading