Skip to content

Commit

Permalink
fix: separation of tracing to a different package
Browse files Browse the repository at this point in the history
  • Loading branch information
nirga committed Oct 20, 2023
1 parent 76a83ca commit 2ed288e
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 32 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<img width="300" src="https://raw.githubusercontent.com/traceloop/openllmetry/main/img/logo.png">
</a>
</p>
<h1 align="center">Open LLMetry</h1>
<h1 align="center">Open LLMetry JS</h1>
<p align="center">
<p align="center">Open-source observability for your LLM application</p>
</p>
Expand Down Expand Up @@ -53,14 +53,16 @@ Install the SDK:
npm install --save @traceloop/node-server-sdk
```

Then, to start instrumenting your code, just add this line to your code:
Then, to start instrumenting your code, just add these 2 lines to your code:

```js
import * as traceloop from "@traceloop/node-server-sdk";

traceloop.initialize({ app_name: "your_app_name" });
```

Make sure to `import` the SDK before importing any LLM module.

That's it. You're now tracing your code with OpenLLMetry-JS!
If you're running this locally, you may want to disable batch sending, so you can see the traces immediately:

Expand Down
2 changes: 1 addition & 1 deletion packages/sample-app/src/sample_openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import OpenAI from "openai";

traceloop.initialize({
appName: "sample_openai",
baseUrl: process.env.TRACELOOP_BASE_URL!,
baseUrl: process.env.TRACELOOP_BASE_URL,
apiKey: process.env.TRACELOOP_API_KEY!,
disableBatch: true,
});
Expand Down
29 changes: 2 additions & 27 deletions packages/traceloop-sdk/src/lib/configuration/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
import { NodeSDK } from "@opentelemetry/sdk-node";
import {
SimpleSpanProcessor,
BatchSpanProcessor,
} from "@opentelemetry/sdk-trace-node";
import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-proto";
import { Resource } from "@opentelemetry/resources";
import { SemanticResourceAttributes } from "@opentelemetry/semantic-conventions";
import { INSTRUMENTATIONS } from "../node-server-sdk";
import { InitializeOptions } from "../interfaces";
import { validateConfiguration } from "./validation";
import { startTracing } from "../tracing";

export let _configuration: InitializeOptions;

Expand All @@ -31,22 +23,5 @@ export const initialize = async (options: InitializeOptions) => {
}
_configuration = Object.freeze(options);

// const traceExporter = new ConsoleSpanExporter();
const traceExporter = new OTLPTraceExporter({
url: `${_configuration.baseUrl}/v1/traces`,
headers: { Authorization: `Bearer ${_configuration.apiKey}` },
});

const sdk = new NodeSDK({
resource: new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: _configuration.appName,
}),
spanProcessor: _configuration.disableBatch
? new SimpleSpanProcessor(traceExporter)
: new BatchSpanProcessor(traceExporter),
traceExporter,
instrumentations: INSTRUMENTATIONS,
});

sdk.start();
startTracing(_configuration);
};
4 changes: 2 additions & 2 deletions packages/traceloop-sdk/src/lib/node-server-sdk.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { OpenAIInstrumentation } from "@traceloop/instrumentation-openai";
import { initInstrumentations } from "./tracing";

export * from "./errors";
export { InitializeOptions } from "./interfaces";
export { initialize } from "./configuration";

export const INSTRUMENTATIONS = [new OpenAIInstrumentation()];
initInstrumentations();
44 changes: 44 additions & 0 deletions packages/traceloop-sdk/src/lib/tracing/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { NodeSDK } from "@opentelemetry/sdk-node";
import {
SimpleSpanProcessor,
BatchSpanProcessor,
} from "@opentelemetry/sdk-trace-node";
import { OTLPTraceExporter } from "@opentelemetry/exporter-trace-otlp-proto";
import { Resource } from "@opentelemetry/resources";
import { SemanticResourceAttributes } from "@opentelemetry/semantic-conventions";
import { InitializeOptions } from "../interfaces";
import { OpenAIInstrumentation } from "@traceloop/instrumentation-openai";

let _sdk: NodeSDK;
let instrumentations: any[] = [];

export const initInstrumentations = () => {
instrumentations.push(new OpenAIInstrumentation());
};

/**
* Initializes the Traceloop SDK.
* Must be called once before any other SDK methods.
*
* @param options - The options to initialize the SDK. See the {@link InitializeOptions} for details.
* @throws {InitializationError} if the configuration is invalid or if failed to fetch feature data.
*/
export const startTracing = (options: InitializeOptions) => {
const traceExporter = new OTLPTraceExporter({
url: `${options.baseUrl}/v1/traces`,
headers: { Authorization: `Bearer ${options.apiKey}` },
});

_sdk = new NodeSDK({
resource: new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: options.appName,
}),
spanProcessor: options.disableBatch
? new SimpleSpanProcessor(traceExporter)
: new BatchSpanProcessor(traceExporter),
traceExporter,
instrumentations,
});

_sdk.start();
};

0 comments on commit 2ed288e

Please sign in to comment.