Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(sdk): nextjs build errors #126

Merged
merged 1 commit into from
Mar 5, 2024
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
4 changes: 0 additions & 4 deletions packages/traceloop-sdk/src/lib/node-server-sdk.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { initInstrumentations } from "./tracing";

export * from "./errors";
export { InitializeOptions } from "./interfaces";
export { initialize } from "./configuration";
Expand All @@ -8,5 +6,3 @@ export * from "./tracing/decorators";
export * from "./tracing/association";
export * from "./tracing/score";
export * from "./prompts";

initInstrumentations();
185 changes: 109 additions & 76 deletions packages/traceloop-sdk/src/lib/tracing/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,61 +24,136 @@ let _spanProcessor: SimpleSpanProcessor | BatchSpanProcessor;
let openAIInstrumentation: AIInstrumentation | undefined;
let azureOpenAIInstrumentation: AIInstrumentation | undefined;
let llamaIndexInstrumentation: AIInstrumentation | undefined;
let pineconeInstrumentation: AIInstrumentation | undefined;
let vertexaiInstrumentation: AIInstrumentation | undefined;
let aiplatformInstrumentation: AIInstrumentation | undefined;
let bedrockInstrumentation: AIInstrumentation | undefined;
let cohereInstrumentation: AIInstrumentation | undefined;

const instrumentations: Instrumentation[] = [];

const hasModule = (module: string) => {
try {
require.resolve(`${module}`);
return true;
} catch (e) {
return false;
}
};

