autotel-plugins 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,194 @@
1
+ declare const INSTRUMENTED_FLAG: "__autotelDrizzleInstrumented";
2
+ type QueryFunction = (...args: any[]) => any;
3
+ interface DrizzleClientLike {
4
+ query?: QueryFunction;
5
+ execute?: QueryFunction;
6
+ [INSTRUMENTED_FLAG]?: true;
7
+ [key: string]: any;
8
+ }
9
+ /**
10
+ * Configuration options for Drizzle instrumentation.
11
+ */
12
+ interface InstrumentDrizzleConfig {
13
+ /**
14
+ * Custom tracer name. Defaults to "autotel-plugins/drizzle".
15
+ */
16
+ tracerName?: string;
17
+ /**
18
+ * Database system identifier (e.g., "postgresql", "mysql", "sqlite").
19
+ * Defaults to "postgresql".
20
+ */
21
+ dbSystem?: string;
22
+ /**
23
+ * Database name to include in spans.
24
+ */
25
+ dbName?: string;
26
+ /**
27
+ * Whether to capture full SQL query text in spans.
28
+ * Defaults to true.
29
+ */
30
+ captureQueryText?: boolean;
31
+ /**
32
+ * Maximum length for captured query text. Queries longer than this
33
+ * will be truncated. Defaults to 1000 characters.
34
+ */
35
+ maxQueryTextLength?: number;
36
+ /**
37
+ * Remote hostname or IP address of the database server.
38
+ * Example: "db.example.com" or "192.168.1.100"
39
+ */
40
+ peerName?: string;
41
+ /**
42
+ * Remote port number of the database server.
43
+ * Example: 5432 for PostgreSQL, 3306 for MySQL
44
+ */
45
+ peerPort?: number;
46
+ }
47
+ /**
48
+ * Instruments a database connection pool/client with OpenTelemetry tracing.
49
+ *
50
+ * This function wraps the connection's `query` and `execute` methods to create spans for each database
51
+ * operation.
52
+ * The instrumentation is idempotent - calling it multiple times on the same connection will only
53
+ * instrument it once.
54
+ *
55
+ * @typeParam TClient - The type of the database connection pool or client
56
+ * @param client - The database connection pool or client to instrument
57
+ * @param config - Optional configuration for instrumentation behavior
58
+ * @returns The instrumented pool/client (same instance, modified in place)
59
+ *
60
+ * @example
61
+ * ```typescript
62
+ * // PostgreSQL with node-postgres
63
+ * import { drizzle } from 'drizzle-orm/node-postgres';
64
+ * import { Pool } from 'pg';
65
+ * import { instrumentDrizzle } from 'autotel-plugins/drizzle';
66
+ *
67
+ * const pool = new Pool({ connectionString: process.env.DATABASE_URL });
68
+ * const instrumentedPool = instrumentDrizzle(pool, {
69
+ * dbSystem: 'postgresql',
70
+ * dbName: 'myapp',
71
+ * peerName: 'db.example.com',
72
+ * peerPort: 5432,
73
+ * });
74
+ * const db = drizzle({ client: instrumentedPool });
75
+ * ```
76
+ *
77
+ * @example
78
+ * ```typescript
79
+ * // MySQL with mysql2
80
+ * import { drizzle } from 'drizzle-orm/mysql2';
81
+ * import mysql from 'mysql2/promise';
82
+ * import { instrumentDrizzle } from 'autotel-plugins/drizzle';
83
+ *
84
+ * const connection = await mysql.createConnection({
85
+ * host: 'localhost',
86
+ * user: 'root',
87
+ * database: 'mydb',
88
+ * });
89
+ * const instrumentedConnection = instrumentDrizzle(connection, { dbSystem: 'mysql' });
90
+ * const db = drizzle({ client: instrumentedConnection });
91
+ * ```
92
+ *
93
+ * @example
94
+ * ```typescript
95
+ * // SQLite with better-sqlite3
96
+ * import { drizzle } from 'drizzle-orm/better-sqlite3';
97
+ * import Database from 'better-sqlite3';
98
+ * import { instrumentDrizzle } from 'autotel-plugins/drizzle';
99
+ *
100
+ * const sqlite = new Database('sqlite.db');
101
+ * const instrumentedSqlite = instrumentDrizzle(sqlite, { dbSystem: 'sqlite' });
102
+ * const db = drizzle({ client: instrumentedSqlite });
103
+ * ```
104
+ *
105
+ * @example
106
+ * ```typescript
107
+ * // LibSQL/Turso
108
+ * import { drizzle } from 'drizzle-orm/libsql';
109
+ * import { createClient } from '@libsql/client';
110
+ * import { instrumentDrizzle } from 'autotel-plugins/drizzle';
111
+ *
112
+ * const client = createClient({
113
+ * url: process.env.DATABASE_URL!,
114
+ * authToken: process.env.DATABASE_AUTH_TOKEN,
115
+ * });
116
+ * const instrumentedClient = instrumentDrizzle(client, { dbSystem: 'sqlite' });
117
+ * const db = drizzle({ client: instrumentedClient });
118
+ * ```
119
+ */
120
+ declare function instrumentDrizzle<TClient extends DrizzleClientLike>(client: TClient, config?: InstrumentDrizzleConfig): TClient;
121
+ /**
122
+ * Interface for Drizzle database instances with minimal type requirements.
123
+ */
124
+ interface DrizzleDbLike {
125
+ $client?: DrizzleClientLike | any;
126
+ execute?: QueryFunction;
127
+ transaction?: QueryFunction;
128
+ _?: {
129
+ session?: {
130
+ execute?: QueryFunction;
131
+ [INSTRUMENTED_FLAG]?: true;
132
+ [key: string]: any;
133
+ };
134
+ [key: string]: any;
135
+ };
136
+ [INSTRUMENTED_FLAG]?: true;
137
+ [key: string]: any;
138
+ }
139
+ /**
140
+ * Instruments a Drizzle database instance with OpenTelemetry tracing.
141
+ *
142
+ * This function instruments the database at the session level, automatically tracing all database
143
+ * operations including query builders, direct SQL execution, and transactions.
144
+ *
145
+ * The instrumentation is idempotent - calling it multiple times on the same
146
+ * database will only instrument it once.
147
+ *
148
+ * @typeParam TDb - The type of the Drizzle database instance
149
+ * @param db - The Drizzle database instance to instrument
150
+ * @param config - Optional configuration for instrumentation behavior
151
+ * @returns The instrumented database instance (same instance, modified in place)
152
+ *
153
+ * @example
154
+ * ```typescript
155
+ * // PostgreSQL with postgres.js
156
+ * import { drizzle } from 'drizzle-orm/postgres-js';
157
+ * import postgres from 'postgres';
158
+ * import { instrumentDrizzleClient } from 'autotel-plugins/drizzle';
159
+ *
160
+ * // Using connection string
161
+ * const db = drizzle(process.env.DATABASE_URL!);
162
+ * instrumentDrizzleClient(db, { dbSystem: 'postgresql' });
163
+ *
164
+ * // Or with a client instance
165
+ * const queryClient = postgres(process.env.DATABASE_URL!);
166
+ * const db = drizzle({ client: queryClient });
167
+ * instrumentDrizzleClient(db, { dbSystem: 'postgresql' });
168
+ * ```
169
+ *
170
+ * @example
171
+ * ```typescript
172
+ * // PostgreSQL with node-postgres (pg)
173
+ * import { drizzle } from 'drizzle-orm/node-postgres';
174
+ * import { Pool } from 'pg';
175
+ * import { instrumentDrizzleClient } from 'autotel-plugins/drizzle';
176
+ *
177
+ * // Using connection string
178
+ * const db = drizzle(process.env.DATABASE_URL!);
179
+ * instrumentDrizzleClient(db, { dbSystem: 'postgresql' });
180
+ *
181
+ * // Or with a pool
182
+ * const pool = new Pool({ connectionString: process.env.DATABASE_URL });
183
+ * const db = drizzle({ client: pool });
184
+ * instrumentDrizzleClient(db, {
185
+ * dbSystem: 'postgresql',
186
+ * dbName: 'myapp',
187
+ * peerName: 'db.example.com',
188
+ * peerPort: 5432,
189
+ * });
190
+ * ```
191
+ */
192
+ declare function instrumentDrizzleClient<TDb extends DrizzleDbLike>(db: TDb, config?: InstrumentDrizzleConfig): TDb;
193
+
194
+ export { type InstrumentDrizzleConfig, instrumentDrizzle, instrumentDrizzleClient };
@@ -0,0 +1,194 @@
1
+ declare const INSTRUMENTED_FLAG: "__autotelDrizzleInstrumented";
2
+ type QueryFunction = (...args: any[]) => any;
3
+ interface DrizzleClientLike {
4
+ query?: QueryFunction;
5
+ execute?: QueryFunction;
6
+ [INSTRUMENTED_FLAG]?: true;
7
+ [key: string]: any;
8
+ }
9
+ /**
10
+ * Configuration options for Drizzle instrumentation.
11
+ */
12
+ interface InstrumentDrizzleConfig {
13
+ /**
14
+ * Custom tracer name. Defaults to "autotel-plugins/drizzle".
15
+ */
16
+ tracerName?: string;
17
+ /**
18
+ * Database system identifier (e.g., "postgresql", "mysql", "sqlite").
19
+ * Defaults to "postgresql".
20
+ */
21
+ dbSystem?: string;
22
+ /**
23
+ * Database name to include in spans.
24
+ */
25
+ dbName?: string;
26
+ /**
27
+ * Whether to capture full SQL query text in spans.
28
+ * Defaults to true.
29
+ */
30
+ captureQueryText?: boolean;
31
+ /**
32
+ * Maximum length for captured query text. Queries longer than this
33
+ * will be truncated. Defaults to 1000 characters.
34
+ */
35
+ maxQueryTextLength?: number;
36
+ /**
37
+ * Remote hostname or IP address of the database server.
38
+ * Example: "db.example.com" or "192.168.1.100"
39
+ */
40
+ peerName?: string;
41
+ /**
42
+ * Remote port number of the database server.
43
+ * Example: 5432 for PostgreSQL, 3306 for MySQL
44
+ */
45
+ peerPort?: number;
46
+ }
47
+ /**
48
+ * Instruments a database connection pool/client with OpenTelemetry tracing.
49
+ *
50
+ * This function wraps the connection's `query` and `execute` methods to create spans for each database
51
+ * operation.
52
+ * The instrumentation is idempotent - calling it multiple times on the same connection will only
53
+ * instrument it once.
54
+ *
55
+ * @typeParam TClient - The type of the database connection pool or client
56
+ * @param client - The database connection pool or client to instrument
57
+ * @param config - Optional configuration for instrumentation behavior
58
+ * @returns The instrumented pool/client (same instance, modified in place)
59
+ *
60
+ * @example
61
+ * ```typescript
62
+ * // PostgreSQL with node-postgres
63
+ * import { drizzle } from 'drizzle-orm/node-postgres';
64
+ * import { Pool } from 'pg';
65
+ * import { instrumentDrizzle } from 'autotel-plugins/drizzle';
66
+ *
67
+ * const pool = new Pool({ connectionString: process.env.DATABASE_URL });
68
+ * const instrumentedPool = instrumentDrizzle(pool, {
69
+ * dbSystem: 'postgresql',
70
+ * dbName: 'myapp',
71
+ * peerName: 'db.example.com',
72
+ * peerPort: 5432,
73
+ * });
74
+ * const db = drizzle({ client: instrumentedPool });
75
+ * ```
76
+ *
77
+ * @example
78
+ * ```typescript
79
+ * // MySQL with mysql2
80
+ * import { drizzle } from 'drizzle-orm/mysql2';
81
+ * import mysql from 'mysql2/promise';
82
+ * import { instrumentDrizzle } from 'autotel-plugins/drizzle';
83
+ *
84
+ * const connection = await mysql.createConnection({
85
+ * host: 'localhost',
86
+ * user: 'root',
87
+ * database: 'mydb',
88
+ * });
89
+ * const instrumentedConnection = instrumentDrizzle(connection, { dbSystem: 'mysql' });
90
+ * const db = drizzle({ client: instrumentedConnection });
91
+ * ```
92
+ *
93
+ * @example
94
+ * ```typescript
95
+ * // SQLite with better-sqlite3
96
+ * import { drizzle } from 'drizzle-orm/better-sqlite3';
97
+ * import Database from 'better-sqlite3';
98
+ * import { instrumentDrizzle } from 'autotel-plugins/drizzle';
99
+ *
100
+ * const sqlite = new Database('sqlite.db');
101
+ * const instrumentedSqlite = instrumentDrizzle(sqlite, { dbSystem: 'sqlite' });
102
+ * const db = drizzle({ client: instrumentedSqlite });
103
+ * ```
104
+ *
105
+ * @example
106
+ * ```typescript
107
+ * // LibSQL/Turso
108
+ * import { drizzle } from 'drizzle-orm/libsql';
109
+ * import { createClient } from '@libsql/client';
110
+ * import { instrumentDrizzle } from 'autotel-plugins/drizzle';
111
+ *
112
+ * const client = createClient({
113
+ * url: process.env.DATABASE_URL!,
114
+ * authToken: process.env.DATABASE_AUTH_TOKEN,
115
+ * });
116
+ * const instrumentedClient = instrumentDrizzle(client, { dbSystem: 'sqlite' });
117
+ * const db = drizzle({ client: instrumentedClient });
118
+ * ```
119
+ */
120
+ declare function instrumentDrizzle<TClient extends DrizzleClientLike>(client: TClient, config?: InstrumentDrizzleConfig): TClient;
121
+ /**
122
+ * Interface for Drizzle database instances with minimal type requirements.
123
+ */
124
+ interface DrizzleDbLike {
125
+ $client?: DrizzleClientLike | any;
126
+ execute?: QueryFunction;
127
+ transaction?: QueryFunction;
128
+ _?: {
129
+ session?: {
130
+ execute?: QueryFunction;
131
+ [INSTRUMENTED_FLAG]?: true;
132
+ [key: string]: any;
133
+ };
134
+ [key: string]: any;
135
+ };
136
+ [INSTRUMENTED_FLAG]?: true;
137
+ [key: string]: any;
138
+ }
139
+ /**
140
+ * Instruments a Drizzle database instance with OpenTelemetry tracing.
141
+ *
142
+ * This function instruments the database at the session level, automatically tracing all database
143
+ * operations including query builders, direct SQL execution, and transactions.
144
+ *
145
+ * The instrumentation is idempotent - calling it multiple times on the same
146
+ * database will only instrument it once.
147
+ *
148
+ * @typeParam TDb - The type of the Drizzle database instance
149
+ * @param db - The Drizzle database instance to instrument
150
+ * @param config - Optional configuration for instrumentation behavior
151
+ * @returns The instrumented database instance (same instance, modified in place)
152
+ *
153
+ * @example
154
+ * ```typescript
155
+ * // PostgreSQL with postgres.js
156
+ * import { drizzle } from 'drizzle-orm/postgres-js';
157
+ * import postgres from 'postgres';
158
+ * import { instrumentDrizzleClient } from 'autotel-plugins/drizzle';
159
+ *
160
+ * // Using connection string
161
+ * const db = drizzle(process.env.DATABASE_URL!);
162
+ * instrumentDrizzleClient(db, { dbSystem: 'postgresql' });
163
+ *
164
+ * // Or with a client instance
165
+ * const queryClient = postgres(process.env.DATABASE_URL!);
166
+ * const db = drizzle({ client: queryClient });
167
+ * instrumentDrizzleClient(db, { dbSystem: 'postgresql' });
168
+ * ```
169
+ *
170
+ * @example
171
+ * ```typescript
172
+ * // PostgreSQL with node-postgres (pg)
173
+ * import { drizzle } from 'drizzle-orm/node-postgres';
174
+ * import { Pool } from 'pg';
175
+ * import { instrumentDrizzleClient } from 'autotel-plugins/drizzle';
176
+ *
177
+ * // Using connection string
178
+ * const db = drizzle(process.env.DATABASE_URL!);
179
+ * instrumentDrizzleClient(db, { dbSystem: 'postgresql' });
180
+ *
181
+ * // Or with a pool
182
+ * const pool = new Pool({ connectionString: process.env.DATABASE_URL });
183
+ * const db = drizzle({ client: pool });
184
+ * instrumentDrizzleClient(db, {
185
+ * dbSystem: 'postgresql',
186
+ * dbName: 'myapp',
187
+ * peerName: 'db.example.com',
188
+ * peerPort: 5432,
189
+ * });
190
+ * ```
191
+ */
192
+ declare function instrumentDrizzleClient<TDb extends DrizzleDbLike>(db: TDb, config?: InstrumentDrizzleConfig): TDb;
193
+
194
+ export { type InstrumentDrizzleConfig, instrumentDrizzle, instrumentDrizzleClient };