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

feat: add undici, socket.io and lru-memoizer instrumentations #934

Merged
merged 4 commits into from
Aug 14, 2024
Merged
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
73 changes: 73 additions & 0 deletions package-lock.json

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

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@
"@opentelemetry/instrumentation-kafkajs": "0.1.0",
"@opentelemetry/instrumentation-knex": "0.37.0",
"@opentelemetry/instrumentation-koa": "0.41.0",
"@opentelemetry/instrumentation-lru-memoizer": "0.39.0",
"@opentelemetry/instrumentation-memcached": "0.37.0",
"@opentelemetry/instrumentation-mongodb": "0.45.0",
"@opentelemetry/instrumentation-mongoose": "0.39.0",
Expand All @@ -141,7 +142,9 @@
"@opentelemetry/instrumentation-redis-4": "0.40.0",
"@opentelemetry/instrumentation-restify": "0.39.0",
"@opentelemetry/instrumentation-router": "0.38.0",
"@opentelemetry/instrumentation-socket.io": "0.41.0",
"@opentelemetry/instrumentation-tedious": "0.11.0",
"@opentelemetry/instrumentation-undici": "0.4.0",
"@opentelemetry/instrumentation-winston": "0.38.0",
"@opentelemetry/propagator-b3": "1.25.1",
"@opentelemetry/resources": "1.25.1",
Expand Down
8 changes: 7 additions & 1 deletion scripts/generate-metadata-yaml.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ const KNOWN_TARGET_LIBRARY_VERSIONS = new Map([
["@opentelemetry/instrumentation-grpc", ["1.x"]],
["@opentelemetry/instrumentation-aws-sdk", ["2.x", "3.x"]],
["@opentelemetry/instrumentation-redis", ["^2.6.0", "3.x"]],
["@opentelemetry/instrumentation-redis-4", ["4.x"]]
["@opentelemetry/instrumentation-redis-4", ["4.x"]],
["@opentelemetry/instrumentation-lru-memoizer", [">=1.3 <3"]],
["@opentelemetry/instrumentation-socket.io", [">=2 <5"]],
["@opentelemetry/instrumentation-undici", [">=5.12.0"]],
]);