export const initInstrumentations = () => {
if (hasModule("openai")) {
try {
const {
OpenAIInstrumentation,
} = require("@traceloop/instrumentation-openai");
const instrumentation = new OpenAIInstrumentation();
instrumentations.push(instrumentation as Instrumentation);
openAIInstrumentation = instrumentation;
} catch (e) {
/* empty */
}

if (hasModule("@azure/openai")) {
try {
const {
AzureOpenAIInstrumentation,
} = require("@traceloop/instrumentation-azure");
const instrumentation = new AzureOpenAIInstrumentation();
instrumentations.push(instrumentation as Instrumentation);
azureOpenAIInstrumentation = instrumentation;
} catch (e) {
/* empty */
}
try {
const {
LlamaIndexInstrumentation,
} = require("@traceloop/instrumentation-llamaindex");
const instrumentation = new LlamaIndexInstrumentation();
instrumentations.push(instrumentation as Instrumentation);
llamaIndexInstrumentation = instrumentation;
} catch (e) {
/* empty */
}
try {
const {
PineconeInstrumentation,
} = require("@traceloop/instrumentation-pinecone");
const instrumentation = new PineconeInstrumentation();
instrumentations.push(instrumentation as Instrumentation);
} catch (e) {
/* empty */
}
const {
VertexAIInstrumentation,
} = require("@traceloop/instrumentation-vertexai");
const instrumentation = new VertexAIInstrumentation();
instrumentations.push(instrumentation);
vertexaiInstrumentation = instrumentation;

if (hasModule("llamaindex")) {
try {
const {
AIPlatformInstrumentation,
} = require("@traceloop/instrumentation-vertexai");
const instrumentation = new AIPlatformInstrumentation();
instrumentations.push(instrumentation as Instrumentation);
aiplatformInstrumentation = instrumentation;
} catch (e) {
/* empty */
}

try {
const {
LangChainInstrumentation,
} = require("@traceloop/instrumentation-langchain");
const instrumentation = new LangChainInstrumentation();
instrumentations.push(instrumentation as Instrumentation);
} catch (e) {
/* empty */
}

try {
const {
BedrockInstrumentation,
} = require("@traceloop/instrumentation-bedrock");
const instrumentation = new BedrockInstrumentation();
instrumentations.push(instrumentation as Instrumentation);
bedrockInstrumentation = instrumentation;
} catch (e) {
/* empty */
}

try {
const {
CohereInstrumentation,
} = require("@traceloop/instrumentation-cohere");
const instrumentation = new CohereInstrumentation();
instrumentations.push(instrumentation as Instrumentation);
cohereInstrumentation = instrumentation;
} catch (e) {
/* empty */
}
};

export const manuallyInitInstrumentations = (
instrumentModules: InitializeOptions["instrumentModules"],
) => {
if (instrumentModules?.openAI) {
const {
OpenAIInstrumentation,
} = require("@traceloop/instrumentation-openai");
const instrumentation = new OpenAIInstrumentation();
instrumentations.push(instrumentation as Instrumentation);
openAIInstrumentation = instrumentation;
instrumentation.manuallyInstrument(instrumentModules.openAI);
}

if (instrumentModules?.llamaIndex) {
const {
LlamaIndexInstrumentation,
} = require("@traceloop/instrumentation-llamaindex");
const instrumentation = new LlamaIndexInstrumentation();
instrumentations.push(instrumentation as Instrumentation);
llamaIndexInstrumentation = instrumentation;
instrumentation.manuallyInstrument(instrumentModules.llamaIndex);
}

if (hasModule("@pinecone-database/pinecone")) {
if (instrumentModules?.pinecone) {
const {
PineconeInstrumentation,
} = require("@traceloop/instrumentation-pinecone");
const instrumentation = new PineconeInstrumentation();
instrumentations.push(instrumentation as Instrumentation);
pineconeInstrumentation = instrumentation;
}

if (hasModule("@google-cloud/vertexai")) {
if (instrumentModules?.google_vertexai) {
const {
VertexAIInstrumentation,
} = require("@traceloop/instrumentation-vertexai");
Expand All @@ -87,7 +162,7 @@ export const initInstrumentations = () => {
vertexaiInstrumentation = instrumentation;
}

if (hasModule("@google-cloud/aiplatform")) {
if (instrumentModules?.google_aiplatform) {
const {
AIPlatformInstrumentation,
} = require("@traceloop/instrumentation-vertexai");
Expand All @@ -96,15 +171,7 @@ export const initInstrumentations = () => {
aiplatformInstrumentation = instrumentation;
}

if (hasModule("langchain")) {
const {
LangChainInstrumentation,
} = require("@traceloop/instrumentation-langchain");
const instrumentation = new LangChainInstrumentation();
instrumentations.push(instrumentation as Instrumentation);
}

if (hasModule("@aws-sdk/client-bedrock-runtime")) {
if (instrumentModules?.bedrock) {
const {
BedrockInstrumentation,
} = require("@traceloop/instrumentation-bedrock");
Expand All @@ -113,7 +180,16 @@ export const initInstrumentations = () => {
bedrockInstrumentation = instrumentation;
}

if (hasModule("cohere-ai")) {
if (instrumentModules?.azureOpenAI) {
const {
AzureOpenAIInstrumentation,
} = require("@traceloop/instrumentation-azure");
const instrumentation = new AzureOpenAIInstrumentation();
instrumentations.push(instrumentation as Instrumentation);
azureOpenAIInstrumentation = instrumentation;
}

if (instrumentModules?.cohere) {
const {
CohereInstrumentation,
} = require("@traceloop/instrumentation-cohere");
Expand All @@ -131,6 +207,11 @@ export const initInstrumentations = () => {
* @throws {InitializationError} if the configuration is invalid or if failed to fetch feature data.
*/
export const startTracing = (options: InitializeOptions) => {
if (Object.keys(options.instrumentModules || {}).length > 0) {
manuallyInitInstrumentations(options.instrumentModules);
} else {
initInstrumentations();
}
if (!shouldSendTraces()) {
openAIInstrumentation?.setConfig({
traceContent: false,
Expand Down Expand Up @@ -211,54 +292,6 @@ export const startTracing = (options: InitializeOptions) => {
});

_sdk.start();

if (options.instrumentModules?.openAI) {
(openAIInstrumentation as AIInstrumentation).manuallyInstrument(
options.instrumentModules.openAI,
);
}

if (options.instrumentModules?.llamaIndex) {
(llamaIndexInstrumentation as AIInstrumentation).manuallyInstrument(
options.instrumentModules.llamaIndex,
);
}

if (options.instrumentModules?.pinecone) {
(pineconeInstrumentation as AIInstrumentation).manuallyInstrument(
options.instrumentModules.pinecone,
);
}

if (options.instrumentModules?.google_vertexai) {
(vertexaiInstrumentation as AIInstrumentation).manuallyInstrument(
options.instrumentModules.google_vertexai,
);
}

if (options.instrumentModules?.google_aiplatform) {
(aiplatformInstrumentation as AIInstrumentation).manuallyInstrument(
options.instrumentModules.google_aiplatform,
);
}

if (options.instrumentModules?.bedrock) {
(bedrockInstrumentation as AIInstrumentation).manuallyInstrument(
options.instrumentModules.bedrock,
);
}

if (options.instrumentModules?.azureOpenAI) {
(azureOpenAIInstrumentation as AIInstrumentation).manuallyInstrument(
options.instrumentModules.azureOpenAI,
);
}

if (options.instrumentModules?.cohere) {
(cohereInstrumentation as AIInstrumentation).manuallyInstrument(
options.instrumentModules.cohere,
);
}
};

export const shouldSendTraces = () => {
Expand Down
Loading