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
2 changes: 1 addition & 1 deletion .size-limit.js
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ module.exports = [
ignore: [...builtinModules, ...nodePrefixedBuiltinModules],
gzip: false,
brotli: false,
limit: '540 KiB',
limit: '541 KiB',
disablePlugins: ['@size-limit/webpack'],
webpack: false,
modifyEsbuildConfig: function (config) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as Sentry from '@sentry/cloudflare';
import { instrumentWorkersAiClient } from '@sentry/core';
import { MockAi } from './mocks';

interface Env {
Expand All @@ -24,7 +23,7 @@ export default Sentry.withSentry(
// Wrapped in-request, mirroring how `@sentry/cloudflare` instruments `env.AI` on first
// property access: after `withSentry` has initialized the SDK, so `dataCollection.genAI`
// is visible when recording options are resolved.
const ai = instrumentWorkersAiClient(aiBinding);
const ai = Sentry.instrumentWorkersAiClient(aiBinding);

const url = new URL(request.url);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as Sentry from '@sentry/cloudflare';
import { instrumentWorkersAiClient } from '@sentry/core';
import { AIChatAgent } from '@cloudflare/ai-chat';
import { Agent, callable, routeAgentRequest } from 'agents';
import { streamText } from 'ai';
Expand All @@ -23,7 +22,7 @@ const sentryOptions = (env: Env) => ({
* Vercel AI SDK + `workers-ai-provider` stack (the OpenAI-compatible SSE shape).
*/
function streamWorkersAi(): Response {
const ai = instrumentWorkersAiClient(new MockAi(), { recordInputs: true, recordOutputs: true });
const ai = Sentry.instrumentWorkersAiClient(new MockAi(), { recordInputs: true, recordOutputs: true });
const workersai = createWorkersAI({ binding: ai as unknown as Ai });

const result = streamText({
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 @@ -130,6 +130,7 @@ export {
instrumentOpenAiClient,
instrumentAnthropicAiClient,
instrumentGoogleGenAIClient,
instrumentWorkersAiClient,
} from '@sentry/server-utils';

// eslint-disable-next-line typescript/no-deprecated
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isObjectLike } from '@sentry/core';
import { instrumentWorkersAiClient } from '@sentry/core';
import { instrumentWorkersAiClient } from '@sentry/server-utils';
import type { CloudflareOptions } from '../../client';
import {
isAiBinding,
Expand Down
2 changes: 0 additions & 2 deletions packages/core/src/server-exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,3 @@ export {
export { wrapToolsWithSpans, extractLLMFromParams, extractAgentNameFromParams } from './tracing/langgraph/utils';
export { LANGGRAPH_INTEGRATION_NAME } from './tracing/langgraph/constants';
export type { LangGraphOptions, LangGraphIntegration, CompiledGraph } from './tracing/langgraph/types';
export { instrumentWorkersAiClient } from './tracing/workers-ai';
export type { WorkersAiClient, WorkersAiOptions } from './tracing/workers-ai/types';
3 changes: 2 additions & 1 deletion packages/core/src/tracing/vercel-ai/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import type { Event } from '../../types/event';
import type { Span, SpanAttributes, SpanAttributeValue, SpanJSON, StreamedSpanJSON } from '../../types/span';
import { _INTERNAL_skipAiProviderWrapping } from '../../utils/ai/providerSkip';
import { spanToJSON } from '../../utils/spanUtils';
import { WORKERS_AI_INTEGRATION_NAME } from '../workers-ai/constants';
import {
GEN_AI_CONVERSATION_ID,
GEN_AI_EMBEDDINGS_INPUT,
Expand Down Expand Up @@ -65,6 +64,8 @@ import {
OPERATION_NAME_ATTRIBUTE,
} from './vercel-ai-attributes';

const WORKERS_AI_INTEGRATION_NAME = 'WorkersAI';

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

m: We have 3 WORKERS_AI_INTEGRATION_NAME, the plan was once this is consolidated into one, to export it from server-utils and then import it wherever this is needed.

There should be a todo in vercel-ai-dc-subscriber.ts

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch I'll fix this in the vercel ai when all the integrations that need this are moved to server utils


/**
* Post-process spans emitted by the Vercel AI SDK.
* This is supposed to be used in `client.on('spanStart', ...)
Expand Down
1 change: 1 addition & 0 deletions packages/server-utils/src/ai/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
export { instrumentOpenAiClient } from './openai';
export { instrumentAnthropicAiClient } from './anthropic-ai';
export { instrumentGoogleGenAIClient } from './google-genai';
export { instrumentWorkersAiClient } from './workers-ai';
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { SPAN_STATUS_ERROR } from '../../tracing';
import { startSpan, startSpanManual } from '../../tracing/trace';
import type { Span } from '../../types/span';
import { _INTERNAL_shouldSkipAiProviderWrapping } from '../../utils/ai/providerSkip';
import { isObjectLike } from '../../utils/is';
import { resolveAIRecordingOptions, shouldEnableTruncation } from '../ai/utils';
import {
_INTERNAL_shouldSkipAiProviderWrapping,
isObjectLike,
SPAN_STATUS_ERROR,
startSpan,
startSpanManual,
} from '@sentry/core';
import type { Span } from '@sentry/core';
import { resolveAIRecordingOptions, shouldEnableTruncation } from '../core/utils';
import { WORKERS_AI_INTEGRATION_NAME } from './constants';
import { instrumentWorkersAiStream } from './streaming';
import type { WorkersAiOptions } from './types';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SPAN_STATUS_ERROR } from '../../tracing';
import type { Span } from '../../types/span';
import { endStreamSpan, type StreamResponseState } from '../ai/utils';
import { SPAN_STATUS_ERROR } from '@sentry/core';
import type { Span } from '@sentry/core';
import { endStreamSpan, type StreamResponseState } from '../core/utils';
import type { WorkersAiUsage } from './types';
import { setOutputMessagesAttribute } from './utils';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { AIRecordingOptions } from '../ai/utils';
import type { AIRecordingOptions } from '../core/utils';

export interface WorkersAiOptions extends AIRecordingOptions {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ import {
GEN_AI_RESPONSE_TOOL_CALLS,
GEN_AI_SYSTEM_INSTRUCTIONS,
} from '@sentry/conventions/attributes';
import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN } from '../../semanticAttributes';
import type { Span, SpanAttributeValue } from '../../types/span';
import { GEN_AI_REQUEST_STREAM_ATTRIBUTE } from '../ai/gen-ai-attributes';
import { extractSystemInstructions, getTruncatedJsonString, setTokenUsageAttributes } from '../ai/utils';
import { stringify } from '../../utils/string';
import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN } from '@sentry/core';
import type { Span, SpanAttributeValue } from '@sentry/core';
import { GEN_AI_REQUEST_STREAM_ATTRIBUTE } from '../core/gen-ai-attributes';
import { extractSystemInstructions, getTruncatedJsonString, setTokenUsageAttributes } from '../core/utils';
import { stringify } from '@sentry/core';
import { WORKERS_AI_ORIGIN, WORKERS_AI_PROVIDER_NAME } from './constants';
import type { WorkersAiInput, WorkersAiOutput } from './types';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from 'vitest';
import type { Span } from '../../../src';
import type { Span } from '@sentry/core';
import {
GEN_AI_OUTPUT_MESSAGES,
GEN_AI_RESPONSE_STREAMING,
Expand All @@ -9,7 +9,7 @@ import {
GEN_AI_USAGE_OUTPUT_TOKENS,
GEN_AI_USAGE_TOTAL_TOKENS,
} from '@sentry/conventions/attributes';
import { instrumentWorkersAiStream } from '../../../src/tracing/workers-ai/streaming';
import { instrumentWorkersAiStream } from '../../../../src/ai/workers-ai/streaming';

function createMockSpan(): { span: Span; attributes: Record<string, unknown>; ended: () => boolean } {
const attributes: Record<string, unknown> = {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,46 @@ import {
GEN_AI_SYSTEM_INSTRUCTIONS,
} from '@sentry/conventions/attributes';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import type { Span } from '../../../src';
import {
_INTERNAL_clearAiProviderSkips,
addVercelAiProcessors,
Client,
createTransport,
getCurrentScope,
getGlobalScope,
getIsolationScope,
resolvedSyncPromise,
SEMANTIC_ATTRIBUTE_SENTRY_OP,
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE,
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
setCurrentClient,
spanToJSON,
startSpan,
} from '../../../src';
import { addVercelAiProcessors } from '../../../src/tracing/vercel-ai';
import { AI_OPERATION_ID_ATTRIBUTE } from '../../../src/tracing/vercel-ai/vercel-ai-attributes';
import { instrumentWorkersAiClient } from '../../../src/tracing/workers-ai';
import type { DataCollection } from '../../../src/types/datacollection';
import { _INTERNAL_clearAiProviderSkips } from '../../../src/utils/ai/providerSkip';
import { spanToJSON } from '../../../src/utils/spanUtils';
import { getDefaultTestClientOptions, TestClient } from '../../mocks/client';
} from '@sentry/core';
import type { ClientOptions, DataCollection, Span } from '@sentry/core';
import { instrumentWorkersAiClient } from '../../../../src/ai/workers-ai';

const AI_OPERATION_ID_ATTRIBUTE = 'ai.operationId';

function getDefaultTestClientOptions(options: Partial<ClientOptions> = {}): ClientOptions {
return {
integrations: [],
transport: () => createTransport({ recordDroppedEvent: () => undefined }, () => resolvedSyncPromise({})),
stackParser: () => [],
...options,
} as ClientOptions;
}

class TestClient extends Client<any> {
public eventFromException(): PromiseLike<any> {
return resolvedSyncPromise({});
}

public eventFromMessage(): PromiseLike<any> {
return resolvedSyncPromise({});
}
}

const MODEL = '@cf/meta/llama-3.1-8b-instruct';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest';
import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN } from '../../../src';
import type { Span } from '../../../src';
import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN } from '@sentry/core';
import type { Span } from '@sentry/core';
import {
GEN_AI_PROVIDER_NAME,
GEN_AI_EMBEDDINGS_INPUT,
Expand All @@ -21,14 +21,14 @@ import {
GEN_AI_USAGE_OUTPUT_TOKENS,
GEN_AI_USAGE_TOTAL_TOKENS,
} from '@sentry/conventions/attributes';
import { GEN_AI_REQUEST_STREAM_ATTRIBUTE } from '../../../src/tracing/ai/gen-ai-attributes';
import { WORKERS_AI_ORIGIN, WORKERS_AI_PROVIDER_NAME } from '../../../src/tracing/workers-ai/constants';
import { GEN_AI_REQUEST_STREAM_ATTRIBUTE } from '../../../../src/ai/core/gen-ai-attributes';
import { WORKERS_AI_ORIGIN, WORKERS_AI_PROVIDER_NAME } from '../../../../src/ai/workers-ai/constants';
import {
addRequestAttributes,
addResponseAttributes,
extractRequestAttributes,
getOperationName,
} from '../../../src/tracing/workers-ai/utils';
} from '../../../../src/ai/workers-ai/utils';

const MODEL = '@cf/meta/llama-3.1-8b-instruct';

Expand Down
Loading