diff --git a/develop-docs/sdk/telemetry/traces/modules/ai-agents.mdx b/develop-docs/sdk/telemetry/traces/modules/ai-agents.mdx index ae60466096767..6c58d534c1d62 100644 --- a/develop-docs/sdk/telemetry/traces/modules/ai-agents.mdx +++ b/develop-docs/sdk/telemetry/traces/modules/ai-agents.mdx @@ -126,14 +126,16 @@ Additional attributes on the span: ### Token Usage Data -| Attribute | Type | Description | Example | -| :----------------------------------------- | :--- | :----------------------------------------------------------------------------------------------------------------------------- | :------ | -| `gen_ai.usage.input_tokens` | int | The number of tokens used in the AI input (prompt), including cached tokens. | `60` | -| `gen_ai.usage.cache_read.input_tokens` | int | The number of cached tokens used in the AI input (prompt). | `50` | -| `gen_ai.usage.cache_creation.input_tokens` | int | Tokens written to cache when processing input. | `20` | -| `gen_ai.usage.output_tokens` | int | The number of tokens used in the AI output, including reasoning tokens. | `130` | -| `gen_ai.usage.reasoning.output_tokens` | int | The number of tokens used for reasoning. | `30` | -| `gen_ai.usage.total_tokens` | int | The sum of `gen_ai.usage.input_tokens` and `gen_ai.usage.output_tokens`. | `190` | + ## Embedding Span @@ -165,12 +167,14 @@ Describes a tool execution. Additional attributes on the span: -| Attribute | Type | Description | Example | -| :--------------------------- | :----- | :------------------------------------------------------------------------------------------------------------- | :----------------------------------------- | -| `gen_ai.tool.name` | string | Name of the tool executed. | `"random_number"` | -| `gen_ai.tool.description` | string | Description of the tool executed. | `"Tool returning a random number"` | -| `gen_ai.tool.call.arguments` | string | Arguments of the tool call (stringified). | `'{"max":10}'` | -| `gen_ai.tool.call.result` | string | Result of the tool call (stringified). | `"7"` | + ## Derived by Relay diff --git a/package.json b/package.json index e077145a96ce0..08a14972bd17b 100644 --- a/package.json +++ b/package.json @@ -66,6 +66,7 @@ "@radix-ui/themes": "^3.1.3", "@sentry-internal/global-search": "^1.3.0", "@sentry/browser": "11.6.0", + "@sentry/conventions": "^0.31.0", "@sentry/nextjs": "11.6.0", "@types/mdx": "^2.0.9", "algoliasearch": "^4.23.3", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index be0bb14b16151..dccedbbb13d1c 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -105,6 +105,9 @@ importers: '@sentry/browser': specifier: 11.6.0 version: 11.6.0 + '@sentry/conventions': + specifier: ^0.31.0 + version: 0.31.0 '@sentry/nextjs': specifier: 11.6.0 version: 11.6.0(@babel/core@7.29.7)(next@15.5.24(@babel/core@7.29.7)(@opentelemetry/api@1.9.1)(@types/node@22.19.11)(babel-plugin-macros@3.1.0)(react-dom@19.2.4(react@19.2.4))(react@19.2.4)(sass@1.98.0))(react@19.2.4)(webpack@5.105.2(esbuild@0.28.1)(postcss@8.5.23)) @@ -3115,6 +3118,10 @@ packages: resolution: {integrity: sha512-ttSRcoLf2kIENDIFzH12aJaVMdxnidMYLvVNwEX/JjXfXaQot8u5M47QmgAFOOhVJ4nQ2uU8wTCh+fwSPrrCfg==} engines: {node: '>=14'} + '@sentry/conventions@0.31.0': + resolution: {integrity: sha512-TOXB7z08JPVop4eiYLhZhgWitUYUMX6T1SjLCthaa8YLnNyA3+gbT1ccUinf2EWvzFwBuYPShPB7oYSkKeCFIQ==} + engines: {node: '>=14'} + '@sentry/core@11.6.0': resolution: {integrity: sha512-PbITT1kfJYKJiS9fH4ZtOcwIONKyt+tUsTZSPtcAjPdkUDOzMKT61MkUa1Ze07VzNtLALT24ZdUOc6d9K4YoKA==} engines: {node: '>=20.19.0 <22.0.0 || >=22.12.0 <23.0.0 || >=23.2.0'} @@ -10621,6 +10628,8 @@ snapshots: '@sentry/conventions@0.26.0': {} + '@sentry/conventions@0.31.0': {} + '@sentry/core@11.6.0': dependencies: '@sentry/conventions': 0.26.0 diff --git a/src/components/conventionAttributes/index.spec.tsx b/src/components/conventionAttributes/index.spec.tsx new file mode 100644 index 0000000000000..e2a9f6d933399 --- /dev/null +++ b/src/components/conventionAttributes/index.spec.tsx @@ -0,0 +1,63 @@ +import {ATTRIBUTE_METADATA} from '@sentry/conventions/attributes'; +import React from 'react'; +import {renderToStaticMarkup} from 'react-dom/server'; +import {describe, expect, it, vi} from 'vitest'; + +import {ConventionAttributes} from './index'; + +// Vitest compiles JSX with the classic runtime here (tsconfig `jsx: preserve`). +vi.stubGlobal('React', React); + +describe('ConventionAttributes', () => { + it('renders attributes from the conventions package with a category link', () => { + const html = renderToStaticMarkup( + + ); + + expect(html).toContain('gen_ai.tool.name'); + expect(html).toContain(ATTRIBUTE_METADATA['gen_ai.tool.name'].type); + expect(html).toContain(ATTRIBUTE_METADATA['gen_ai.tool.name'].brief); + expect(html).toContain( + 'href="https://getsentry.github.io/sentry-conventions/attributes/gen_ai/"' + ); + }); + + it('renders additional context as numbered notes, shared notes once', () => { + const keys = ['gen_ai.usage.input_tokens', 'gen_ai.usage.output_tokens']; + const html = renderToStaticMarkup(); + const notes = [ + ...new Set(keys.flatMap(key => ATTRIBUTE_METADATA[key].additionalContext ?? [])), + ]; + + expect(notes.length).toBeGreaterThan(0); + expect(html.match(/
  • [1]' + ); + expect(html).toContain('id="gen_ai-usage-input_tokens-note-1"'); + }); + + it('renders no notes list when attributes have no additional context', () => { + const html = renderToStaticMarkup( + + ); + + expect(html).not.toContain('
      '); + }); + + it('single-quotes string examples that contain double quotes', () => { + const html = renderToStaticMarkup( + + ); + const example = String(ATTRIBUTE_METADATA['gen_ai.tool.call.arguments'].example); + + expect(example).toContain('"'); + expect(html).toContain(`'${example.replace(/"/g, '"')}'`); + }); + + it('throws on unknown attributes so the build fails', () => { + expect(() => + renderToStaticMarkup() + ).toThrow('unknown attribute "gen_ai.not.a.thing"'); + }); +}); diff --git a/src/components/conventionAttributes/index.tsx b/src/components/conventionAttributes/index.tsx new file mode 100644 index 0000000000000..c88e76da4c3cd --- /dev/null +++ b/src/components/conventionAttributes/index.tsx @@ -0,0 +1,112 @@ +import { + ATTRIBUTE_METADATA, + type AttributeMetadata, + type AttributeValue, +} from '@sentry/conventions/attributes'; +import {micromark} from 'micromark'; +import {Fragment} from 'react'; + +type Props = { + attributes: string[]; +}; + +const metadata: Record = ATTRIBUTE_METADATA; + +/** + * Renders attribute details from `@sentry/conventions`, so pages don't copy + * types, descriptions, examples, and notes that drift from the source of truth. + * Each attribute's `additionalContext` becomes a numbered note under the table. + * Attributes that share a note point to the same number. + */ +export function ConventionAttributes({attributes}: Props) { + const notes: string[] = []; + const rows = attributes.map(key => { + const attribute = metadata[key]; + if (!attribute) { + throw new Error(`: unknown attribute "${key}"`); + } + const noteNumbers = (attribute.additionalContext ?? []).map(note => { + const index = notes.indexOf(note); + return index === -1 ? notes.push(note) : index + 1; + }); + return {key, attribute, noteNumbers}; + }); + const firstKey = rows[0].key; + const category = firstKey.includes('.') ? firstKey.split('.')[0] : 'general'; + const noteId = (n: number) => `${firstKey.replace(/\./g, '-')}-note-${n}`; + + return ( + + + + + + + + + + + + {rows.map(({key, attribute, noteNumbers}) => ( + + + + + + + ))} + +
      AttributeTypeDescriptionExample
      + {key} + {attribute.type} + + {noteNumbers.map(n => ( + + [{n}] + + ))} + + {attribute.example !== undefined && ( + {formatExample(attribute.example)} + )} +
      + {notes.length > 0 && ( +
        + {notes.map((note, i) => ( +
      1. + ))} +
      + )} +

      + See all{' '} + + {category} attributes in Sentry Conventions + + . +

      +
      + ); +} + +function inlineMarkdown(text: string) { + return micromark(text) + .replace(/^

      /, '') + .replace(/<\/p>$/, ''); +} + +// Single-quote strings that contain double quotes, such as stringified JSON, +// so they read as `'{"max":10}'` instead of escaped JSON. +function formatExample(example: AttributeValue) { + if (typeof example === 'string' && example.includes('"') && !example.includes("'")) { + return `'${example}'`; + } + return JSON.stringify(example); +} diff --git a/src/mdxComponents.ts b/src/mdxComponents.ts index 13d823b180d63..27d17c1f72fe1 100644 --- a/src/mdxComponents.ts +++ b/src/mdxComponents.ts @@ -12,6 +12,7 @@ import {CommunitySupportedPlatforms} from './components/communitySupportedPlatfo import {ConfigKey} from './components/configKey'; import {ConfigValue} from './components/configValue'; import {ContentSeparator} from './components/contentSeparator'; +import {ConventionAttributes} from './components/conventionAttributes'; import {CopyableCard} from './components/copyableCard'; import {CreateGitHubAppForm} from './components/createGitHubAppForm'; import {DefinitionList} from './components/definitionList'; @@ -82,6 +83,7 @@ export function mdxComponents( CliChecksumTable, CommunitySupportedPlatforms, ContentSeparator, + ConventionAttributes, DevDocsCardGrid, PlatformFilter, CodeBlock,