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
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as Sentry from '@sentry/browser';

window.Sentry = Sentry;

Sentry.init({
dsn: 'https://public@dsn.ingest.sentry.io/1337',
tracesSampleRate: 1,
debug: true,
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Mock LangGraph graph for browser testing
export class MockStateGraph {
compile(options = {}) {
const compiledGraph = {
name: options.name,
graph_name: options.name,
lc_kwargs: {
name: options.name,
},
builder: {
nodes: {},
},
invoke: async input => {
const messages = input?.messages;
return {
messages: [
...messages,
{
role: 'assistant',
content: 'Mock response from LangGraph',
},
],
};
},
};

return compiledGraph;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { MockStateGraph } from './mocks.js';
import { instrumentLangGraph } from '@sentry/browser';

// Test that manual instrumentation doesn't crash the browser
// The instrumentation automatically creates spans
// Test both agent creation and invocation

const graph = new MockStateGraph();
instrumentLangGraph(graph, { recordInputs: false, recordOutputs: false });
const compiledGraph = graph.compile({ name: 'mock-graph' });

const response = await compiledGraph.invoke({
messages: [{ role: 'user', content: 'What is the capital of France?' }],
});

console.log('Received response', response);
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { expect } from '@playwright/test';
import { sentryTest } from '../../../../utils/fixtures';
import { envelopeRequestParser, waitForTransactionRequest } from '../../../../utils/helpers';

// These tests are not exhaustive because the instrumentation is
// already tested in the node integration tests and we merely
// want to test that the instrumentation does not crash in the browser
// and that gen_ai transactions are sent.

sentryTest('manual LangGraph instrumentation sends gen_ai transactions', async ({ getLocalTestUrl, page }) => {
const createTransactionPromise = waitForTransactionRequest(page, event => {
return !!event.transaction?.includes('create_agent mock-graph');
});

const invokeTransactionPromise = waitForTransactionRequest(page, event => {
return !!event.transaction?.includes('invoke_agent mock-graph');
});

const url = await getLocalTestUrl({ testDir: __dirname });
await page.goto(url);

const createReq = await createTransactionPromise;
const invokeReq = await invokeTransactionPromise;

const createEventData = envelopeRequestParser(createReq);
const invokeEventData = envelopeRequestParser(invokeReq);

// Verify create_agent transaction
expect(createEventData.transaction).toBe('create_agent mock-graph');
expect(createEventData.contexts?.trace?.op).toBe('gen_ai.create_agent');
expect(createEventData.contexts?.trace?.origin).toBe('auto.ai.langgraph');
expect(createEventData.contexts?.trace?.data).toMatchObject({
'gen_ai.operation.name': 'create_agent',
'gen_ai.agent.name': 'mock-graph',
});

// Verify invoke_agent transaction
expect(invokeEventData.transaction).toBe('invoke_agent mock-graph');
expect(invokeEventData.contexts?.trace?.op).toBe('gen_ai.invoke_agent');
expect(invokeEventData.contexts?.trace?.origin).toBe('auto.ai.langgraph');
expect(invokeEventData.contexts?.trace?.data).toMatchObject({
'gen_ai.operation.name': 'invoke_agent',
'gen_ai.agent.name': 'mock-graph',
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const IMPORTED_INTEGRATION_CDN_BUNDLE_PATHS: Record<string, string> = {
instrumentAnthropicAiClient: 'instrumentanthropicaiclient',
instrumentOpenAiClient: 'instrumentopenaiclient',
instrumentGoogleGenAIClient: 'instrumentgooglegenaiclient',
instrumentLangGraph: 'instrumentlanggraph',
// technically, this is not an integration, but let's add it anyway for simplicity
makeMultiplexedTransport: 'multiplexedtransport',
};
Expand Down
1 change: 1 addition & 0 deletions packages/browser/rollup.bundle.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const reexportedPluggableIntegrationFiles = [
'instrumentanthropicaiclient',
'instrumentopenaiclient',
'instrumentgooglegenaiclient',
'instrumentlanggraph',
];

browserPluggableIntegrationFiles.forEach(integrationName => {
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 @@ -66,6 +66,7 @@ export {
instrumentAnthropicAiClient,
instrumentOpenAiClient,
instrumentGoogleGenAIClient,
instrumentLangGraph,
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 { instrumentLangGraph } from '@sentry/core';
1 change: 1 addition & 0 deletions packages/browser/src/utils/lazyLoadIntegration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const LazyLoadableIntegrations = {
instrumentAnthropicAiClient: 'instrumentanthropicaiclient',
instrumentOpenAiClient: 'instrumentopenaiclient',
instrumentGoogleGenAIClient: 'instrumentgooglegenaiclient',
instrumentLangGraph: 'instrumentlanggraph',
} as const;

const WindowWithMaybeIntegration = WINDOW as {
Expand Down