From fc4a2deab5b00c57bf3d318989fdb22b724d0926 Mon Sep 17 00:00:00 2001 From: Emmanuel T Odeke Date: Sun, 22 Dec 2024 04:35:45 -0800 Subject: [PATCH] Remove accidentally committed code --- src/.instrumentation-spanner/index.ts | 16 -- .../instrumentation.ts | 204 ------------------ src/.instrumentation-spanner/old-index_ts | 99 --------- src/.instrumentation-spanner/types.ts | 25 --- src/.instrumentation-spanner/version.ts | 18 -- test/common/helper.ts | 44 ---- 6 files changed, 406 deletions(-) delete mode 100644 src/.instrumentation-spanner/index.ts delete mode 100644 src/.instrumentation-spanner/instrumentation.ts delete mode 100644 src/.instrumentation-spanner/old-index_ts delete mode 100644 src/.instrumentation-spanner/types.ts delete mode 100644 src/.instrumentation-spanner/version.ts delete mode 100644 test/common/helper.ts diff --git a/src/.instrumentation-spanner/index.ts b/src/.instrumentation-spanner/index.ts deleted file mode 100644 index 3efe082da..000000000 --- a/src/.instrumentation-spanner/index.ts +++ /dev/null @@ -1,16 +0,0 @@ -// Copyright 2024 Google LLC -// -// 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 -// -// http://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. - -export * from './instrumentation'; -export * from './types'; diff --git a/src/.instrumentation-spanner/instrumentation.ts b/src/.instrumentation-spanner/instrumentation.ts deleted file mode 100644 index 3ca936bc9..000000000 --- a/src/.instrumentation-spanner/instrumentation.ts +++ /dev/null @@ -1,204 +0,0 @@ -// Copyright 2024 Google LLC -// -// 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 -// -// http://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. - -import { - InstrumentationBase, - InstrumentationNodeModuleDefinition, - isWrapped, -} from '@opentelemetry/instrumentation'; -import { - SEMATTRS_DB_SQL_TABLE, - SEMATTRS_DB_STATEMENT, - SEMATTRS_DB_SYSTEM, -} from '@opentelemetry/semantic-conventions'; -import { - Span, - SpanStatusCode, - Context, - context, - trace, - SpanKind, -} from '@opentelemetry/api'; -import {PACKAGE_NAME, PACKAGE_VERSION} from './version'; -import type * as spannerTypes from '..'; -import {SpannerInstrumentationConfig} from './types'; - -const optedInPII: boolean = - process.env.SPANNER_ENABLE_EXTENDED_TRACING === 'true'; - -interface SQLStatement { - sql: string; -} - -const {DiagConsoleLogger, DiagLogLevel, diag} = require('@opentelemetry/api'); -diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.ALL); - -export class SpannerInstrumentation extends InstrumentationBase { - static readonly COMMON_ATTRIBUTES = { - SEMATTRS_DB_SYSTEM: 'spanner', - }; - - constructor(config: SpannerInstrumentationConfig = {}) { - super(PACKAGE_NAME, PACKAGE_VERSION, config); - } - - protected init() { - console.log('SpannerInstrumentation.init invoked'); - - return [ - new InstrumentationNodeModuleDefinition( - '@google-cloud/spanner', - ['*'], - (moduleExports: typeof spannerTypes) => { - console.log('invoking wrapping'); - if (isWrapped(moduleExports.Instance.database)) { - this._unwrap(moduleExports, 'Instance'); - } - this._wrap( - moduleExports, - 'Instance', - this._patchCreateDatabase() as any - ); - } - ), - ]; - } - - private _patchCreateDatabase() { - console.log('_patchCreateDatabase'); - return (originalCreateDatabase: Function) => { - console.log('wrapping for patchCreateDatabase'); - const plugin = this; - - return function createDatabase() { - const db = originalCreateDatabase(...arguments); - console.log('createDatabase'); - - plugin._wrap(db, 'run', plugin._patchDatabaseRun(db) as any); - - return db; - }; - }; - } - - private _patchDatabaseRun(db: typeof spannerTypes.Database) { - return (originalDatabaseRun: Function) => { - const plugin = this; - - return function databaseRun() { - if (!plugin['_enabled']) { - plugin._unwrap(db, 'run'); - return originalDatabaseRun.apply(db, arguments); - } - - const query = arguments[0] || ''; - const cbIndex = Array.from(arguments).findIndex( - arg => typeof arg === 'function' - ); - const span = plugin.startTrace('Database.run', {sql: query}); - - const parentContext = context.active(); - if (cbIndex === -1) { - // We've got a promise not a callback. - const streamableQuery = context.with( - trace.setSpan(context.active(), span), - () => { - return originalDatabaseRun.apply(db, arguments); - } - ); - context.bind(parentContext, streamableQuery); - - return streamableQuery - .on('error', err => { - setSpanError(span, err); - }) - .on('end', () => { - span.end(); - }); - } - - // Here we've got a callback hence can wrap it differently. - plugin._wrap( - arguments, - cbIndex, - plugin._patchCallbackRun(span, parentContext) - ); - - return context.with(trace.setSpan(context.active(), span), () => { - return originalDatabaseRun.apply(db, arguments); - }); - }; - }; - } - - private _patchCallbackRun(span: Span, parentContext: Context) { - return (originalCallback: Function) => { - return (err: Error, rows: any) => { - setSpanError(span, err); - span.end(); - return context.with(parentContext, () => { - originalCallback(...arguments); - }); - }; - }; - } - - private startTrace( - spanNameSuffix: string, - opts: {tableName?: string; sql?: string | SQLStatement} - ): Span { - const span = this.tracer.startSpan( - 'cloud.google.com/nodejs/spanner/' + spanNameSuffix, - {kind: SpanKind.CLIENT} - ); - - if (opts.tableName) { - span.setAttribute(SEMATTRS_DB_SQL_TABLE, opts.tableName); - } - - const definedExtendedTracing = - this._config.enableExtendedTracing !== undefined; - // If they optedInPII but opts.enableExtendedTracing=false, reject it. - const explicitlySkipET = - definedExtendedTracing && !this._config.enableExtendedTracing; - if ( - opts.sql && - !explicitlySkipET && - (this._config.enableExtendedTracing || optedInPII) - ) { - const sql = opts.sql; - if (typeof sql === 'string') { - span.setAttribute(SEMATTRS_DB_STATEMENT, sql as string); - } else { - const stmt = sql as SQLStatement; - span.setAttribute(SEMATTRS_DB_STATEMENT, stmt.sql); - } - } - - return span; - } -} - -// setSpanError sets err, if non-nil onto the span with -// status.code=ERROR and the message of err.toString() -function setSpanError(span: Span, err: Error | String) { - if (!err || !span) { - return; - } - - span.setStatus({ - code: SpanStatusCode.ERROR, - message: err.toString(), - }); -} diff --git a/src/.instrumentation-spanner/old-index_ts b/src/.instrumentation-spanner/old-index_ts deleted file mode 100644 index c1cdfb51c..000000000 --- a/src/.instrumentation-spanner/old-index_ts +++ /dev/null @@ -1,99 +0,0 @@ -// Copyright 2024 Google LLC -// -// 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. - -import { - InstrumentationBase, - InstrumentationConfig, - InstrumentationNodeModuleDefinition, - InstrumentationNodeModuleFile, -} from '@opentelemetry/instrumentation'; - -import { - context, - trace, - Span, -} from '@opentelemetry/api'; - -export class SpannerInstrumentation extends InstrumentationBase { - constructor(config: InstrumentationConfig = {}) { - super('@google-cloud/spanner', 'v1.0.0', config); - } - - /* - * The init method will be called when the plugin is constructed. - * It returns an IntrumentationNodeModuleDefinition which describes - * the node module to be instrumented and patched. - * It might also return a list of InstrumentationNodeModuleDefinitions if - * the plugin should patch multiple omodules or versions. - */ - protected init(): [InstrumentationNodeModuleDefinition] { - console.log('spanner-instrumentation invoked'); - - return [ - new InstrumentationNodeModuleDefinition( - '@google-cloud/spanner', - ['*'], - moduleExports => { - this._wrap( - moduleExports.Spanner.prototype, - }), - ]; - } - - private _onPatchMain(moduleExports: typeof Database) { - console.log('_onPatchMain'); - this._wrap( - moduleExports.Database.prototype, - 'run', - this._patchMainMethodName()); - return moduleExports; - } - - private _onUnPatchMain(moduleExports: typeof Database) { - this._unwrap(moduleExports, 'run'); - } - - private _onPatchMethodName(moduleExports: typeof Database) { - console.log('_onPatchMethodName'); - this._wrap( - moduleExports, - 'run', - this._patchMethodName()); - return moduleExports; - } - - private _onUnPatchMethodName(moduleExports: typeof Database) { - this._unwrap(moduleExports, 'run'); - } - - private _patchMethodName(): (original) => any { - const plugin = this; - return function methodName(original) { - return function patchMethodName(this: any): Promise { - console.log('methodName', arguments); - return original.apply(this, arguments); - } - } - } - - private _patchMainMethodName(): (original) => any { - const plugin = this; - return function mainMethodName(original) { - return function patchMainMethodName(this: any): Promise { - console.log('mainMethodName', arguments); - return original.apply(this, arguments); - } - } - } -} diff --git a/src/.instrumentation-spanner/types.ts b/src/.instrumentation-spanner/types.ts deleted file mode 100644 index 18098cb7e..000000000 --- a/src/.instrumentation-spanner/types.ts +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright 2024 Google LLC -// -// 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 -// -// http://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. - -import {InstrumentationConfig} from '@opentelemetry/instrumentation'; - -export interface SpannerInstrumentationConfig extends InstrumentationConfig { - // enableExtendedTracing when set to true allows spans to be - // annotated with the SQL being executed, where applicable. - enableExtendedTracing?: boolean; -} - -export interface DatabaseKind { - run(): any; -} diff --git a/src/.instrumentation-spanner/version.ts b/src/.instrumentation-spanner/version.ts deleted file mode 100644 index e3b95e7d9..000000000 --- a/src/.instrumentation-spanner/version.ts +++ /dev/null @@ -1,18 +0,0 @@ -// Copyright 2024 Google LLC -// -// 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 -// -// http://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 PACKAGE_NAME = 'spanner'; -const PACKAGE_VERSION = 'v1.0.0'; - -export {PACKAGE_NAME, PACKAGE_VERSION}; diff --git a/test/common/helper.ts b/test/common/helper.ts deleted file mode 100644 index fee1d8336..000000000 --- a/test/common/helper.ts +++ /dev/null @@ -1,44 +0,0 @@ -/*! - * Copyright 2024 Google LLC. All Rights Reserved. - * - * 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 - * - * http://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. - */ - -import {EventEmitter} from 'events'; -import * as through from 'through2'; - -export class FakeTransaction extends EventEmitter { - _ended: boolean; - constructor() { - super(); - this._ended = false; - } - commit(gaxOptions, callback) { - this.end(); - callback(null, {}); - } - createReadStream() { - return through.obj(); - } - deleteRows() {} - end() { - if (!this._ended) { - this.emit('end'); - this._ended = true; - } - } - insert() {} - replace() {} - upsert() {} - update() {} -}