const INSTRUMENTATIONS = [
Expand All @@ -38,6 +41,7 @@ const INSTRUMENTATIONS = [
{ name: "@opentelemetry/instrumentation-kafkajs", target: "kafkajs", },
{ name: "@opentelemetry/instrumentation-knex", target: "knex", },
{ name: "@opentelemetry/instrumentation-koa", target: "koa", },
{ name: "@opentelemetry/instrumentation-lru-memoizer", target: "lru-memoizer", },
{ name: "@opentelemetry/instrumentation-memcached", target: "memcached", },
{ name: "@opentelemetry/instrumentation-mongodb", target: "mongodb", },
{ name: "@opentelemetry/instrumentation-mongoose", target: "mongoose", },
Expand All @@ -51,7 +55,9 @@ const INSTRUMENTATIONS = [
{ name: "@opentelemetry/instrumentation-redis-4", target: "redis", },
{ name: "@opentelemetry/instrumentation-restify", target: "restify", },
{ name: "@opentelemetry/instrumentation-router", target: "router", },
{ name: "@opentelemetry/instrumentation-socket.io", target: "socket.io", },
{ name: "@opentelemetry/instrumentation-tedious", target: "tedious", },
{ name: "@opentelemetry/instrumentation-undici", target: "undici", },
{ name: "@opentelemetry/instrumentation-winston", target: "winston", },
{ name: "splunk-opentelemetry-instrumentation-elasticsearch", target: "@elastic/elasticsearch", support: "supported", },
{ name: "splunk-opentelemetry-instrumentation-sequelize", target: "sequelize", support: "supported", },
Expand Down
46 changes: 17 additions & 29 deletions src/instrumentations/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
import { Options, CaptureHttpUriParameters } from '../tracing/options';
import { IncomingMessage, ServerResponse } from 'http';
import {
HttpInstrumentationConfig,
HttpResponseCustomAttributeFunction,
HttpRequestCustomAttributeFunction,
HttpInstrumentation,
} from '@opentelemetry/instrumentation-http';
import { diag, isSpanContextValid, TraceFlags } from '@opentelemetry/api';
import { Span } from '@opentelemetry/api';
Expand All @@ -45,7 +45,7 @@

try {
// As long as Node <11 is supported, need to use the legacy API.
return Url.parse(request.url || '', true).query;

Check warning on line 48 in src/instrumentations/http.ts

View workflow job for this annotation

GitHub Actions / lint

'url.parse' was deprecated since v11.0.0. Use 'url.URL' constructor instead
} catch (err) {
diag.debug(`error parsing url '${request.url}`, err);
}
Expand Down Expand Up @@ -115,18 +115,26 @@
}

export function configureHttpInstrumentation(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
instrumentation: any,
instrumentation: HttpInstrumentation,
options: Options
) {
if (!options.serverTimingEnabled) {
return;
const config = instrumentation.getConfig();

if (shouldAddRequestHook(options)) {
const requestHook = createHttpRequestHook(options);
if (config.requestHook === undefined) {
config.requestHook = requestHook;
} else {
const original = config.requestHook;
config.requestHook = function (this: unknown, span, request) {
requestHook(span, request);
original.call(this, span, request);
};
}
}

if (
typeof instrumentation['setConfig'] !== 'function' ||
typeof instrumentation['getConfig'] !== 'function'
) {
if (!options.serverTimingEnabled) {
instrumentation.setConfig(config);
return;
}

Expand Down Expand Up @@ -156,12 +164,6 @@
);
};

let config = instrumentation.getConfig() as HttpInstrumentationConfig;

if (config === undefined) {
config = {};
}

if (config.responseHook === undefined) {
config.responseHook = responseHook;
} else {
Expand All @@ -172,22 +174,8 @@
};
}

if (shouldAddRequestHook(options)) {
const requestHook = createHttpRequestHook(options);
if (config.requestHook === undefined) {
config.requestHook = requestHook;
} else {
const original = config.requestHook;
config.requestHook = function (this: unknown, span, request) {
requestHook(span, request);
original.call(this, span, request);
};
}
}

instrumentation.setConfig(config);
}

function appendHeader(response: ServerResponse, header: string, value: string) {
const existing = response.getHeader(header);

Expand Down
15 changes: 15 additions & 0 deletions src/instrumentations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { IORedisInstrumentation } from '@opentelemetry/instrumentation-ioredis';
import { KafkaJsInstrumentation } from '@opentelemetry/instrumentation-kafkajs';
import { KnexInstrumentation } from '@opentelemetry/instrumentation-knex';
import { KoaInstrumentation } from '@opentelemetry/instrumentation-koa';
import { LruMemoizerInstrumentation } from '@opentelemetry/instrumentation-lru-memoizer';
import { MemcachedInstrumentation } from '@opentelemetry/instrumentation-memcached';
import { MongoDBInstrumentation } from '@opentelemetry/instrumentation-mongodb';
import { MongooseInstrumentation } from '@opentelemetry/instrumentation-mongoose';
Expand All @@ -47,11 +48,13 @@ import { RedisInstrumentation } from '@opentelemetry/instrumentation-redis';
import { RedisInstrumentation as Redis4Instrumentation } from '@opentelemetry/instrumentation-redis-4';
import { RestifyInstrumentation } from '@opentelemetry/instrumentation-restify';
import { RouterInstrumentation } from '@opentelemetry/instrumentation-router';
import { SocketIoInstrumentation } from '@opentelemetry/instrumentation-socket.io';
import { TediousInstrumentation } from '@opentelemetry/instrumentation-tedious';
import { WinstonInstrumentation } from '@opentelemetry/instrumentation-winston';
import { ElasticsearchInstrumentation } from './external/elasticsearch';
import { SequelizeInstrumentation } from './external/sequelize';
import { TypeormInstrumentation } from './external/typeorm';
import { UndiciInstrumentation } from '@opentelemetry/instrumentation-undici';
import type { Instrumentation } from '@opentelemetry/instrumentation';

type InstrumentationInfo = {
Expand Down Expand Up @@ -132,6 +135,10 @@ export const bundledInstrumentations: InstrumentationInfo[] = [
create: () => new KoaInstrumentation(),
shortName: 'koa',
},
{
create: () => new LruMemoizerInstrumentation(),
shortName: 'lru_memoizer',
},
{
create: () => new MemcachedInstrumentation(),
shortName: 'memcached',
Expand Down Expand Up @@ -184,6 +191,10 @@ export const bundledInstrumentations: InstrumentationInfo[] = [
create: () => new RouterInstrumentation(),
shortName: 'router',
},
{
create: () => new SocketIoInstrumentation(),
shortName: 'socketio',
},
{
create: () => new TediousInstrumentation(),
shortName: 'tedious',
Expand All @@ -204,6 +215,10 @@ export const bundledInstrumentations: InstrumentationInfo[] = [
create: () => new TypeormInstrumentation(),
shortName: 'typeorm',
},
{
create: () => new UndiciInstrumentation(),
shortName: 'undici',
},
];

function envKey(info: InstrumentationInfo) {
Expand Down
3 changes: 2 additions & 1 deletion src/tracing/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import {
parseEnvBooleanString,
} from '../utils';
import { isProfilingContextManagerSet } from '../profiling';
import { HttpInstrumentation } from '@opentelemetry/instrumentation-http';

/**
* We disallow calling `startTracing` twice because:
Expand Down Expand Up @@ -224,7 +225,7 @@ function configureInstrumentations(options: Options) {
configureGraphQlInstrumentation(instr, options);
break;
case '@opentelemetry/instrumentation-http':
configureHttpInstrumentation(instr, options);
configureHttpInstrumentation(instr as HttpInstrumentation, options);
break;
case '@opentelemetry/instrumentation-redis':
configureRedisInstrumentation(instr, options);
Expand Down
14 changes: 9 additions & 5 deletions test/instrumentations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('instrumentations', () => {

it('loads instrumentations if they are installed', () => {
const loadedInstrumentations = getInstrumentations();
assert.equal(loadedInstrumentations.length, 36);
assert.equal(loadedInstrumentations.length, 39);
});

it('does not load instrumentations if OTEL_INSTRUMENTATION_COMMON_DEFAULT_ENABLED is false', () => {
Expand All @@ -47,10 +47,14 @@ describe('instrumentations', () => {
const instrumentations = getInstrumentations();
assert.equal(instrumentations.length, 1);
const instrumentation: Instrumentation = instrumentations[0];
// Dots in the instrumentation name are removed in the short name, e.g. socket.io -> socketio
const instrumentationName = instrumentation.instrumentationName.replace(
'.',
''
);
assert(
instrumentation.instrumentationName.includes(
bundled.shortName.replace('_', '-')
)
instrumentationName.includes(bundled.shortName.replace('_', '-')),
instrumentation.instrumentationName
);
cleanEnvironment();
}
Expand All @@ -74,6 +78,6 @@ describe('instrumentations', () => {
),
undefined
);
assert.equal(loadedInstrumentations.length, 35);
assert.equal(loadedInstrumentations.length, 38);
});
});
Loading