Skip to content
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ For notes on migrating to 2.x / 0.200.x see [the upgrade guide](doc/upgrade-to-2
* refactor: replace assertRejects() with assert.rejects() [#5614](https://github.com/open-telemetry/opentelemetry-js/pull/5614) @cjihrig
* refactor(core): migrate from deprecated semconv constants [#5575](https://github.com/open-telemetry/opentelemetry-js/pull/5575) @alumni55748
* refactor(opentelemetry-core): simplify `parseKeyPairsIntoRecord()` [#5610](https://github.com/open-telemetry/opentelemetry-js/pull/5610) @cjihrig
* refactor(opentelemetry-core): simplify `parsePairKeyValue()` [#5885](https://github.com/open-telemetry/opentelemetry-js/pull/5885) @sivakumarsc

## 2.0.0

Expand Down
1 change: 1 addition & 0 deletions packages/opentelemetry-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"version": "node ../../scripts/version-update.js",
"watch": "tsc --build --watch tsconfig.json tsconfig.esm.json tsconfig.esnext.json",
"prewatch": "npm run precompile",
"test:bench": "node test/performance/benchmark/index.js",
"peer-api-check": "node ../../scripts/peer-api-check.js",
"align-api-deps": "node ../../scripts/align-api-deps.js"
},
Expand Down
42 changes: 28 additions & 14 deletions packages/opentelemetry-core/src/baggage/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,24 +57,38 @@ export function getKeyPairs(baggage: Baggage): string[] {
export function parsePairKeyValue(
entry: string
): ParsedBaggageKeyValue | undefined {
const valueProps = entry.split(BAGGAGE_PROPERTIES_SEPARATOR);
if (valueProps.length <= 0) return;
const keyPairPart = valueProps.shift();
if (!keyPairPart) return;
if (!entry) return;
const metadataSeparatorIndex = entry.indexOf(BAGGAGE_PROPERTIES_SEPARATOR);
const keyPairPart =
metadataSeparatorIndex === -1
? entry
: entry.substring(0, metadataSeparatorIndex);

const separatorIndex = keyPairPart.indexOf(BAGGAGE_KEY_PAIR_SEPARATOR);
if (separatorIndex <= 0) return;
const key = decodeURIComponent(
keyPairPart.substring(0, separatorIndex).trim()
);
const value = decodeURIComponent(
keyPairPart.substring(separatorIndex + 1).trim()
);

const rawKey = keyPairPart.substring(0, separatorIndex).trim();
const rawValue = keyPairPart.substring(separatorIndex + 1).trim();

if (!rawKey || !rawValue) return;
let key: string;
let value: string;
try {
key = decodeURIComponent(rawKey);
value = decodeURIComponent(rawValue);
} catch {
return;
}

let metadata;
if (valueProps.length > 0) {
metadata = baggageEntryMetadataFromString(
valueProps.join(BAGGAGE_PROPERTIES_SEPARATOR)
);
if (
metadataSeparatorIndex !== -1 &&
metadataSeparatorIndex < entry.length - 1
) {
const metadataString = entry.substring(metadataSeparatorIndex + 1);
metadata = baggageEntryMetadataFromString(metadataString);
}

return { key, value, metadata };
}

Expand Down
17 changes: 17 additions & 0 deletions packages/opentelemetry-core/test/performance/benchmark/index.js
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('./parsePairKeyValue');
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* 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 { parsePairKeyValue } = require('../../../build/src/baggage/utils');
const { baggageEntryMetadataFromString } = require('@opentelemetry/api');

// Constants
const BAGGAGE_PROPERTIES_SEPARATOR = ';';
const BAGGAGE_KEY_PAIR_SEPARATOR = '=';

// Old implementation for comparison
function parsePairKeyValue_Old(entry) {
const valueProps = entry.split(BAGGAGE_PROPERTIES_SEPARATOR);
if (valueProps.length <= 0) return;
const keyPairPart = valueProps.shift();
if (!keyPairPart) return;
const separatorIndex = keyPairPart.indexOf(BAGGAGE_KEY_PAIR_SEPARATOR);
if (separatorIndex <= 0) return;
const key = decodeURIComponent(
keyPairPart.substring(0, separatorIndex).trim()
);
const value = decodeURIComponent(
keyPairPart.substring(separatorIndex + 1).trim()
);
let metadata;
if (valueProps.length > 0) {
metadata = baggageEntryMetadataFromString(
valueProps.join(BAGGAGE_PROPERTIES_SEPARATOR)
);
}
return { key, value, metadata };
}

const suite = new Benchmark.Suite();

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

// Simple key-value pairs
suite.add('parsePairKeyValue simple (old)', function() {
parsePairKeyValue_Old('key1=value1');
});

suite.add('parsePairKeyValue simple (new)', function() {
parsePairKeyValue('key1=value1');
});

// Key-value with metadata
suite.add('parsePairKeyValue with metadata (old)', function() {
parsePairKeyValue_Old('key1=value1;metadata=sample');
});

suite.add('parsePairKeyValue with metadata (new)', function() {
parsePairKeyValue('key1=value1;metadata=sample');
});

// URI encoded values
suite.add('parsePairKeyValue URI encoded (old)', function() {
parsePairKeyValue_Old('user%20id=john%20doe');
});

suite.add('parsePairKeyValue URI encoded (new)', function() {
parsePairKeyValue('user%20id=john%20doe');
});

// Complex case
suite.add('parsePairKeyValue complex (old)', function() {
parsePairKeyValue_Old('user%20id=john%20doe;metadata=user%20info;tenant=prod');
});

suite.add('parsePairKeyValue complex (new)', function() {
parsePairKeyValue('user%20id=john%20doe;metadata=user%20info;tenant=prod');
});

suite.run();