Skip to content

Commit 338db8b

Browse files
nicohrubecclaude
andcommitted
ref(node): Vendor @opentelemetry/sql-common
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 170161b commit 338db8b

5 files changed

Lines changed: 85 additions & 10 deletions

File tree

packages/node/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@
6868
"@opentelemetry/api": "^1.9.1",
6969
"@opentelemetry/core": "^2.6.1",
7070
"@opentelemetry/instrumentation": "^0.214.0",
71-
"@opentelemetry/sql-common": "^0.41.2",
7271
"@opentelemetry/sdk-trace-base": "^2.6.1",
7372
"@opentelemetry/semantic-conventions": "^1.40.0",
7473
"@sentry/core": "10.53.1",

packages/node/src/integrations/tracing/mysql2/vendored/instrumentation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import {
3333
semconvStabilityFromStr,
3434
} from '@opentelemetry/instrumentation';
3535
import { DB_SYSTEM_VALUE_MYSQL, ATTR_DB_STATEMENT, ATTR_DB_SYSTEM } from './semconv';
36-
import { addSqlCommenterComment } from '@opentelemetry/sql-common';
36+
import { addSqlCommenterComment } from '../../utils/sql-common';
3737
import type { Connection, Query, QueryOptions, QueryError, FieldPacket, FormatFunction } from './mysql2-types';
3838
import { MySQL2InstrumentationConfig } from './types';
3939
import { getConnectionAttributes, getConnectionPrototypeToInstrument, getQueryText, getSpanName, once } from './utils';

packages/node/src/integrations/tracing/postgres/vendored/instrumentation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ import {
5353
} from './internal-types';
5454
import { PgInstrumentationConfig } from './types';
5555
import * as utils from './utils';
56-
import { addSqlCommenterComment } from '@opentelemetry/sql-common';
56+
import { addSqlCommenterComment } from '../../utils/sql-common';
5757
import { SDK_VERSION } from '@sentry/core';
5858
const PACKAGE_NAME = '@sentry/instrumentation-pg';
5959
import { SpanNames } from './enums/SpanNames';
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* NOTICE from the Sentry authors:
17+
* - Vendored from: https://github.com/open-telemetry/opentelemetry-js-contrib/tree/5a5918fd4f9f16b14c9ef4d3de08ab98c20e5b46/packages/sql-common
18+
* - Upstream version: @opentelemetry/sql-common@0.41.2
19+
*/
20+
21+
/* eslint-disable */
22+
23+
import { trace, Span, ROOT_CONTEXT, defaultTextMapSetter } from '@opentelemetry/api';
24+
import { W3CTraceContextPropagator } from '@opentelemetry/core';
25+
26+
// NOTE: This function currently is returning false-positives
27+
// in cases where comment characters appear in string literals
28+
// ("SELECT '-- not a comment';" would return true, although has no comment)
29+
function hasValidSqlComment(query: string): boolean {
30+
const indexOpeningDashDashComment = query.indexOf('--');
31+
if (indexOpeningDashDashComment >= 0) {
32+
return true;
33+
}
34+
35+
const indexOpeningSlashComment = query.indexOf('/*');
36+
if (indexOpeningSlashComment < 0) {
37+
return false;
38+
}
39+
40+
const indexClosingSlashComment = query.indexOf('*/');
41+
return indexOpeningDashDashComment < indexClosingSlashComment;
42+
}
43+
44+
// sqlcommenter specification (https://google.github.io/sqlcommenter/spec/#value-serialization)
45+
// expects us to URL encode based on the RFC 3986 spec (https://en.wikipedia.org/wiki/Percent-encoding),
46+
// but encodeURIComponent does not handle some characters correctly (! ' ( ) *),
47+
// which means we need special handling for this
48+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent
49+
function fixedEncodeURIComponent(str: string) {
50+
return encodeURIComponent(str).replace(/[!'()*]/g, c => `%${c.charCodeAt(0).toString(16).toUpperCase()}`);
51+
}
52+
53+
export function addSqlCommenterComment(span: Span, query: string): string {
54+
if (typeof query !== 'string' || query.length === 0) {
55+
return query;
56+
}
57+
58+
// As per sqlcommenter spec we shall not add a comment if there already is a comment
59+
// in the query
60+
if (hasValidSqlComment(query)) {
61+
return query;
62+
}
63+
64+
const propagator = new W3CTraceContextPropagator();
65+
const headers: { [key: string]: string } = {};
66+
propagator.inject(trace.setSpan(ROOT_CONTEXT, span), headers, defaultTextMapSetter);
67+
68+
// sqlcommenter spec requires keys in the comment to be sorted lexicographically
69+
const sortedKeys = Object.keys(headers).sort();
70+
71+
if (sortedKeys.length === 0) {
72+
return query;
73+
}
74+
75+
const commentString = sortedKeys
76+
.map(key => {
77+
const encodedValue = fixedEncodeURIComponent(headers[key]!);
78+
return `${key}='${encodedValue}'`;
79+
})
80+
.join(',');
81+
82+
return `${query} /*${commentString}*/`;
83+
}

yarn.lock

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6114,13 +6114,6 @@
61146114
resolved "https://registry.yarnpkg.com/@opentelemetry/semantic-conventions/-/semantic-conventions-1.40.0.tgz#10b2944ca559386590683392022a897eefd011d3"
61156115
integrity sha512-cifvXDhcqMwwTlTK04GBNeIe7yyo28Mfby85QXFe1Yk8nmi36Ab/5UQwptOx84SsoGNRg+EVSjwzfSZMy6pmlw==
61166116

6117-
"@opentelemetry/sql-common@^0.41.2":
6118-
version "0.41.2"
6119-
resolved "https://registry.yarnpkg.com/@opentelemetry/sql-common/-/sql-common-0.41.2.tgz#7f4a14166cfd6c9ffe89096db1cc75eaf6443b19"
6120-
integrity sha512-4mhWm3Z8z+i508zQJ7r6Xi7y4mmoJpdvH0fZPFRkWrdp5fq7hhZ2HhYokEOLkfqSMgPR4Z9EyB3DBkbKGOqZiQ==
6121-
dependencies:
6122-
"@opentelemetry/core" "^2.0.0"
6123-
61246117
"@oxc-parser/binding-android-arm64@0.76.0":
61256118
version "0.76.0"
61266119
resolved "https://registry.yarnpkg.com/@oxc-parser/binding-android-arm64/-/binding-android-arm64-0.76.0.tgz#2bf8524add42f7a399ea0da9ae8e764bb9aeb61b"

0 commit comments

Comments
 (0)