forked from googleapis/nodejs-spanner
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
observability: add base for instrumentation + tests
This change adds the scaffolding for instrumentation along with tests for the tracing mechanisms that'll be used to trace the entire package. Built from googleapis#2087 Updates googleapis#2079
- Loading branch information
Showing
3 changed files
with
514 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,245 @@ | ||
// 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 assert = require('assert'); | ||
|
||
const { | ||
AlwaysOffSampler, | ||
AlwaysOnSampler, | ||
NodeTracerProvider, | ||
InMemorySpanExporter, | ||
} = require('@opentelemetry/sdk-trace-node'); | ||
const {SimpleSpanProcessor} = require('@opentelemetry/sdk-trace-base'); | ||
const { | ||
SPAN_PREFIX, | ||
disableContextAndManager, | ||
setGlobalContextManager, | ||
startTrace, | ||
setTracerProvider, | ||
} = require('../src/instrument'); | ||
const { | ||
SEMATTRS_DB_NAME, | ||
SEMATTRS_DB_SQL_TABLE, | ||
SEMATTRS_DB_STATEMENT, | ||
SEMATTRS_DB_SYSTEM, | ||
} = require('@opentelemetry/semantic-conventions'); | ||
|
||
const {ContextManager} = require('@opentelemetry/api'); | ||
const { | ||
AsyncHooksContextManager, | ||
} = require('@opentelemetry/context-async-hooks'); | ||
|
||
const projectId = process.env.SPANNER_TEST_PROJECTID || 'test-project'; | ||
|
||
// const {DiagConsoleLogger, DiagLogLevel, diag} = require('@opentelemetry/api'); | ||
// diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.ALL); | ||
|
||
describe('Test startTrace', () => { | ||
const exporter = new InMemorySpanExporter(); | ||
const sampler = new AlwaysOnSampler(); | ||
|
||
const provider = new NodeTracerProvider({ | ||
sampler: sampler, | ||
exporter: exporter, | ||
}); | ||
provider.addSpanProcessor(new SimpleSpanProcessor(exporter)); | ||
provider.register(); | ||
setTracerProvider(provider); | ||
|
||
const contextManager = new AsyncHooksContextManager(); | ||
setGlobalContextManager(contextManager); | ||
|
||
after(async () => { | ||
exporter.forceFlush(); | ||
await provider.shutdown(); | ||
disableContextAndManager(contextManager); | ||
}); | ||
|
||
it('startTrace with TracerProvider in global configuration', () => { | ||
startTrace('mySpan', {}, async span => { | ||
await new Promise((resolve, reject) => setTimeout(resolve, 400)); | ||
span.end(); | ||
|
||
const spans = exporter.getFinishedSpans(); | ||
assert.ok(spans.length, 1, 'exactly 1 span must have been exported'); | ||
const span0 = spans[0]; | ||
|
||
// The span MUST start with the prefix of this library. | ||
const gotName = span0.name; | ||
const wantName = SPAN_PREFIX + '/mySpan'; | ||
assert.equal( | ||
gotName, | ||
wantName, | ||
'name mismatch:\n\tGot: ' + gotName + '\n\tWant: ' + wantName | ||
); | ||
}); | ||
}); | ||
|
||
it('startTrace with TracerProvider in options, skips using global TracerProvider', () => { | ||
const exporter2 = new InMemorySpanExporter(); | ||
const provider2 = new NodeTracerProvider({ | ||
sampler: sampler, | ||
exporter: exporter2, | ||
}); | ||
provider2.addSpanProcessor(new SimpleSpanProcessor(exporter2)); | ||
provider2.register(); | ||
|
||
startTrace('aSpan', {tracerProvider: provider2}, async span => { | ||
await new Promise((resolve, reject) => setTimeout(resolve, 400)); | ||
span.end(); | ||
|
||
const gotSpansFromGlobal = exporter.getFinishedSpans(); | ||
assert.ok( | ||
gotSpansFromGlobal.length, | ||
0, | ||
'no spans should be registered to the global' | ||
); | ||
|
||
const gotSpansFromCurrent = exporter2.getFinishedSpans(); | ||
assert.ok( | ||
gotSpansFromCurrent.length, | ||
1, | ||
'a span should be retrieved from this exporter' | ||
); | ||
|
||
exporter2.forceFlush(); | ||
await provider2.shutdown(); | ||
}); | ||
}); | ||
|
||
it('startTrace with semantic attributes', () => { | ||
const opts = {tableName: 'table', dbName: 'db'}; | ||
startTrace('aSpan', opts, async span => { | ||
assert.equal( | ||
span.attributes[SEMATTRS_DB_SYSTEM], | ||
'spanner', | ||
'Missing DB_SYSTEM attribute' | ||
); | ||
|
||
assert.equal( | ||
span.attributes[SEMATTRS_DB_SQL_TABLE], | ||
'table', | ||
'Missing DB_SQL_TABLE attribute' | ||
); | ||
|
||
assert.equal( | ||
span.attributes[SEMATTRS_DB_NAME], | ||
'db', | ||
'Missing DB_NAME attribute' | ||
); | ||
}); | ||
}); | ||
|
||
it('startTrace with enableExtendedTracing=true, no sql value set', () => { | ||
const opts = {enableExtendedTracing: true}; | ||
startTrace('aSpan', opts, async span => { | ||
assert.equal( | ||
span.attributes[SEMATTRS_DB_STATEMENT], | ||
undefined, | ||
'Unexpected DB_STATEMENT attribute' | ||
); | ||
}); | ||
}); | ||
|
||
it('startTrace with enableExtendedTracing=true, sql string value set', () => { | ||
const opts = { | ||
enableExtendedTracing: true, | ||
sql: 'SELECT CURRENT_TIMESTAMP()', | ||
}; | ||
startTrace('aSpan', opts, async span => { | ||
assert.equal( | ||
span.attributes[SEMATTRS_DB_STATEMENT], | ||
'SELECT CURRENT_TIMESTAMP()', | ||
'Mismatched DB_STATEMENT attribute' | ||
); | ||
}); | ||
}); | ||
|
||
it('startTrace with enableExtendedTracing=false, sql string value set', () => { | ||
const opts = { | ||
enableExtendedTracing: false, | ||
sql: 'SELECt CURRENT_TIMESTAMP()', | ||
}; | ||
startTrace('aSpan', opts, async span => { | ||
assert.equal( | ||
span.attributes[SEMATTRS_DB_STATEMENT], | ||
undefined, | ||
'Mismatched DB_STATEMENT attribute' | ||
); | ||
}); | ||
}); | ||
|
||
it('startTrace with enableExtendedTracing=true, sql ExecuteSqlRequest value set', () => { | ||
const req = {sql: 'SELECT 1=1'}; | ||
const opts = { | ||
enableExtendedTracing: true, | ||
sql: req, | ||
}; | ||
startTrace('aSpan', opts, async span => { | ||
assert.equal( | ||
span.attributes[SEMATTRS_DB_STATEMENT], | ||
'SELECT 1=1', | ||
'Mismatched DB_STATEMENT attribute' | ||
); | ||
}); | ||
}); | ||
|
||
it('startTrace with enableExtendedTracing=false, sql ExecuteSqlRequest value set', () => { | ||
const req = {sql: 'SELECT 1=1'}; | ||
const opts = { | ||
enableExtendedTracing: true, | ||
sql: req, | ||
}; | ||
startTrace('aSpan', opts, async span => { | ||
assert.equal( | ||
span.attributes[SEMATTRS_DB_STATEMENT], | ||
undefined, | ||
'Mismatched DB_STATEMENT attribute' | ||
); | ||
}); | ||
}); | ||
|
||
it('alwaysOffSampler used', () => { | ||
const exporter2 = new InMemorySpanExporter(); | ||
const provider2 = new NodeTracerProvider({ | ||
sampler: new AlwaysOffSampler(), | ||
exporter: exporter2, | ||
}); | ||
provider2.addSpanProcessor(new SimpleSpanProcessor(exporter2)); | ||
provider2.register(); | ||
|
||
|
||
startTrace('aSpan', {tracerProvider: provider2}, async span => { | ||
await new Promise((resolve, reject) => setTimeout(resolve, 400)); | ||
span.end(); | ||
|
||
const gotSpansFromGlobal = exporter.getFinishedSpans(); | ||
assert.ok( | ||
gotSpansFromGlobal.length, | ||
0, | ||
'no spans should be exported | ||
); | ||
|
||
const gotSpansFromCurrent = exporter2.getFinishedSpans(); | ||
assert.ok( | ||
gotSpansFromCurrent.length, | ||
0, | ||
'no span should have been exported' | ||
); | ||
|
||
exporter2.forceFlush(); | ||
await provider2.shutdown(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.