Skip to content
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

feat(sdk-logs): add droppedAttributesCount field to ReadableLogRecord #4289

Merged
merged 7 commits into from
Dec 5, 2023
2 changes: 2 additions & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ All notable changes to experimental packages in this project will be documented

### :rocket: (Enhancement)

* feat(sdk-logs): add droppedAttributesCount field to ReadableLogRecord

### :bug: (Bug Fix)

* fix(sdk-logs): avoid map attribute set when count limit exceeded
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export const mockedReadableLogRecord: ReadableLogRecord = {
attributes: {
'some-attribute': 'some attribute value',
},
droppedAttributesCount: 0,
severityNumber: SeverityNumber.ERROR,
severityText: 'error',
body: 'some_log_body',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export const mockedReadableLogRecord: ReadableLogRecord = {
attributes: {
'some-attribute': 'some attribute value',
},
droppedAttributesCount: 0,
severityNumber: SeverityNumber.ERROR,
severityText: 'error',
body: 'some_log_body',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export const mockedReadableLogRecord: ReadableLogRecord = {
attributes: {
'some-attribute': 'some attribute value',
},
droppedAttributesCount: 0,
severityNumber: SeverityNumber.ERROR,
severityText: 'error',
body: 'some_log_body',
Expand Down
2 changes: 2 additions & 0 deletions experimental/packages/otlp-transformer/test/logs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ describe('Logs', () => {
attributes: {
'some-attribute': 'some attribute value',
},
droppedAttributesCount: 0,
severityNumber: SeverityNumber.ERROR,
severityText: 'error',
body: 'some_log_body',
Expand All @@ -122,6 +123,7 @@ describe('Logs', () => {
attributes: {
'another-attribute': 'another attribute value',
},
droppedAttributesCount: 0,
};
log_1_1_1 = {
...log_fragment_1,
Expand Down
6 changes: 6 additions & 0 deletions experimental/packages/sdk-logs/src/LogRecord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export class LogRecord implements ReadableLogRecord {
private _severityText?: string;
private _severityNumber?: logsAPI.SeverityNumber;
private _body?: string;
private totalAttributesCount: number = 0;

private _isReadonly: boolean = false;
private readonly _logRecordLimits: Required<LogRecordLimits>;
Expand Down Expand Up @@ -73,6 +74,10 @@ export class LogRecord implements ReadableLogRecord {
return this._body;
}

get droppedAttributesCount(): number {
return this.totalAttributesCount - Object.keys(this.attributes).length;
}

constructor(
_sharedState: LoggerProviderSharedState,
instrumentationScope: InstrumentationScope,
Expand Down Expand Up @@ -129,6 +134,7 @@ export class LogRecord implements ReadableLogRecord {
api.diag.warn(`Invalid attribute value set for key: ${key}`);
return this;
}
this.totalAttributesCount += 1;
if (
Object.keys(this.attributes).length >=
this._logRecordLimits.attributeCountLimit &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ export interface ReadableLogRecord {
readonly resource: IResource;
readonly instrumentationScope: InstrumentationScope;
readonly attributes: LogAttributes;
readonly droppedAttributesCount: number;
}
Original file line number Diff line number Diff line change
Expand Up @@ -203,13 +203,14 @@ describe('LogRecord', () => {
}

it('should remove / drop all remaining values after the number of values exceeds this limit', () => {
const { attributes } = logRecord;
const { attributes, droppedAttributesCount } = logRecord;
assert.strictEqual(Object.keys(attributes).length, 100);
assert.strictEqual(attributes.foo0, 'bar0');
assert.deepStrictEqual(attributes.foo98, { bar: 'bar98' });
assert.strictEqual(attributes.foo147, undefined);
assert.strictEqual(attributes.foo148, undefined);
assert.strictEqual(attributes.foo149, undefined);
assert.strictEqual(droppedAttributesCount, 50);
});
});

Expand Down