Skip to content

Commit

Permalink
feat: instr-pino (#225)
Browse files Browse the repository at this point in the history
Closes: #33
  • Loading branch information
trentm authored Jun 13, 2024
1 parent e3fdea8 commit 27c842d
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 30 deletions.
86 changes: 56 additions & 30 deletions package-lock.json

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

Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ This follows from the [OpenTelemetry JS supported runtimes](https://github.com/o
| `@opentelemetry/instrumentation-http` | `http` module for Node.js `>=14` | [README](https://github.com/open-telemetry/opentelemetry-js/tree/main/experimental/packages/opentelemetry-instrumentation-http#readme) |
| `@opentelemetry/instrumentation-ioredis` | `ioredis` version range `>=2 <6` | [README](https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/opentelemetry-instrumentation-ioredis#readme) |
| `@opentelemetry/instrumentation-mongodb` | `mongodb` version range `>=3.3 <7` | [README](https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/opentelemetry-instrumentation-mongodb#readme) |
| `@opentelemetry/instrumentation-pino` | `pino` version range `>=5.14.0 <10` | [README](https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/opentelemetry-instrumentation-pino#readme) |
| `@opentelemetry/instrumentation-pg` | `pg` version range `>=8 <9` | [README](https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/opentelemetry-instrumentation-pg#readme) |
| `@opentelemetry/instrumentation-redis-4` | `redis` version range `^4.0.0` | [README](https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/opentelemetry-instrumentation-redis-4#readme) |
| `@opentelemetry/instrumentation-tedious` | `tedious` version range `>=1.11.0 <=15` | [README](https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/plugins/node/instrumentation-tedious#readme) |
Expand Down
4 changes: 4 additions & 0 deletions packages/opentelemetry-node/lib/instrumentations.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
* "@opentelemetry/instrumentation-grpc": import('@opentelemetry/instrumentation-grpc').GrpcInstrumentation | InstrumentationFactory,
* "@opentelemetry/instrumentation-hapi": import('@opentelemetry/instrumentation-hapi').HapiInstrumentation | InstrumentationFactory,
* "@opentelemetry/instrumentation-mongodb": import('@opentelemetry/instrumentation-mongodb').MongoDBInstrumentation | InstrumentationFactory
* "@opentelemetry/instrumentation-pino": import('@opentelemetry/instrumentation-pino').PinoInstrumentation | InstrumentationFactory
* "@opentelemetry/instrumentation-pg": import('@opentelemetry/instrumentation-pg').PgInstrumentation | InstrumentationFactory
* "@opentelemetry/instrumentation-winston": import('@opentelemetry/instrumentation-winston').WinstonInstrumentationConfig | InstrumentationFactory,
* "@opentelemetry/instrumentation-tedious": import('@opentelemetry/instrumentation-tedious').TediousInstrumentation | InstrumentationFactory,
Expand Down Expand Up @@ -63,6 +64,7 @@ const {
const {
MongoDBInstrumentation,
} = require('@opentelemetry/instrumentation-mongodb');
const {PinoInstrumentation} = require('@opentelemetry/instrumentation-pino');
const {PgInstrumentation} = require('@opentelemetry/instrumentation-pg');
const {
WinstonInstrumentation,
Expand Down Expand Up @@ -102,6 +104,8 @@ const INSTRUMENTATIONS = {
new IORedisInstrumentation(cfg),
'@opentelemetry/instrumentation-mongodb': (cfg) =>
new MongoDBInstrumentation(cfg),
'@opentelemetry/instrumentation-pino': (cfg) =>
new PinoInstrumentation(cfg),
'@opentelemetry/instrumentation-pg': (cfg) => new PgInstrumentation(cfg),
'@opentelemetry/instrumentation-winston': (cfg) =>
new WinstonInstrumentation(cfg),
Expand Down
2 changes: 2 additions & 0 deletions packages/opentelemetry-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
"@opentelemetry/instrumentation-ioredis": "^0.41.0",
"@opentelemetry/instrumentation-mongodb": "^0.45.0",
"@opentelemetry/instrumentation-pg": "^0.42.0",
"@opentelemetry/instrumentation-pino": "^0.40.0",
"@opentelemetry/instrumentation-redis-4": "^0.40.0",
"@opentelemetry/instrumentation-tedious": "^0.11.0",
"@opentelemetry/instrumentation-undici": "^0.3.0",
Expand Down Expand Up @@ -116,6 +117,7 @@
"module-details-from-path": "^1.0.3",
"mongodb": "^6.7.0",
"pg": "^8.12.0",
"pino": "^9.2.0",
"redis": "^4.6.14",
"semver": "^7.6.2",
"tape": "^5.7.5",
Expand Down
33 changes: 33 additions & 0 deletions packages/opentelemetry-node/test/fixtures/use-pino.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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-pino.js

const pino = require('pino');
const otel = require('@opentelemetry/api');

const log = pino();

log.info({foo: 'bar'}, 'hi');

const tracer = otel.trace.getTracer('test');
tracer.startActiveSpan('manual-span', (span) => {
log.info('with span info');
span.end();
});
66 changes: 66 additions & 0 deletions packages/opentelemetry-node/test/instr-pino.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* 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 that 'pino' instrumentation generates the telemetry we expect.

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

/** @type {import('./testutils').TestFixture[]} */
const testFixtures = [
{
name: 'use-pino',
args: ['./fixtures/use-pino.js'],
cwd: __dirname,
env: {
NODE_OPTIONS: '--require=@elastic/opentelemetry-node',
OTEL_LOG_LEVEL: 'none',
},
// verbose: true,
checkResult: (t, err, stdout, _stderr) => {
t.error(err, `exited successfully: err=${err}`);
// Clumsy pass of stdout info to `checkTelemetry`.
t.recs = stdout.trim().split(/\n/g).map(JSON.parse);
},
checkTelemetry: (t, col) => {
const recs = t.recs;
const spans = col.sortedSpans;
t.equal(spans.length, 1);

// Check log records to stdout.
t.equal(recs.length, 2);
t.equal(recs[0].level, 30 /* pino INFO */);
t.equal(recs[0].msg, 'hi');
t.equal(recs[1].level, 30 /* pino INFO */);
t.equal(recs[1].msg, 'with span info');
t.equal(recs[1].trace_id, spans[0].traceId);
t.equal(recs[1].span_id, spans[0].spanId);

// TODO: instr-pino doesn't yet support log-sending, so we don't
// yet expect logs sent via OTLP.
// const logs = col.logs;
// ...
},
},
];

test('pino instrumentation', (suite) => {
runTestFixtures(suite, testFixtures);
suite.end();
});
1 change: 1 addition & 0 deletions packages/opentelemetry-node/types/instrumentations.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type InstrumentaionsMap = {
"@opentelemetry/instrumentation-grpc": import('@opentelemetry/instrumentation-grpc').GrpcInstrumentation | InstrumentationFactory;
"@opentelemetry/instrumentation-hapi": import('@opentelemetry/instrumentation-hapi').HapiInstrumentation | InstrumentationFactory;
"@opentelemetry/instrumentation-mongodb": import('@opentelemetry/instrumentation-mongodb').MongoDBInstrumentation | InstrumentationFactory;
"@opentelemetry/instrumentation-pino": import('@opentelemetry/instrumentation-pino').PinoInstrumentation | InstrumentationFactory;
"@opentelemetry/instrumentation-pg": import('@opentelemetry/instrumentation-pg').PgInstrumentation | InstrumentationFactory;
"@opentelemetry/instrumentation-winston": import('@opentelemetry/instrumentation-winston').WinstonInstrumentationConfig | InstrumentationFactory;
"@opentelemetry/instrumentation-tedious": import('@opentelemetry/instrumentation-tedious').TediousInstrumentation | InstrumentationFactory;
Expand Down

0 comments on commit 27c842d

Please sign in to comment.