Skip to content

Commit

Permalink
fix: avoid type imports of the instrumented package in the built asse…
Browse files Browse the repository at this point in the history
…ts (open-telemetry#1017)

* fix: remove cassandra types from exports

* fix: remove router types from exports

* fix: remove knex types from exports

* fix: remove mysql2 types from exports

* style: lint mysql2 source

* fix: keep `typeof Router` in non-public API
  • Loading branch information
rauno56 authored Jun 27, 2022
1 parent f9e81b0 commit e265723
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class CassandraDriverInstrumentation extends InstrumentationBase {
}

protected init() {
return new InstrumentationNodeModuleDefinition<typeof CassandraDriver>(
return new InstrumentationNodeModuleDefinition<any>(
'cassandra-driver',
supportedVersions,
driverModule => {
Expand Down Expand Up @@ -290,7 +290,7 @@ export class CassandraDriverInstrumentation extends InstrumentationBase {
};
}

public startSpan(
private startSpan(
{ op, query }: { op: string; query?: unknown },
client: CassandraDriver.Client
): Span {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const DEFAULT_CONFIG: types.KnexInstrumentationConfig = {
maxQueryLength: 1022,
};

export class KnexInstrumentation extends InstrumentationBase<typeof knex> {
export class KnexInstrumentation extends InstrumentationBase<any> {
constructor(config: types.KnexInstrumentationConfig = {}) {
super(
`@opentelemetry/instrumentation-${constants.MODULE_NAME}`,
Expand All @@ -44,7 +44,7 @@ export class KnexInstrumentation extends InstrumentationBase<typeof knex> {
}

init() {
const module = new InstrumentationNodeModuleDefinition<typeof knex>(
const module = new InstrumentationNodeModuleDefinition<any>(
constants.MODULE_NAME,
constants.SUPPORTED_VERSIONS
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ import { VERSION } from './version';

type formatType = typeof mysqlTypes.format;

export class MySQL2Instrumentation extends InstrumentationBase<
typeof mysqlTypes
> {
export class MySQL2Instrumentation extends InstrumentationBase<any> {
static readonly COMMON_ATTRIBUTES = {
[SemanticAttributes.DB_SYSTEM]: DbSystemValues.MYSQL,
};
Expand All @@ -50,7 +48,7 @@ export class MySQL2Instrumentation extends InstrumentationBase<

protected init() {
return [
new InstrumentationNodeModuleDefinition<typeof mysqlTypes>(
new InstrumentationNodeModuleDefinition<any>(
'mysql2',
['>= 1.4.2 < 3.0'],
(moduleExports: any, moduleVersion) => {
Expand Down
18 changes: 17 additions & 1 deletion plugins/node/opentelemetry-instrumentation-mysql2/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,23 @@

import { SpanAttributes } from '@opentelemetry/api';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import type { Query, QueryOptions } from 'mysql2';

/*
Following types declare an expectation on mysql2 types and define a subset we
use in the instrumentation of the types actually defined in mysql2 pacakge
We need to import them here so that the installing party of the instrumentation
doesn't have to absolutely install the mysql2 package as well - specially
important for auto-loaders and meta-pacakges.
*/
interface QueryOptions {
sql: string;
values?: any | any[] | { [param: string]: any };
}

interface Query {
sql: string;
}

interface Config {
host?: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,15 @@ import * as utils from './utils';
import AttributeNames from './enums/AttributeNames';
import LayerType from './enums/LayerType';

export default class RouterInstrumentation extends InstrumentationBase<
typeof Router
> {
export default class RouterInstrumentation extends InstrumentationBase<any> {
constructor() {
super(`@opentelemetry/instrumentation-${constants.MODULE_NAME}`, VERSION);
}

private _moduleVersion?: string;

init() {
const module = new InstrumentationNodeModuleDefinition<typeof Router>(
const module = new InstrumentationNodeModuleDefinition<any>(
constants.MODULE_NAME,
constants.SUPPORTED_VERSIONS,
(moduleExports, moduleVersion) => {
Expand Down Expand Up @@ -170,7 +168,7 @@ export default class RouterInstrumentation extends InstrumentationBase<
const type = layer.method
? LayerType.REQUEST_HANDLER
: LayerType.MIDDLEWARE;
const route = utils.getRoute(req);
const route = req.baseUrl + (req.route?.path ?? '') || '/';
const spanName =
type === LayerType.REQUEST_HANDLER
? `request handler - ${route}`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import * as constants from './constants';
import * as types from './types';
import * as Router from 'router';

// Detect whether a function is a router package internal plumming handler
export const isInternal = (fn: Function) => {
Expand All @@ -30,9 +29,6 @@ export const isInternal = (fn: Function) => {
return false;
};

export const getRoute = (req: Router.RoutedRequest) =>
req.baseUrl + (req.route?.path ?? '') || '/';

export const renameHttpSpan = (
span?: types.InstrumentationSpan,
method?: string,
Expand Down

0 comments on commit e265723

Please sign in to comment.