Skip to content
Draft
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
1 change: 1 addition & 0 deletions dev-packages/node-integration-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"@hono/node-server": "^1.19.10",
"@langchain/anthropic": "^0.3.10",
"@langchain/core": "^0.3.80",
"@langchain/openai": "^0.5.0",
"@langchain/langgraph": "^0.2.32",
"@nestjs/common": "^11",
"@nestjs/core": "^11",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Sentry.init({
transport: loggingTransport,
beforeSendTransaction: event => {
// Filter out mock express server transactions
if (event.transaction.includes('/v1/messages')) {
if (event.transaction.includes('/v1/messages') || event.transaction.includes('/v1/embeddings')) {
return null;
}
return event;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Sentry.init({
transport: loggingTransport,
beforeSendTransaction: event => {
// Filter out mock express server transactions
if (event.transaction.includes('/v1/messages')) {
if (event.transaction.includes('/v1/messages') || event.transaction.includes('/v1/embeddings')) {
return null;
}
return event;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { OpenAIEmbeddings } from '@langchain/openai';
import * as Sentry from '@sentry/node';
import express from 'express';

function startMockOpenAIServer() {
const app = express();
app.use(express.json());

app.post('/v1/embeddings', (req, res) => {
const { model, input } = req.body;

if (model === 'error-model') {
res.status(400).json({
error: {
message: 'Model not found',
type: 'invalid_request_error',
},
});
return;
}

const inputs = Array.isArray(input) ? input : [input];
res.json({
object: 'list',
data: inputs.map((_, i) => ({
object: 'embedding',
embedding: [0.1, 0.2, 0.3],
index: i,
})),
model: model,
usage: {
prompt_tokens: 10,
total_tokens: 10,
},
});
});

return new Promise(resolve => {
const server = app.listen(0, () => {
resolve(server);
});
});
}

async function run() {
const server = await startMockOpenAIServer();
const baseUrl = `http://localhost:${server.address().port}/v1`;

await Sentry.startSpan({ op: 'function', name: 'main' }, async () => {
// Test 1: embedQuery
const embeddings = new OpenAIEmbeddings({
model: 'text-embedding-3-small',
dimensions: 1536,
apiKey: 'mock-api-key',
configuration: { baseURL: baseUrl },
});

await embeddings.embedQuery('Hello world');

// Test 2: embedDocuments
await embeddings.embedDocuments(['First document', 'Second document']);

// Test 3: Error handling
const errorEmbeddings = new OpenAIEmbeddings({
model: 'error-model',
apiKey: 'mock-api-key',
configuration: { baseURL: baseUrl },
});

try {
await errorEmbeddings.embedQuery('This will fail');
} catch {
// Expected error
}
});

await Sentry.flush(2000);

server.close();
}

run();
119 changes: 119 additions & 0 deletions dev-packages/node-integration-tests/suites/tracing/langchain/test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { SEMANTIC_ATTRIBUTE_SENTRY_OP, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN } from '@sentry/core';
import { afterAll, describe, expect } from 'vitest';
import {
GEN_AI_EMBED_DO_EMBED_OPERATION_ATTRIBUTE,
GEN_AI_EMBEDDINGS_INPUT_ATTRIBUTE,
GEN_AI_INPUT_MESSAGES_ATTRIBUTE,
GEN_AI_INPUT_MESSAGES_ORIGINAL_LENGTH_ATTRIBUTE,
GEN_AI_OPERATION_NAME_ATTRIBUTE,
GEN_AI_REQUEST_DIMENSIONS_ATTRIBUTE,
GEN_AI_REQUEST_MAX_TOKENS_ATTRIBUTE,
GEN_AI_REQUEST_MODEL_ATTRIBUTE,
GEN_AI_REQUEST_TEMPERATURE_ATTRIBUTE,
Expand Down Expand Up @@ -430,4 +433,120 @@ describe('LangChain integration', () => {
.completed();
});
});

// =========================================================================
// Embeddings tests
// =========================================================================

const EXPECTED_TRANSACTION_EMBEDDINGS = {
transaction: 'main',
spans: expect.arrayContaining([
// embedQuery span
expect.objectContaining({
data: expect.objectContaining({
[GEN_AI_OPERATION_NAME_ATTRIBUTE]: 'embed',
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: GEN_AI_EMBED_DO_EMBED_OPERATION_ATTRIBUTE,
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.ai.langchain',
[GEN_AI_SYSTEM_ATTRIBUTE]: 'openai',
[GEN_AI_REQUEST_MODEL_ATTRIBUTE]: 'text-embedding-3-small',
[GEN_AI_REQUEST_DIMENSIONS_ATTRIBUTE]: 1536,
}),
description: 'embed text-embedding-3-small',
op: GEN_AI_EMBED_DO_EMBED_OPERATION_ATTRIBUTE,
origin: 'auto.ai.langchain',
status: 'ok',
}),
// embedDocuments span
expect.objectContaining({
data: expect.objectContaining({
[GEN_AI_OPERATION_NAME_ATTRIBUTE]: 'embed_many',
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: GEN_AI_EMBED_DO_EMBED_OPERATION_ATTRIBUTE,
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.ai.langchain',
[GEN_AI_SYSTEM_ATTRIBUTE]: 'openai',
[GEN_AI_REQUEST_MODEL_ATTRIBUTE]: 'text-embedding-3-small',
}),
description: 'embed_many text-embedding-3-small',
op: GEN_AI_EMBED_DO_EMBED_OPERATION_ATTRIBUTE,
origin: 'auto.ai.langchain',
status: 'ok',
}),
// Error span
expect.objectContaining({
data: expect.objectContaining({
[GEN_AI_OPERATION_NAME_ATTRIBUTE]: 'embed',
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: GEN_AI_EMBED_DO_EMBED_OPERATION_ATTRIBUTE,
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.ai.langchain',
[GEN_AI_SYSTEM_ATTRIBUTE]: 'openai',
[GEN_AI_REQUEST_MODEL_ATTRIBUTE]: 'error-model',
}),
description: 'embed error-model',
op: GEN_AI_EMBED_DO_EMBED_OPERATION_ATTRIBUTE,
origin: 'auto.ai.langchain',
status: 'internal_error',
}),
]),
};

const EXPECTED_TRANSACTION_EMBEDDINGS_PII = {
transaction: 'main',
spans: expect.arrayContaining([
// embedQuery span with input recorded
expect.objectContaining({
data: expect.objectContaining({
[GEN_AI_OPERATION_NAME_ATTRIBUTE]: 'embed',
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: GEN_AI_EMBED_DO_EMBED_OPERATION_ATTRIBUTE,
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.ai.langchain',
[GEN_AI_SYSTEM_ATTRIBUTE]: 'openai',
[GEN_AI_REQUEST_MODEL_ATTRIBUTE]: 'text-embedding-3-small',
[GEN_AI_EMBEDDINGS_INPUT_ATTRIBUTE]: 'Hello world',
}),
description: 'embed text-embedding-3-small',
op: GEN_AI_EMBED_DO_EMBED_OPERATION_ATTRIBUTE,
origin: 'auto.ai.langchain',
status: 'ok',
}),
// embedDocuments span with input recorded
expect.objectContaining({
data: expect.objectContaining({
[GEN_AI_OPERATION_NAME_ATTRIBUTE]: 'embed_many',
[GEN_AI_EMBEDDINGS_INPUT_ATTRIBUTE]: JSON.stringify(['First document', 'Second document']),
}),
description: 'embed_many text-embedding-3-small',
op: GEN_AI_EMBED_DO_EMBED_OPERATION_ATTRIBUTE,
origin: 'auto.ai.langchain',
status: 'ok',
}),
]),
};

createEsmAndCjsTests(__dirname, 'scenario-embeddings.mjs', 'instrument.mjs', (createRunner, test) => {
test('creates embedding spans with sendDefaultPii: false', async () => {
await createRunner().ignore('event').expect({ transaction: EXPECTED_TRANSACTION_EMBEDDINGS }).start().completed();
});

test('does not create duplicate embedding spans from double module patching', async () => {
await createRunner()
.ignore('event')
.expect({
transaction: event => {
const spans = event.spans || [];
const embeddingSpans = spans.filter(span => span.op === GEN_AI_EMBED_DO_EMBED_OPERATION_ATTRIBUTE);
// The scenario makes 3 embedding calls (2 successful + 1 error).
expect(embeddingSpans).toHaveLength(3);
},
})
.start()
.completed();
});
});

createEsmAndCjsTests(__dirname, 'scenario-embeddings.mjs', 'instrument-with-pii.mjs', (createRunner, test) => {
test('creates embedding spans with sendDefaultPii: true', async () => {
await createRunner()
.ignore('event')
.expect({ transaction: EXPECTED_TRANSACTION_EMBEDDINGS_PII })
.start()
.completed();
});
});
});
1 change: 1 addition & 0 deletions packages/aws-serverless/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export {
getSentryRelease,
createGetModuleFromFilename,
createLangChainCallbackHandler,
wrapLangChainEmbeddings,
httpHeadersToSpanAttributes,
winterCGHeadersToDict,
// eslint-disable-next-line deprecation/deprecation
Expand Down
1 change: 1 addition & 0 deletions packages/browser/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export {
instrumentGoogleGenAIClient,
instrumentLangGraph,
createLangChainCallbackHandler,
wrapLangChainEmbeddings,
logger,
} from '@sentry/core';
export type { Span, FeatureFlagsIntegration } from '@sentry/core';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { wrapLangChainEmbeddings } from '@sentry/core';
1 change: 1 addition & 0 deletions packages/bun/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export {
getSentryRelease,
createGetModuleFromFilename,
createLangChainCallbackHandler,
wrapLangChainEmbeddings,
httpHeadersToSpanAttributes,
winterCGHeadersToDict,
// eslint-disable-next-line deprecation/deprecation
Expand Down
1 change: 1 addition & 0 deletions packages/cloudflare/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ export {
consoleLoggingIntegration,
createConsolaReporter,
createLangChainCallbackHandler,
wrapLangChainEmbeddings,
featureFlagsIntegration,
growthbookIntegration,
logger,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ export { ANTHROPIC_AI_INTEGRATION_NAME } from './tracing/anthropic-ai/constants'
export { instrumentGoogleGenAIClient } from './tracing/google-genai';
export { GOOGLE_GENAI_INTEGRATION_NAME } from './tracing/google-genai/constants';
export type { GoogleGenAIResponse } from './tracing/google-genai/types';
export { createLangChainCallbackHandler } from './tracing/langchain';
export { createLangChainCallbackHandler, wrapEmbeddingMethod, wrapLangChainEmbeddings } from './tracing/langchain';
export { LANGCHAIN_INTEGRATION_NAME } from './tracing/langchain/constants';
export type { LangChainOptions, LangChainIntegration } from './tracing/langchain/types';
export { instrumentStateGraphCompile, instrumentLangGraph } from './tracing/langgraph';
Expand Down
Loading
Loading