-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(opentelemetry-node): add @opentelemetry/instrumentation-cassandr…
…a-driver (#553)
- Loading branch information
1 parent
afa684a
commit 880710a
Showing
11 changed files
with
256 additions
and
0 deletions.
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
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
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
61 changes: 61 additions & 0 deletions
61
packages/opentelemetry-node/test/fixtures/use-cassandra-driver.js
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,61 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you 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. | ||
*/ | ||
|
||
// Usage: node -r @elastic/opentelemetry-node use-cassandra-driver.js | ||
|
||
const cassandra = require('cassandra-driver'); | ||
const otel = require('@opentelemetry/api'); | ||
|
||
const host = process.env.CASSANDRA_HOST; | ||
const port = process.env.CASSANDRA_PORT || '9042'; | ||
const keyspace = 'keyspace1'; | ||
const table = 'table1'; | ||
|
||
async function main() { | ||
const client = new cassandra.Client({ | ||
contactPoints: [`${host}:${port}`], | ||
localDataCenter: 'datacenter1', | ||
}); | ||
|
||
await client.connect(); | ||
await client.execute( | ||
`CREATE KEYSPACE IF NOT EXISTS ${keyspace} WITH replication = { 'class': 'SimpleStrategy', 'replication_factor': 1 };` | ||
); | ||
await client.execute(`USE ${keyspace}`); | ||
await client.execute( | ||
`CREATE TABLE IF NOT EXISTS ${keyspace}.${table}(id uuid,text varchar,PRIMARY KEY(id));` | ||
); | ||
await client.batch([ | ||
{ | ||
query: `INSERT INTO ${keyspace}.${table} (id, text) VALUES (uuid(), ?)`, | ||
params: ['value1'], | ||
}, | ||
{ | ||
query: `INSERT INTO ${keyspace}.${table} (id, text) VALUES (uuid(), ?)`, | ||
params: ['value2'], | ||
}, | ||
]); | ||
await client.execute(`DROP TABLE IF EXISTS ${keyspace}.${table}`); | ||
await client.shutdown(); | ||
} | ||
const tracer = otel.trace.getTracer('test'); | ||
tracer.startActiveSpan('manual-span', async (span) => { | ||
await main(); | ||
span.end(); | ||
}); |
76 changes: 76 additions & 0 deletions
76
packages/opentelemetry-node/test/instr-cassandra-driver.test.js
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,76 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you 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 test = require('tape'); | ||
const {filterOutDnsNetSpans, runTestFixtures} = require('./testutils'); | ||
|
||
// TODO: check https://github.com/elastic/apm-agent-nodejs/blob/main/test/_is_cassandra_incompat.js | ||
const skip = process.env.CASSANDRA_HOST === undefined; | ||
if (skip) { | ||
console.log( | ||
'# SKIP cassandra-driver tests: CASSANDRA_HOST is not set (try with `CASSANDRA_HOST=localhost`)' | ||
); | ||
} | ||
|
||
/** @type {import('./testutils').TestFixture[]} */ | ||
const testFixtures = [ | ||
{ | ||
name: 'use-cassandra-driver.js', | ||
args: ['./fixtures/use-cassandra-driver.js'], | ||
cwd: __dirname, | ||
env: { | ||
NODE_OPTIONS: '--require=@elastic/opentelemetry-node', | ||
}, | ||
// verbose: true, | ||
checkTelemetry: (t, col) => { | ||
// Expected a trace like this: | ||
// ------ trace 15ca3e (8 spans) ------ | ||
// span 3d3aa5 "manual-span" (402.5ms, SPAN_KIND_INTERNAL) | ||
// +27ms `- span b8ab8f "tcp.connect" (0.5ms, SPAN_KIND_INTERNAL) | ||
// +17ms `- span 6c9179 "tcp.connect" (0.4ms, SPAN_KIND_INTERNAL) | ||
// +25ms `- span 97c330 "cassandra-driver.execute" (2.7ms, SPAN_KIND_CLIENT) | ||
// +2ms `- span 91e594 "cassandra-driver.execute" (1.6ms, SPAN_KIND_CLIENT) | ||
// +2ms `- span 9fbb84 "cassandra-driver.execute" (88.3ms, SPAN_KIND_CLIENT) | ||
// +88ms `- span ef48b9 "cassandra-driver.batch" (5.7ms, SPAN_KIND_CLIENT) | ||
// +6ms `- span fa5471 "cassandra-driver.execute" (232.5ms, SPAN_KIND_CLIENT) | ||
const spans = filterOutDnsNetSpans(col.sortedSpans); | ||
t.equal(spans.length, 6); | ||
spans.slice(1).forEach((s) => { | ||
t.equal(s.traceId, spans[0].traceId, 'traceId'); | ||
t.equal(s.parentSpanId, spans[0].spanId, 'parentSpanId'); | ||
t.equal(s.kind, 'SPAN_KIND_CLIENT', 'kind'); | ||
t.equal( | ||
s.scope.name, | ||
'@opentelemetry/instrumentation-cassandra-driver' | ||
); | ||
t.equal(s.attributes['db.system'], 'cassandra'); | ||
}); | ||
t.equal(spans[1].name, 'cassandra-driver.execute'); | ||
t.equal(spans[2].name, 'cassandra-driver.execute'); | ||
t.equal(spans[3].name, 'cassandra-driver.execute'); | ||
t.equal(spans[4].name, 'cassandra-driver.batch'); | ||
t.equal(spans[5].name, 'cassandra-driver.execute'); | ||
}, | ||
}, | ||
]; | ||
|
||
test('cassandra-driver instrumentation', {skip}, (suite) => { | ||
runTestFixtures(suite, testFixtures); | ||
suite.end(); | ||
}); |
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.