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: allow toggling of instrumentations via env vars #769

Merged
merged 4 commits into from
Aug 1, 2023
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
255 changes: 208 additions & 47 deletions src/instrumentations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,60 +15,221 @@
*/

import { load } from './loader';
import { getEnvBoolean } from '../utils';
import type { EnvVarKey } from '../types';

const bundledInstrumentations: [string, string][] = [
['@opentelemetry/instrumentation-amqplib', 'AmqplibInstrumentation'],
['@opentelemetry/instrumentation-aws-sdk', 'AwsInstrumentation'],
['@opentelemetry/instrumentation-bunyan', 'BunyanInstrumentation'],
[
'@opentelemetry/instrumentation-cassandra-driver',
'CassandraDriverInstrumentation',
],
['@opentelemetry/instrumentation-connect', 'ConnectInstrumentation'],
['@opentelemetry/instrumentation-dataloader', 'DataloaderInstrumentation'],
['@opentelemetry/instrumentation-dns', 'DnsInstrumentation'],
['@opentelemetry/instrumentation-express', 'ExpressInstrumentation'],
['@opentelemetry/instrumentation-fastify', 'FastifyInstrumentation'],
['@opentelemetry/instrumentation-generic-pool', 'GenericPoolInstrumentation'],
['@opentelemetry/instrumentation-graphql', 'GraphQLInstrumentation'],
['@opentelemetry/instrumentation-grpc', 'GrpcInstrumentation'],
['@opentelemetry/instrumentation-hapi', 'HapiInstrumentation'],
['@opentelemetry/instrumentation-http', 'HttpInstrumentation'],
['@opentelemetry/instrumentation-ioredis', 'IORedisInstrumentation'],
['@opentelemetry/instrumentation-knex', 'KnexInstrumentation'],
['@opentelemetry/instrumentation-koa', 'KoaInstrumentation'],
['@opentelemetry/instrumentation-memcached', 'MemcachedInstrumentation'],
['@opentelemetry/instrumentation-mongodb', 'MongoDBInstrumentation'],
['@opentelemetry/instrumentation-mongoose', 'MongooseInstrumentation'],
['@opentelemetry/instrumentation-mysql', 'MySQLInstrumentation'],
['@opentelemetry/instrumentation-mysql2', 'MySQL2Instrumentation'],
['@opentelemetry/instrumentation-nestjs-core', 'NestInstrumentation'],
['@opentelemetry/instrumentation-net', 'NetInstrumentation'],
['@opentelemetry/instrumentation-pg', 'PgInstrumentation'],
['@opentelemetry/instrumentation-pino', 'PinoInstrumentation'],
['@opentelemetry/instrumentation-redis', 'RedisInstrumentation'],
['@opentelemetry/instrumentation-redis-4', 'RedisInstrumentation'],
['@opentelemetry/instrumentation-restify', 'RestifyInstrumentation'],
['@opentelemetry/instrumentation-router', 'RouterInstrumentation'],
['@opentelemetry/instrumentation-tedious', 'TediousInstrumentation'],
['@opentelemetry/instrumentation-winston', 'WinstonInstrumentation'],
['./external/elasticsearch', 'ElasticsearchInstrumentation'],
['./external/kafkajs', 'KafkaJsInstrumentation'],
['./external/sequelize', 'SequelizeInstrumentation'],
['./external/typeorm', 'TypeormInstrumentation'],
type InstrumentationInfo = {
module: string;
name: string;
shortName: string;
};

export const bundledInstrumentations: InstrumentationInfo[] = [
{
module: '@opentelemetry/instrumentation-amqplib',
name: 'AmqplibInstrumentation',
shortName: 'amqplib',
},
{
module: '@opentelemetry/instrumentation-aws-sdk',
name: 'AwsInstrumentation',
shortName: 'aws_sdk',
},
{
module: '@opentelemetry/instrumentation-bunyan',
name: 'BunyanInstrumentation',
shortName: 'bunyan',
},
{
module: '@opentelemetry/instrumentation-cassandra-driver',
name: 'CassandraDriverInstrumentation',
shortName: 'cassandra_driver',
},
{
module: '@opentelemetry/instrumentation-connect',
name: 'ConnectInstrumentation',
shortName: 'connect',
},
{
module: '@opentelemetry/instrumentation-dataloader',
name: 'DataloaderInstrumentation',
shortName: 'dataloader',
},
{
module: '@opentelemetry/instrumentation-dns',
name: 'DnsInstrumentation',
shortName: 'dns',
},
{
module: '@opentelemetry/instrumentation-express',
name: 'ExpressInstrumentation',
shortName: 'express',
},
{
module: '@opentelemetry/instrumentation-fastify',
name: 'FastifyInstrumentation',
shortName: 'fastify',
},
{
module: '@opentelemetry/instrumentation-generic-pool',
name: 'GenericPoolInstrumentation',
shortName: 'generic_pool',
},
{
module: '@opentelemetry/instrumentation-graphql',
name: 'GraphQLInstrumentation',
shortName: 'graphql',
},
{
module: '@opentelemetry/instrumentation-grpc',
name: 'GrpcInstrumentation',
shortName: 'grpc',
},
{
module: '@opentelemetry/instrumentation-hapi',
name: 'HapiInstrumentation',
shortName: 'hapi',
},
{
module: '@opentelemetry/instrumentation-http',
name: 'HttpInstrumentation',
shortName: 'http',
},
{
module: '@opentelemetry/instrumentation-ioredis',
name: 'IORedisInstrumentation',
shortName: 'ioredis',
},
{
module: '@opentelemetry/instrumentation-knex',
name: 'KnexInstrumentation',
shortName: 'knex',
},
{
module: '@opentelemetry/instrumentation-koa',
name: 'KoaInstrumentation',
shortName: 'koa',
},
{
module: '@opentelemetry/instrumentation-memcached',
name: 'MemcachedInstrumentation',
shortName: 'memcached',
},
{
module: '@opentelemetry/instrumentation-mongodb',
name: 'MongoDBInstrumentation',
shortName: 'mongodb',
},
{
module: '@opentelemetry/instrumentation-mongoose',
name: 'MongooseInstrumentation',
shortName: 'mongoose',
},
{
module: '@opentelemetry/instrumentation-mysql',
name: 'MySQLInstrumentation',
shortName: 'mysql',
},
{
module: '@opentelemetry/instrumentation-mysql2',
name: 'MySQL2Instrumentation',
shortName: 'mysql2',
},
{
module: '@opentelemetry/instrumentation-nestjs-core',
name: 'NestInstrumentation',
shortName: 'nestjs_core',
},
{
module: '@opentelemetry/instrumentation-net',
name: 'NetInstrumentation',
shortName: 'net',
},
{
module: '@opentelemetry/instrumentation-pg',
name: 'PgInstrumentation',
shortName: 'pg',
},
{
module: '@opentelemetry/instrumentation-pino',
name: 'PinoInstrumentation',
shortName: 'pino',
},
{
module: '@opentelemetry/instrumentation-redis',
name: 'RedisInstrumentation',
shortName: 'redis',
},
{
module: '@opentelemetry/instrumentation-redis-4',
name: 'RedisInstrumentation',
shortName: 'redis_4',
},
{
module: '@opentelemetry/instrumentation-restify',
name: 'RestifyInstrumentation',
shortName: 'restify',
},
{
module: '@opentelemetry/instrumentation-router',
name: 'RouterInstrumentation',
shortName: 'router',
},
{
module: '@opentelemetry/instrumentation-tedious',
name: 'TediousInstrumentation',
shortName: 'tedious',
},
{
module: '@opentelemetry/instrumentation-winston',
name: 'WinstonInstrumentation',
shortName: 'winston',
},
{
module: './external/elasticsearch',
name: 'ElasticsearchInstrumentation',
shortName: 'elasticsearch',
},
{
module: './external/kafkajs',
name: 'KafkaJsInstrumentation',
shortName: 'kafkajs',
},
{
module: './external/sequelize',
name: 'SequelizeInstrumentation',
shortName: 'sequelize',
},
{
module: './external/typeorm',
name: 'TypeormInstrumentation',
shortName: 'typeorm',
},
];

function envKey(info: InstrumentationInfo) {
return `OTEL_INSTRUMENTATION_${info.shortName.toUpperCase()}_ENABLED` as EnvVarKey;
t2t2 marked this conversation as resolved.
Show resolved Hide resolved
}

function getInstrumentationsToLoad() {
const enabledByDefault = getEnvBoolean(
'OTEL_INSTRUMENTATION_COMMON_DEFAULT_ENABLED',
true
);
return bundledInstrumentations.filter((info) =>
getEnvBoolean(envKey(info), enabledByDefault)
);
}

export function getInstrumentations() {
const result = [];
const loaded = [];

// Defensively load all supported instrumentations
for (const i in bundledInstrumentations) {
const [module, name] = bundledInstrumentations[i];
const Instrumentation = load(module, name);
for (const desc of getInstrumentationsToLoad()) {
const Instrumentation = load(desc.module, desc.name);
if (typeof Instrumentation === 'function') {
result.push(new (Instrumentation as typeof Instrumentation)());
loaded.push(new (Instrumentation as typeof Instrumentation)());
}
}

return result;
return loaded;
}
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export type EnvVarKey =
| 'OTEL_EXPORTER_OTLP_PROTOCOL'
| 'OTEL_EXPORTER_OTLP_TRACES_ENDPOINT'
| 'OTEL_EXPORTER_OTLP_TRACES_PROTOCOL'
| 'OTEL_INSTRUMENTATION_COMMON_DEFAULT_ENABLED'
| 'OTEL_LOG_LEVEL'
| 'OTEL_METRIC_EXPORT_INTERVAL'
| 'OTEL_METRICS_EXPORTER'
Expand Down
60 changes: 0 additions & 60 deletions test/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,10 @@ const assertFunction = (api, memberName) => {
);
};

/**
* The intention here is to test consistent behavior and shape across our APIs
* Currently there's none of it, which is fine because only tracing is stable
* and that's why most of those tests are currently skipped. For now this
* communicates the intention and progress of merging the API surface.
*/

describe('API', () => {
describe('global', () => {
const api = splunk;

it.skip('should expose start and stop', () => {
assertFunction(api, 'start');
assertFunction(api, 'stop');
});

it('should export signal-specific start', () => {
assertFunction(api, 'startTracing');
assertFunction(api, 'startProfiling');
Expand All @@ -68,12 +56,6 @@ describe('API', () => {
// would-be-loop-iteration. will perhaps not need these local vars eventually.
const api = tracing;

it.skip('should expose start and stop', () => {
// We'd eventually want to have consistency in our APIs internally
assertFunction(api, 'start');
assertFunction(api, 'stop');
});

it('should export signal-specific start', () => {
assertFunction(api, 'startTracing');
});
Expand All @@ -97,58 +79,16 @@ describe('API', () => {
describe('metrics', () => {
const api = metrics;

it.skip('should expose start and stop', () => {
// We'd eventually want to have consistency in our APIs internally
assertFunction(api, 'start');
assertFunction(api, 'stop');
});

it('should export signal-specific start', () => {
assertFunction(api, 'startMetrics');
});

it.skip('should export signal-specific stop', () => {
assertFunction(api, 'stopMetrics');
});

it.skip('should throw if start is called multiple times', () => {
api.startMetrics();
assert.throws(() => api.startMetrics());
api.stopMetrics();
});

it.skip('should do nothing if stop is called without start (even multiple times)', () => {
api.stopMetrics();
api.stopMetrics();
});
});

describe('profiling', () => {
const api = profiling;

it.skip('should expose start and stop', () => {
// We'd eventually want to have consistency in our APIs internally
assertFunction(api, 'start');
assertFunction(api, 'stop');
});

it('should export signal-specific start', () => {
assertFunction(api, 'startProfiling');
});

it.skip('should export signal-specific stop', () => {
assertFunction(api, 'stopProfiling');
});

it.skip('should throw if start is called multiple times', () => {
api.startProfiling();
assert.throws(() => api.startProfiling());
api.stopProfiling();
});

it.skip('should do nothing if stop is called without start (even multiple times)', () => {
api.stopProfiling();
api.stopProfiling();
});
});
});
Loading
Loading