generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 15
Add Compact Console Log Record Exporter for Lambda Environment #236
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
jj22ee
merged 7 commits into
aws-observability:main
from
jj22ee:compressed-console-logs-exporter
Jul 31, 2025
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a279ef1
create console log exporter specific to Lambda environments
jj22ee 13b97c0
lint
jj22ee aced5fe
Merge branch 'main' into compressed-console-logs-exporter
jj22ee d78bd40
update compressed -> compact
jj22ee 3a05283
use process.stdout.write instead of console.log to ignore lambda requ…
jj22ee 5c2a1dc
update unit tests to validate entire logged string
jj22ee 7b80a69
Merge branch 'main' into compressed-console-logs-exporter
jj22ee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
...try-node-autoinstrumentation/src/exporter/console/logs/compressed-console-log-exporter.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| import { ExportResult, ExportResultCode } from '@opentelemetry/core'; | ||
| import { ConsoleLogRecordExporter, ReadableLogRecord } from '@opentelemetry/sdk-logs'; | ||
|
|
||
| export class CompressedConsoleLogRecordExporter extends ConsoleLogRecordExporter { | ||
| override export(logs: ReadableLogRecord[], resultCallback: (result: ExportResult) => void): void { | ||
| this._sendLogRecordsToLambdaConsole(logs, resultCallback); | ||
| } | ||
|
|
||
| private _sendLogRecordsToLambdaConsole(logRecords: ReadableLogRecord[], done?: (result: ExportResult) => void): void { | ||
| for (const logRecord of logRecords) { | ||
| console.log(this['_exportInfo'](logRecord)); | ||
| } | ||
| done?.({ code: ExportResultCode.SUCCESS }); | ||
| } | ||
| } | ||
124 changes: 124 additions & 0 deletions
124
...de-autoinstrumentation/test/exporter/console/logs/compressed-console-log-exporter.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import { expect } from 'expect'; | ||
| import * as sinon from 'sinon'; | ||
| import { ExportResultCode } from '@opentelemetry/core'; | ||
| import { ReadableLogRecord } from '@opentelemetry/sdk-logs'; | ||
| import { Resource } from '@opentelemetry/resources'; | ||
| import { CompressedConsoleLogRecordExporter } from '../../../../src/exporter/console/logs/compressed-console-log-exporter'; | ||
| import { Attributes } from '@opentelemetry/api'; | ||
|
|
||
| describe('CompressedConsoleLogRecordExporter', () => { | ||
| let exporter: CompressedConsoleLogRecordExporter; | ||
| let consoleLogSpy: sinon.SinonSpy; | ||
|
|
||
| const createMockLogRecord = (body: string, attributes: Attributes = {}): ReadableLogRecord => ({ | ||
| hrTime: [1640995200, 0], | ||
| hrTimeObserved: [1640995200, 0], | ||
| body, | ||
| severityText: 'INFO', | ||
| attributes, | ||
| resource: Resource.empty(), | ||
| instrumentationScope: { name: 'test', version: '1.0.0' }, | ||
| droppedAttributesCount: 0, | ||
| }); | ||
|
|
||
| beforeEach(() => { | ||
| exporter = new CompressedConsoleLogRecordExporter(); | ||
| consoleLogSpy = sinon.spy(console, 'log'); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| sinon.restore(); | ||
| }); | ||
|
|
||
| it('should export logs and call callback with success', done => { | ||
| const mockLogRecord = createMockLogRecord('test log message'); | ||
| const logs = [mockLogRecord]; | ||
|
|
||
| exporter.export(logs, result => { | ||
| expect(result.code).toBe(ExportResultCode.SUCCESS); | ||
| expect(consoleLogSpy.calledOnce).toBeTruthy(); | ||
|
|
||
| const loggedContent = consoleLogSpy.firstCall.args[0]; | ||
| expect(typeof loggedContent).toBe('object'); | ||
| expect(loggedContent.body).toBe('test log message'); | ||
| expect(loggedContent.severityText).toBe('INFO'); | ||
| expect(loggedContent.instrumentationScope.name).toBe('test'); | ||
| expect(loggedContent.instrumentationScope.version).toBe('1.0.0'); | ||
|
|
||
| done(); | ||
| }); | ||
| }); | ||
|
|
||
| it('should export multiple logs', done => { | ||
| const mockLogRecords = [createMockLogRecord('log 1'), createMockLogRecord('log 2')]; | ||
|
|
||
| exporter.export(mockLogRecords, result => { | ||
| expect(result.code).toBe(ExportResultCode.SUCCESS); | ||
| expect(consoleLogSpy.callCount).toBe(2); | ||
|
|
||
| const firstLogContent = consoleLogSpy.firstCall.args[0]; | ||
| const secondLogContent = consoleLogSpy.secondCall.args[0]; | ||
|
|
||
| expect(firstLogContent.body).toBe('log 1'); | ||
| expect(secondLogContent.body).toBe('log 2'); | ||
|
|
||
| done(); | ||
| }); | ||
| }); | ||
|
|
||
| it('should handle empty logs array', done => { | ||
| exporter.export([], result => { | ||
| expect(result.code).toBe(ExportResultCode.SUCCESS); | ||
| expect(consoleLogSpy.called).toBeFalsy(); | ||
| done(); | ||
| }); | ||
| }); | ||
|
|
||
| it('should work without callback', () => { | ||
| const mockLogRecord = createMockLogRecord('test log message'); | ||
|
|
||
| expect(() => { | ||
| exporter.export([mockLogRecord], () => {}); | ||
| }).not.toThrow(); | ||
| expect(consoleLogSpy.calledOnce).toBeTruthy(); | ||
|
|
||
| const loggedContent = consoleLogSpy.firstCall.args[0]; | ||
| expect(loggedContent.body).toBe('test log message'); | ||
| }); | ||
|
|
||
| it('should handle undefined callback gracefully', () => { | ||
| const mockLogRecord = createMockLogRecord('test log message'); | ||
|
|
||
| expect(() => { | ||
| exporter['_sendLogRecordsToLambdaConsole']([mockLogRecord]); | ||
| }).not.toThrow(); | ||
| expect(consoleLogSpy.calledOnce).toBeTruthy(); | ||
|
|
||
| const loggedContent = consoleLogSpy.firstCall.args[0]; | ||
| expect(loggedContent.body).toBe('test log message'); | ||
| }); | ||
|
|
||
| it('should format log record with all expected fields', done => { | ||
| const mockLogRecord = createMockLogRecord('detailed test message', { | ||
| customKey: 'customValue', | ||
| requestId: '12345', | ||
| }); | ||
|
|
||
| exporter.export([mockLogRecord], result => { | ||
| expect(result.code).toBe(ExportResultCode.SUCCESS); | ||
|
|
||
| const loggedContent = consoleLogSpy.firstCall.args[0]; | ||
| expect(typeof loggedContent).toBe('object'); | ||
| expect(loggedContent.body).toBe('detailed test message'); | ||
| expect(loggedContent.severityText).toBe('INFO'); | ||
| expect(loggedContent.instrumentationScope.name).toBe('test'); | ||
| expect(loggedContent.instrumentationScope.version).toBe('1.0.0'); | ||
| expect(loggedContent.attributes).toEqual({ customKey: 'customValue', requestId: '12345' }); | ||
|
|
||
| done(); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.