Skip to content

Commit ae78d62

Browse files
authored
Merge pull request #12 from AgentOps-AI/client-dependency
Make `Client` instance available to instrumentation.
2 parents c903bdf + cb5e944 commit ae78d62

File tree

3 files changed

+22
-13
lines changed

3 files changed

+22
-13
lines changed

src/client.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const debug = require('debug')('agentops:client');
2525
* ```
2626
*/
2727
export class Client {
28-
private config: Config;
28+
public config: Config;
2929
public readonly registry: InstrumentationRegistry;
3030
private core: TracingCore | null = null;
3131
private api: API | null = null;
@@ -43,7 +43,7 @@ export class Client {
4343
apiKey: process.env.AGENTOPS_API_KEY,
4444
logLevel: (process.env.AGENTOPS_LOG_LEVEL as LogLevel) || 'error'
4545
};
46-
this.registry = new InstrumentationRegistry();
46+
this.registry = new InstrumentationRegistry(this);
4747
}
4848

4949
/**
@@ -87,7 +87,7 @@ export class Client {
8787
this.core = new TracingCore(
8888
this.config,
8989
await this.getAuthToken(),
90-
this.registry.getActiveInstrumentors(this.config.serviceName!),
90+
this.registry.getActiveInstrumentors(),
9191
resource
9292
);
9393
this.setupExitHandlers();

src/instrumentation/base.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import {
55
InstrumentationConfig
66
} from '@opentelemetry/instrumentation';
77
import { InstrumentorMetadata } from '../types';
8+
import { Client } from '../client';
9+
import { getPackageVersion } from '../attributes';
810

911
const debug = require('debug')('agentops:instrumentation:base');
1012

@@ -65,6 +67,14 @@ export abstract class InstrumentationBase extends _InstrumentationBase {
6567
static readonly metadata: InstrumentorMetadata;
6668
static readonly useRuntimeTargeting?: boolean = false;
6769
private isRuntimeSetup: boolean = false;
70+
private client: Client;
71+
72+
constructor(
73+
client: Client
74+
) {
75+
super(client.config.serviceName!, getPackageVersion(), {});
76+
this.client = client;
77+
}
6878

6979
/**
7080
* Initializes the instrumentation module definition using the static metadata.

src/instrumentation/registry.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
import { Client } from '../client';
12
import { InstrumentorMetadata } from '../types';
2-
import { getPackageVersion } from '../attributes';
33
import { AVAILABLE_INSTRUMENTORS } from './index';
44
import { InstrumentationBase } from './base';
55

@@ -13,18 +13,18 @@ const debug = require('debug')('agentops:instrumentation:registry');
1313
* libraries are present, and provides methods to create and manage active instances.
1414
*/
1515
export class InstrumentationRegistry {
16+
private client: Client;
1617
private instrumentors = new Map<string, typeof InstrumentationBase>();
1718
private enabledInstrumentors = new Map<string, InstrumentationBase>();
18-
private readonly packageVersion: string;
1919

2020
/**
2121
* Creates a new instrumentation registry.
2222
*
2323
* Automatically discovers and registers all available instrumentations
2424
* from the AVAILABLE_INSTRUMENTORS list.
2525
*/
26-
constructor() {
27-
this.packageVersion = getPackageVersion();
26+
constructor(client: Client) {
27+
this.client = client;
2828
}
2929

3030
/**
@@ -40,8 +40,7 @@ export class InstrumentationRegistry {
4040
if (instrumentorClass.useRuntimeTargeting) {
4141
const existingInstance = this.enabledInstrumentors.get(instrumentorClass.identifier);
4242
if (!existingInstance) {
43-
// TODO don't hardcode package name
44-
const instance = this.createInstance(instrumentorClass, 'agentops');
43+
const instance = this.createInstance(instrumentorClass);
4544
if (instance) {
4645
instance.setupRuntimeTargeting();
4746
}
@@ -88,14 +87,14 @@ export class InstrumentationRegistry {
8887
* @param packageName - Name of the service/package being instrumented
8988
* @returns The created instrumentation instance, or null if creation failed
9089
*/
91-
private createInstance(instrumentorClass: typeof InstrumentationBase, packageName: string): InstrumentationBase | undefined {
90+
private createInstance(instrumentorClass: typeof InstrumentationBase): InstrumentationBase | undefined {
9291
const existingInstance = this.enabledInstrumentors.get(instrumentorClass.identifier);
9392
if (existingInstance) {
9493
return existingInstance;
9594
}
9695

9796
try {
98-
const instance = new (instrumentorClass as any)(packageName, this.packageVersion, {});
97+
const instance = new (instrumentorClass as any)(this.client);
9998
this.enabledInstrumentors.set(instrumentorClass.identifier, instance);
10099
debug(`instantiated ${instrumentorClass.identifier}`);
101100
return instance;
@@ -114,15 +113,15 @@ export class InstrumentationRegistry {
114113
* @param serviceName - Name of the service to create instrumentations for
115114
* @returns Array of active instrumentation instances
116115
*/
117-
getActiveInstrumentors(serviceName: string): InstrumentationBase[] {
116+
getActiveInstrumentors(): InstrumentationBase[] {
118117
const available: (typeof InstrumentationBase)[] = this.getAvailable();
119118
const instrumentors: InstrumentationBase[] = [];
120119

121120
for (const instrumentorClass of available) {
122121
// Check if already enabled, otherwise create new instance
123122
let instrumentor = this.enabledInstrumentors.get(instrumentorClass.identifier);
124123
if (!instrumentor && instrumentorClass.available) {
125-
instrumentor = this.createInstance(instrumentorClass, serviceName);
124+
instrumentor = this.createInstance(instrumentorClass);
126125
}
127126

128127
if (instrumentor) {

0 commit comments

Comments
 (0)