Skip to content

Commit

Permalink
perf(tracer): reduce overhead of attribute sanitization
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuron authored and Ievgen Makukh committed Mar 20, 2024
1 parent 3a426e8 commit 519e021
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
12 changes: 7 additions & 5 deletions packages/opentelemetry-core/src/common/attributes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,26 @@

import { diag, SpanAttributeValue, SpanAttributes } from '@opentelemetry/api';

export function sanitizeAttributes(attributes: unknown): SpanAttributes {
const out: SpanAttributes = {};

export function sanitizeAttributes(
attributes: unknown,
out: SpanAttributes = {}
): SpanAttributes {
if (typeof attributes !== 'object' || attributes == null) {
return out;
}

for (const [key, val] of Object.entries(attributes)) {
for (const key in attributes) {
if (!isAttributeKey(key)) {
diag.warn(`Invalid attribute key: ${key}`);
continue;
}
const val = attributes[key as keyof typeof attributes] as unknown;
if (!isAttributeValue(val)) {
diag.warn(`Invalid attribute value set for key: ${key}`);
continue;
}
if (Array.isArray(val)) {
out[key] = val.slice();
out[key] = out[key] == val ? val : val.slice();
} else {
out[key] = val;
}
Expand Down
5 changes: 2 additions & 3 deletions packages/opentelemetry-sdk-trace-base/src/Tracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,8 @@ export class Tracer implements api.Tracer {

// Set initial span attributes. The attributes object may have been mutated
// by the sampler, so we sanitize the merged attributes before setting them.
const initAttributes = sanitizeAttributes(
Object.assign(attributes, samplingResult.attributes)
);
Object.assign(attributes, samplingResult.attributes);
const initAttributes = sanitizeAttributes(attributes, attributes);

const span = new Span(
this,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ const Benchmark = require('benchmark');
const { BasicTracerProvider } = require('../../../build/src');

const tracerProvider = new BasicTracerProvider();
const tracer = tracerProvider.getTracer('test')
const tracer = tracerProvider.getTracer('test');

const suite = new Benchmark.Suite();

suite.on('cycle', event => {
console.log(String(event.target));
});

suite.add('create spans (10 attributes)', function() {
suite.add('create spans (10 attributes)', function () {
const span = tracer.startSpan('span');
span.setAttribute('aaaaaaaaaaaaaaaaaaaa', 'aaaaaaaaaaaaaaaaaaaa');
span.setAttribute('bbbbbbbbbbbbbbbbbbbb', 'aaaaaaaaaaaaaaaaaaaa');
Expand All @@ -41,4 +41,21 @@ suite.add('create spans (10 attributes)', function() {
span.end();
});

suite.add('create spans (10 attributes) passed as options', function () {
const span = tracer.startSpan('span', {
attributes: {
aaaaaaaaaaaaaaaaaaaa: 'aaaaaaaaaaaaaaaaaaaa',
bbbbbbbbbbbbbbbbbbbb: 'aaaaaaaaaaaaaaaaaaaa',
cccccccccccccccccccc: 'aaaaaaaaaaaaaaaaaaaa',
dddddddddddddddddddd: 'aaaaaaaaaaaaaaaaaaaa',
eeeeeeeeeeeeeeeeeeee: 'aaaaaaaaaaaaaaaaaaaa',
ffffffffffffffffffff: 'aaaaaaaaaaaaaaaaaaaa',
gggggggggggggggggggg: 'aaaaaaaaaaaaaaaaaaaa',
hhhhhhhhhhhhhhhhhhhh: 'aaaaaaaaaaaaaaaaaaaa',
iiiiiiiiiiiiiiiiiiii: 'aaaaaaaaaaaaaaaaaaaa',
jjjjjjjjjjjjjjjjjjjj: 'aaaaaaaaaaaaaaaaaaaa',
},
});
});

suite.run();

0 comments on commit 519e021

Please sign in to comment.