Skip to content
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
6 changes: 3 additions & 3 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const debug = require('debug')('agentops:client');
* ```
*/
export class Client {
private config: Config;
public config: Config;
public readonly registry: InstrumentationRegistry;
private core: TracingCore | null = null;
private api: API | null = null;
Expand All @@ -43,7 +43,7 @@ export class Client {
apiKey: process.env.AGENTOPS_API_KEY,
logLevel: (process.env.AGENTOPS_LOG_LEVEL as LogLevel) || 'error'
};
this.registry = new InstrumentationRegistry();
this.registry = new InstrumentationRegistry(this);
}

/**
Expand Down Expand Up @@ -87,7 +87,7 @@ export class Client {
this.core = new TracingCore(
this.config,
await this.getAuthToken(),
this.registry.getActiveInstrumentors(this.config.serviceName!),
this.registry.getActiveInstrumentors(),
resource
);
this.setupExitHandlers();
Expand Down
10 changes: 10 additions & 0 deletions src/instrumentation/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
InstrumentationConfig
} from '@opentelemetry/instrumentation';
import { InstrumentorMetadata } from '../types';
import { Client } from '../client';
import { getPackageVersion } from '../attributes';

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

Expand Down Expand Up @@ -65,6 +67,14 @@ export abstract class InstrumentationBase extends _InstrumentationBase {
static readonly metadata: InstrumentorMetadata;
static readonly useRuntimeTargeting?: boolean = false;
private isRuntimeSetup: boolean = false;
private client: Client;

constructor(
client: Client
) {
super(client.config.serviceName!, getPackageVersion(), {});
this.client = client;
}

/**
* Initializes the instrumentation module definition using the static metadata.
Expand Down
19 changes: 9 additions & 10 deletions src/instrumentation/registry.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Client } from '../client';
import { InstrumentorMetadata } from '../types';
import { getPackageVersion } from '../attributes';
import { AVAILABLE_INSTRUMENTORS } from './index';
import { InstrumentationBase } from './base';

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

/**
* Creates a new instrumentation registry.
*
* Automatically discovers and registers all available instrumentations
* from the AVAILABLE_INSTRUMENTORS list.
*/
constructor() {
this.packageVersion = getPackageVersion();
constructor(client: Client) {
this.client = client;
}

/**
Expand All @@ -40,8 +40,7 @@ export class InstrumentationRegistry {
if (instrumentorClass.useRuntimeTargeting) {
const existingInstance = this.enabledInstrumentors.get(instrumentorClass.identifier);
if (!existingInstance) {
// TODO don't hardcode package name
const instance = this.createInstance(instrumentorClass, 'agentops');
const instance = this.createInstance(instrumentorClass);
if (instance) {
instance.setupRuntimeTargeting();
}
Expand Down Expand Up @@ -88,14 +87,14 @@ export class InstrumentationRegistry {
* @param packageName - Name of the service/package being instrumented
* @returns The created instrumentation instance, or null if creation failed
*/
private createInstance(instrumentorClass: typeof InstrumentationBase, packageName: string): InstrumentationBase | undefined {
private createInstance(instrumentorClass: typeof InstrumentationBase): InstrumentationBase | undefined {
const existingInstance = this.enabledInstrumentors.get(instrumentorClass.identifier);
if (existingInstance) {
return existingInstance;
}

try {
const instance = new (instrumentorClass as any)(packageName, this.packageVersion, {});
const instance = new (instrumentorClass as any)(this.client);
this.enabledInstrumentors.set(instrumentorClass.identifier, instance);
debug(`instantiated ${instrumentorClass.identifier}`);
return instance;
Expand All @@ -114,15 +113,15 @@ export class InstrumentationRegistry {
* @param serviceName - Name of the service to create instrumentations for
* @returns Array of active instrumentation instances
*/
getActiveInstrumentors(serviceName: string): InstrumentationBase[] {
getActiveInstrumentors(): InstrumentationBase[] {
const available: (typeof InstrumentationBase)[] = this.getAvailable();
const instrumentors: InstrumentationBase[] = [];

for (const instrumentorClass of available) {
// Check if already enabled, otherwise create new instance
let instrumentor = this.enabledInstrumentors.get(instrumentorClass.identifier);
if (!instrumentor && instrumentorClass.available) {
instrumentor = this.createInstance(instrumentorClass, serviceName);
instrumentor = this.createInstance(instrumentorClass);
}

if (instrumentor) {
Expand Down