Skip to content

Commit

Permalink
feat(sdk-logs): add droppedAttributesCount in ReadableLogRecord (open…
Browse files Browse the repository at this point in the history
  • Loading branch information
HyunnoH committed Oct 9, 2023
1 parent c320c98 commit f1898bf
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 11 deletions.
1 change: 1 addition & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ All notable changes to experimental packages in this project will be documented

* feat(exporter-metrics-otlp-proto): add esm build [#4099](https://github.com/open-telemetry/opentelemetry-js/pull/4099) @pichlermarc
* feat(opencensus-shim): implement OpenCensus metric producer [#4066](https://github.com/open-telemetry/opentelemetry-js/pull/4066) @aabmass
* feat(sdk-logs): add droppedAttributesCount in ReadableLogRecord [#3758](https://github.com/open-telemetry/opentelemetry-js/issues/3758) @hyunnoh

### :bug: (Bug Fix)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export const mockedReadableLogRecord: ReadableLogRecord = {
traceId: '1f1008dc8e270e85c40a0d7c3939b278',
spanId: '5e107261f64fa53e',
},
droppedAttributesCount: 0,
};

export function ensureExportedAttributesAreCorrect(attributes: IKeyValue[]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export const mockedReadableLogRecord: ReadableLogRecord = {
traceId: '1f1008dc8e270e85c40a0d7c3939b278',
spanId: '5e107261f64fa53e',
},
droppedAttributesCount: 0,
};
export function ensureExportedAttributesAreCorrect(attributes: IKeyValue[]) {
assert.deepStrictEqual(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export const mockedReadableLogRecord: ReadableLogRecord = {
traceId: '1f1008dc8e270e85c40a0d7c3939b278',
spanId: '5e107261f64fa53e',
},
droppedAttributesCount: 0,
};
export function ensureExportedAttributesAreCorrect(attributes: IKeyValue[]) {
assert.deepStrictEqual(
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 @@ -116,13 +116,15 @@ describe('Logs', () => {
traceFlags: TraceFlags.SAMPLED,
traceId: '00000000000000000000000000000001',
},
droppedAttributesCount: 0,
};
const log_fragment_2 = {
hrTime: [1680253797, 687038506] as HrTime,
hrTimeObserved: [1680253797, 687038506] as HrTime,
attributes: {
'another-attribute': 'another attribute value',
},
droppedAttributesCount: 0,
};
log_1_1_1 = {
...log_fragment_1,
Expand Down
28 changes: 19 additions & 9 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() {
return this._totalAttributesCount - Object.keys(this.attributes).length;
}

constructor(
_sharedState: LoggerProviderSharedState,
instrumentationScope: InstrumentationScope,
Expand Down Expand Up @@ -114,29 +119,34 @@ export class LogRecord implements ReadableLogRecord {
if (value === null) {
return this;
}
if (
typeof value === 'object' &&
!Array.isArray(value) &&
Object.keys(value).length > 0
) {
this.attributes[key] = value;
}
if (key.length === 0) {
api.diag.warn(`Invalid attribute key: ${key}`);
return this;
}
if (!isAttributeValue(value)) {
if (
!isAttributeValue(value) &&
!(
typeof value === 'object' &&
!Array.isArray(value) &&
Object.keys(value).length > 0
)
) {
api.diag.warn(`Invalid attribute value set for key: ${key}`);
return this;
}
this._totalAttributesCount += 1;
if (
Object.keys(this.attributes).length >=
this._logRecordLimits.attributeCountLimit &&
!Object.prototype.hasOwnProperty.call(this.attributes, key)
) {
return this;
}
this.attributes[key] = this._truncateToSize(value);
if (isAttributeValue(value)) {
this.attributes[key] = this._truncateToSize(value);
} else {
this.attributes[key] = value;
}
return this;
}

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;
}
24 changes: 22 additions & 2 deletions experimental/packages/sdk-logs/test/common/LogRecord.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,14 +179,34 @@ describe('LogRecord', () => {
describe('when "attributeCountLimit" option defined', () => {
const { logRecord } = setup({ attributeCountLimit: 100 });
for (let i = 0; i < 150; i++) {
logRecord.setAttribute(`foo${i}`, `bar${i}`);
let attributeValue;
switch (i % 3) {
case 0: {
attributeValue = `bar${i}`;
break;
}
case 1: {
attributeValue = [`bar${i}`];
break;
}
case 2: {
attributeValue = {
bar: `bar${i}`,
};
break;
}
default: {
attributeValue = `bar${i}`;
}
}
logRecord.setAttribute(`foo${i}`, attributeValue);
}

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

0 comments on commit f1898bf

Please sign in to comment.