Repository navigation
feat(develop-docs): Render span attributes from sentry-conventions #20557
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fa33fb4
feat(develop-docs): Render span attributes from sentry-conventions
sentry-junior[bot] d021e0c
ref(develop-docs): Simplify ConventionAttributes component
dingsdax 8818a71
Merge remote-tracking branch 'origin/master' into junior/convention-a…
sentry-junior[bot] 418cb56
feat(develop-docs): Render attribute notes from sentry-conventions
sentry-junior[bot] 23302c9
ref(develop-docs): Render attribute note markers as superscript
sentry-junior[bot] f5522cb
fix(develop-docs): Quote stringified JSON examples in ConventionAttri…
sentry-junior[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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>'${example.replace(/"/g, '"')}'</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"'); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.