Skip to content

Commit 7c19ead

Browse files
nicohrubecclaude
andauthored
ref(node): Streamline sql-common (#21360)
Streamlines the vendored `@opentelemetry/sql-common`: - Port upstream unit tests from OTel and expanded the test suite with two more test cases. - Removed the eslint-disable and updated the formatting, else nothing to clean up. Closes #21131 Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 95df562 commit 7c19ead

2 files changed

Lines changed: 110 additions & 3 deletions

File tree

packages/node/src/integrations/tracing/utils/sql-common.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@
1818
* - Upstream version: @opentelemetry/sql-common@0.41.2
1919
*/
2020

21-
/* eslint-disable */
22-
23-
import { trace, Span, ROOT_CONTEXT, defaultTextMapSetter } from '@opentelemetry/api';
21+
import type { Span } from '@opentelemetry/api';
22+
import { defaultTextMapSetter, ROOT_CONTEXT, trace } from '@opentelemetry/api';
2423
import { W3CTraceContextPropagator } from '@opentelemetry/core';
2524

2625
// NOTE: This function currently is returning false-positives
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* Tests ported from @opentelemetry/sql-common@0.41.2
3+
* Original source: https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/packages/sql-common
4+
* Licensed under the Apache License, Version 2.0
5+
*/
6+
7+
import type { SpanContext } from '@opentelemetry/api';
8+
import { createTraceState, INVALID_SPAN_CONTEXT, trace, TraceFlags } from '@opentelemetry/api';
9+
import { describe, expect, it } from 'vitest';
10+
import { addSqlCommenterComment } from '../../../src/integrations/tracing/utils/sql-common';
11+
12+
describe('addSqlCommenterComment', () => {
13+
it('adds comment to a simple query', () => {
14+
const spanContext: SpanContext = {
15+
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
16+
spanId: '6e0c63257de34c92',
17+
traceFlags: TraceFlags.SAMPLED,
18+
};
19+
20+
const query = 'SELECT * from FOO;';
21+
expect(addSqlCommenterComment(trace.wrapSpanContext(spanContext), query)).toBe(
22+
"SELECT * from FOO; /*traceparent='00-d4cda95b652f4a1592b449d5929fda1b-6e0c63257de34c92-01'*/",
23+
);
24+
});
25+
26+
it('does not add a comment if query already has a comment', () => {
27+
const span = trace.wrapSpanContext({
28+
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
29+
spanId: '6e0c63257de34c92',
30+
traceFlags: TraceFlags.SAMPLED,
31+
});
32+
33+
const blockComment = 'SELECT * from FOO; /* Test comment */';
34+
expect(addSqlCommenterComment(span, blockComment)).toBe(blockComment);
35+
36+
const dashedComment = 'SELECT * from FOO; -- Test comment';
37+
expect(addSqlCommenterComment(span, dashedComment)).toBe(dashedComment);
38+
});
39+
40+
it('does not add a comment to an empty query', () => {
41+
const spanContext: SpanContext = {
42+
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
43+
spanId: '6e0c63257de34c92',
44+
traceFlags: TraceFlags.SAMPLED,
45+
};
46+
47+
expect(addSqlCommenterComment(trace.wrapSpanContext(spanContext), '')).toBe('');
48+
});
49+
50+
it('does not add a comment if span context is invalid', () => {
51+
const query = 'SELECT * from FOO;';
52+
expect(addSqlCommenterComment(trace.wrapSpanContext(INVALID_SPAN_CONTEXT), query)).toBe(query);
53+
});
54+
55+
it('correctly also sets trace state', () => {
56+
const spanContext: SpanContext = {
57+
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
58+
spanId: '6e0c63257de34c92',
59+
traceFlags: TraceFlags.SAMPLED,
60+
traceState: createTraceState('foo=bar,baz=qux'),
61+
};
62+
63+
const query = 'SELECT * from FOO;';
64+
expect(addSqlCommenterComment(trace.wrapSpanContext(spanContext), query)).toBe(
65+
"SELECT * from FOO; /*traceparent='00-d4cda95b652f4a1592b449d5929fda1b-6e0c63257de34c92-01',tracestate='foo%3Dbar%2Cbaz%3Dqux'*/",
66+
);
67+
});
68+
69+
it('escapes special characters in values', () => {
70+
const spanContext: SpanContext = {
71+
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
72+
spanId: '6e0c63257de34c92',
73+
traceFlags: TraceFlags.SAMPLED,
74+
traceState: createTraceState("foo='bar,baz='qux!()*',hack='DROP TABLE"),
75+
};
76+
77+
const query = 'SELECT * from FOO;';
78+
expect(addSqlCommenterComment(trace.wrapSpanContext(spanContext), query)).toBe(
79+
"SELECT * from FOO; /*traceparent='00-d4cda95b652f4a1592b449d5929fda1b-6e0c63257de34c92-01',tracestate='foo%3D%27bar%2Cbaz%3D%27qux%21%28%29%2A%27%2Chack%3D%27DROP%20TABLE'*/",
80+
);
81+
});
82+
83+
// Known limitation: `--` inside a string literal is treated as an
84+
// existing comment, so no sqlcommenter comment is appended.
85+
it('does not add a comment when -- appears inside a string literal', () => {
86+
const span = trace.wrapSpanContext({
87+
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
88+
spanId: '6e0c63257de34c92',
89+
traceFlags: TraceFlags.SAMPLED,
90+
});
91+
92+
const query = "SELECT '-- not a comment';";
93+
expect(addSqlCommenterComment(span, query)).toBe(query);
94+
});
95+
96+
it('adds a comment when an opening /* has no closing */', () => {
97+
const spanContext: SpanContext = {
98+
traceId: 'd4cda95b652f4a1592b449d5929fda1b',
99+
spanId: '6e0c63257de34c92',
100+
traceFlags: TraceFlags.SAMPLED,
101+
};
102+
103+
const query = 'SELECT 1 /* unclosed';
104+
expect(addSqlCommenterComment(trace.wrapSpanContext(spanContext), query)).toBe(
105+
`${query} /*traceparent='00-d4cda95b652f4a1592b449d5929fda1b-6e0c63257de34c92-01'*/`,
106+
);
107+
});
108+
});

0 commit comments

Comments
 (0)