Skip to content
Closed
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
32 changes: 18 additions & 14 deletions develop-docs/sdk/telemetry/traces/modules/ai-agents.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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` |
<ConventionAttributes
attributes={[
'gen_ai.usage.input_tokens',
'gen_ai.usage.cache_read.input_tokens',
'gen_ai.usage.cache_creation.input_tokens',
'gen_ai.usage.output_tokens',
'gen_ai.usage.reasoning.output_tokens',
'gen_ai.usage.total_tokens',
]}
/>

## Embedding Span

Expand Down Expand Up @@ -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"` |
<ConventionAttributes
attributes={[
'gen_ai.tool.name',
'gen_ai.tool.description',
'gen_ai.tool.call.arguments',
'gen_ai.tool.call.result',
]}
/>

## Derived by Relay

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 63 additions & 0 deletions src/components/conventionAttributes/index.spec.tsx
Original file line number Diff line number Diff line change
@@ -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(
<ConventionAttributes attributes={['gen_ai.tool.name']} />
);

expect(html).toContain('<code>gen_ai.tool.name</code>');
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(<ConventionAttributes attributes={keys} />);
const notes = [
...new Set(keys.flatMap(key => ATTRIBUTE_METADATA[key].additionalContext ?? [])),
];

expect(notes.length).toBeGreaterThan(0);
expect(html.match(/<li /g)).toHaveLength(notes.length);
expect(html).toContain(
'<sup><a href="#gen_ai-usage-input_tokens-note-1">[1]</a></sup>'
);
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(
<ConventionAttributes attributes={['gen_ai.tool.name']} />
);

expect(html).not.toContain('<ol>');
});

it('single-quotes string examples that contain double quotes', () => {
const html = renderToStaticMarkup(
<ConventionAttributes attributes={['gen_ai.tool.call.arguments']} />
);
const example = String(ATTRIBUTE_METADATA['gen_ai.tool.call.arguments'].example);

expect(example).toContain('"');
expect(html).toContain(`<code>&#x27;${example.replace(/"/g, '&quot;')}&#x27;</code>`);
});

it('throws on unknown attributes so the build fails', () => {
expect(() =>
renderToStaticMarkup(<ConventionAttributes attributes={['gen_ai.not.a.thing']} />)
).toThrow('unknown attribute "gen_ai.not.a.thing"');
});
});
112 changes: 112 additions & 0 deletions src/components/conventionAttributes/index.tsx
Original file line number Diff line number Diff line change
@@ -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<string, AttributeMetadata> = 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(`<ConventionAttributes>: 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 (
<Fragment>
<table>
<thead>
<tr>
<th>Attribute</th>
<th>Type</th>
<th>Description</th>
<th>Example</th>
</tr>
</thead>
<tbody>
{rows.map(({key, attribute, noteNumbers}) => (
<tr key={key}>
<td>
<code>{key}</code>
</td>
<td>{attribute.type}</td>
<td>
<span
dangerouslySetInnerHTML={{__html: inlineMarkdown(attribute.brief)}}
/>
{noteNumbers.map(n => (
<sup key={n}>
<a href={`#${noteId(n)}`}>[{n}]</a>
</sup>
))}
</td>
<td>
{attribute.example !== undefined && (
<code>{formatExample(attribute.example)}</code>
)}
</td>
</tr>
))}
</tbody>
</table>
{notes.length > 0 && (
<ol>
{notes.map((note, i) => (
<li
key={note}
id={noteId(i + 1)}
dangerouslySetInnerHTML={{__html: inlineMarkdown(note)}}
/>
))}
</ol>
)}
<p>
See all{' '}
<a
href={`https://getsentry.github.io/sentry-conventions/attributes/${category}/`}
>
<code>{category}</code> attributes in Sentry Conventions
</a>
.
</p>
</Fragment>
);
}

function inlineMarkdown(text: string) {
return micromark(text)
.replace(/^<p>/, '')
.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);
}
Comment thread
sentry-junior[bot] marked this conversation as resolved.
Resolved
2 changes: 2 additions & 0 deletions src/mdxComponents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -82,6 +83,7 @@ export function mdxComponents(
CliChecksumTable,
CommunitySupportedPlatforms,
ContentSeparator,
ConventionAttributes,
DevDocsCardGrid,
PlatformFilter,
CodeBlock,
Expand Down
Loading