Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ For notes on migrating to 2.x / 0.200.x see [the upgrade guide](doc/upgrade-to-2

### :house: Internal

* perf(otlp-transformer): optimize toAnyValue performance [#6287](https://github.com/open-telemetry/opentelemetry-js/pull/6287) @AbhiPrasad

## 0.211.0

### :boom: Breaking Changes
Expand Down
2 changes: 1 addition & 1 deletion experimental/packages/otlp-transformer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"submodule": "git submodule sync --recursive && git submodule update --init --recursive",
"test": "nyc mocha 'test/**/*.test.ts'",
"test:browser": "karma start --single-run",
"test:bench": "node test/performance/benchmark/index.js | tee .benchmark-results.txt",
"test:bench": "node test/performance/benchmark/micro-benchmarks.js && node test/performance/benchmark/index.js | tee .benchmark-results.txt",
"prewatch": "node ../../../scripts/version-update.js",
"watch": "npm run protos && tsc -w tsconfig.json tsconfig.esm.json tsconfig.esnext.json",
"peer-api-check": "node ../../../scripts/peer-api-check.js",
Expand Down
28 changes: 18 additions & 10 deletions experimental/packages/otlp-transformer/src/common/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,24 @@ export function toAnyValue(value: unknown): IAnyValue {
}
if (t === 'boolean') return { boolValue: value as boolean };
if (value instanceof Uint8Array) return { bytesValue: value };
if (Array.isArray(value))
return { arrayValue: { values: value.map(toAnyValue) } };
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Although v8 is normally great at handling Array.map, I'm guessing the recursion messes up how v8 can optimize the array allocation. The recursion in combination with the new closure also might cause issues. Still kind of surprised at this change though, was expecting to only have to adjust the object conditional.

I'm pretty sure the performance benefit from the object if case is just removing the usage of Object.entries (and the extra [k, v] destructure in .map().

if (t === 'object' && value != null)
return {
kvlistValue: {
values: Object.entries(value as object).map(([k, v]) =>
toKeyValue(k, v)
),
},
};
if (Array.isArray(value)) {
const values: IAnyValue[] = new Array(value.length);
for (let i = 0; i < value.length; i++) {
values[i] = toAnyValue(value[i]);
}
return { arrayValue: { values } };
}
if (t === 'object' && value != null) {
const keys = Object.keys(value);
const values: IKeyValue[] = new Array(keys.length);
for (let i = 0; i < keys.length; i++) {
values[i] = {
key: keys[i],
value: toAnyValue((value as Record<string, unknown>)[keys[i]]),
};
}
return { kvlistValue: { values } };
}

return {};
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,55 +14,4 @@
* limitations under the License.
*/

const Benchmark = require('benchmark');
const {
createExportTraceServiceRequest,
} = require('../../../build/src/trace/internal');
const { BasicTracerProvider } = require('@opentelemetry/sdk-trace-base');
const { ProtobufTraceSerializer } = require('../../../build/src');

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

const suite = new Benchmark.Suite();

const span = createSpan();
const spans = [];
for (let i = 0; i < 100; i++) {
spans.push(createSpan());
}

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

suite.add('transform 1 span', function () {
createExportTraceServiceRequest([span]);
});

suite.add('transform 100 spans', function () {
createExportTraceServiceRequest(spans);
});

suite.add('transform 100 spans to protobuf', function () {
ProtobufTraceSerializer.serializeRequest(spans);
});

suite.run();

function createSpan() {
const span = tracer.startSpan('span');
span.setAttribute('aaaaaaaaaaaaaaaaaaaa', 'aaaaaaaaaaaaaaaaaaaa');
span.setAttribute('bbbbbbbbbbbbbbbbbbbb', 'bbbbbbbbbbbbbbbbbbbb');
span.setAttribute('cccccccccccccccccccc', 'cccccccccccccccccccc');
span.setAttribute('dddddddddddddddddddd', 'dddddddddddddddddddd');
span.setAttribute('eeeeeeeeeeeeeeeeeeee', 'eeeeeeeeeeeeeeeeeeee');
span.setAttribute('ffffffffffffffffffff', 'ffffffffffffffffffff');
span.setAttribute('gggggggggggggggggggg', 'gggggggggggggggggggg');
span.setAttribute('hhhhhhhhhhhhhhhhhhhh', 'hhhhhhhhhhhhhhhhhhhh');
span.setAttribute('iiiiiiiiiiiiiiiiiiii', 'iiiiiiiiiiiiiiiiiiii');
span.setAttribute('jjjjjjjjjjjjjjjjjjjj', 'jjjjjjjjjjjjjjjjjjjj');
span.end();

return span;
}
require('./transform');
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

require('./toAnyValue');
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

const Benchmark = require('benchmark');
const { toAnyValue } = require('../../../build/src/common/internal');

const suite = new Benchmark.Suite();

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

suite.add('toAnyValue string', function () {
toAnyValue('test-string-value');
});

suite.add('toAnyValue integer', function () {
toAnyValue(42);
});

suite.add('toAnyValue array of strings', function () {
toAnyValue(['a', 'b', 'c', 'd', 'e']);
});

suite.add('toAnyValue array of objects', function () {
toAnyValue([{ a: 1 }, { b: 2 }, { c: 3 }]);
});

suite.add('toAnyValue nested object', function () {
toAnyValue({ level1: { level2: { level3: 'deep' } } });
});

suite.add('toAnyValue complex nested', function () {
toAnyValue({
a: { b: { c: { d: 'value' } } },
arr: [1, 2, 3],
mixed: { nested: [{ x: 1 }, { y: 2 }] },
});
});

suite.run();
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright The OpenTelemetry Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

const Benchmark = require('benchmark');
const {
createExportTraceServiceRequest,
} = require('../../../build/src/trace/internal');
const { BasicTracerProvider } = require('@opentelemetry/sdk-trace-base');
const { ProtobufTraceSerializer } = require('../../../build/src');

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

const suite = new Benchmark.Suite();

const span = createSpan();
const spans = [];
for (let i = 0; i < 100; i++) {
spans.push(createSpan());
}

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

suite.add('transform 1 span', function () {
createExportTraceServiceRequest([span]);
});

suite.add('transform 100 spans', function () {
createExportTraceServiceRequest(spans);
});

suite.add('transform 100 spans to protobuf', function () {
ProtobufTraceSerializer.serializeRequest(spans);
});

suite.run();

function createSpan() {
const span = tracer.startSpan('span');
span.setAttribute('aaaaaaaaaaaaaaaaaaaa', 'aaaaaaaaaaaaaaaaaaaa');
span.setAttribute('bbbbbbbbbbbbbbbbbbbb', 'bbbbbbbbbbbbbbbbbbbb');
span.setAttribute('cccccccccccccccccccc', 'cccccccccccccccccccc');
span.setAttribute('dddddddddddddddddddd', 'dddddddddddddddddddd');
span.setAttribute('eeeeeeeeeeeeeeeeeeee', 'eeeeeeeeeeeeeeeeeeee');
span.setAttribute('ffffffffffffffffffff', 'ffffffffffffffffffff');
span.setAttribute('gggggggggggggggggggg', 'gggggggggggggggggggg');
span.setAttribute('hhhhhhhhhhhhhhhhhhhh', 'hhhhhhhhhhhhhhhhhhhh');
span.setAttribute('iiiiiiiiiiiiiiiiiiii', 'iiiiiiiiiiiiiiiiiiii');
span.setAttribute('jjjjjjjjjjjjjjjjjjjj', 'jjjjjjjjjjjjjjjjjjjj');
span.end();

return span;
}