|
| 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 | +} |
0 commit comments