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

perf(tracer): reduce overhead of attribute sanitization #4558

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some environments (runtimes) where the usage of x in y can cause issues where the only additional fix is to also use Object.hasOwnProperty so this may have unexpected (endless looping) consequences.

It's been a while since I saw this previously, but (from memory) it is caused by a 3rd party library adding stuff to Object to support the runtime environment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I propose to add Object.hasOwnProperty then, I think allocations is more of a problem than sheer performance

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added propertyIsEnumerable check

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();