Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add a test for native @elastic/elasticsearch instrumentation #264

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"version": "0.1.0",
"scripts": {},
"devDependencies": {
"@elastic/elasticsearch": "^8.14.0",
"@elastic/opentelemetry-node": "*",
"bunyan": "^1.8.15",
"express": "^4.19.2"
Expand Down
89 changes: 89 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/opentelemetry-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
"@aws-sdk/client-s3": "^3.606.0",
"@aws-sdk/client-sns": "^3.606.0",
"@aws-sdk/client-sqs": "^3.606.0",
"@elastic/elasticsearch": "^8.14.0",
"@elastic/mockotlpserver": "*",
"@grpc/grpc-js": "^1.10.10",
"@grpc/proto-loader": "^0.7.13",
Expand Down
16 changes: 16 additions & 0 deletions packages/opentelemetry-node/test/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,22 @@
name: test-services

services:
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:8.14.2
environment:
- "ES_JAVA_OPTS=-Xms512m -Xmx512m"
- "network.host=_site_"
- "transport.host=127.0.0.1"
- "http.host=0.0.0.0"
- "xpack.security.enabled=false"
ports:
- "9200:9200"
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:9200"]
interval: 1s
timeout: 10s
retries: 30

redis:
image: redis:7
ports:
Expand Down
47 changes: 47 additions & 0 deletions packages/opentelemetry-node/test/fixtures/use-elasticsearch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env node --unhandled-rejections=strict

/*
* 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:
// npm run test-services:start elasticsearch
// node -r @elastic/opentelemetry-node use-elasticsearch.js
// npm run test-services:stop

const {Client} = require('@elastic/elasticsearch');

const client = new Client({
node: process.env.ES_URL || 'http://localhost:9200',
auth: {
username: process.env.ES_USERNAME || undefined,
password: process.env.ES_PASSWORD || undefined,
},
maxRetries: 1,
});

async function run() {
try {
const res = await client.search({q: 'pants'});
console.log('search hits:', res.hits);
} catch (err) {
console.log('search error:', err.message);
}
}

run();
67 changes: 67 additions & 0 deletions packages/opentelemetry-node/test/instr-elasticsearch.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* 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.
*/

// Test the *native* OTel instrumentation for `@elastic/elasticsearch` 8.15.0
// and later.

const test = require('tape');
const {filterOutDnsNetSpans, runTestFixtures} = require('./testutils');

const skip = process.env.ES_URL === undefined;
if (skip) {
console.log(
'# SKIP elasticsearch tests: ES_URL is not set (try with `ES_URL=http://localhost:9200`)'
);
}

/** @type {import('./testutils').TestFixture[]} */
const testFixtures = [
{
name: 'use-elasticsearch.js (CommonJS)',
args: ['./fixtures/use-elasticsearch.js'],
cwd: __dirname,
env: {
NODE_OPTIONS: '--require=@elastic/opentelemetry-node',
},
versionRanges: {
// Min-supported node by @elastic/[email protected].
node: '>=18',
},
// verbose: true,
checkTelemetry: (t, col) => {
// Expected a trace like this:
// XXX
const spans = filterOutDnsNetSpans(col.sortedSpans);
console.log('XXX spans: ', spans);
// 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, '@elastic/transport');
// t.equal(s.attributes['db.system'], 'elasticsearch');
// });
},
},
];

test('elasticsearch instrumentation', {skip}, (suite) => {
runTestFixtures(suite, testFixtures);
suite.end();
});
3 changes: 2 additions & 1 deletion packages/opentelemetry-node/test/test-services.env
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
REDIS_HOST=localhost
PGHOST=localhost
MONGODB_HOST=localhost
MSSQL_HOST=localhost
MSSQL_HOST=localhost
ES_URL=http://localhost:9200 # Elasticsearch
Loading