-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
feat(tracemetrics): Add initial dataset implementation to dashboards #104518
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
Merged
narsaynorath
merged 26 commits into
master
from
nar/feat/tracemetrics-add-dataset-to-dashboards
Dec 10, 2025
+1,000
−182
Merged
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
7acd044
wip
narsaynorath 0e0d522
wip
narsaynorath 9e838d8
wip
narsaynorath 3d0552e
Merge branch 'master' into nar/feat/tracemetrics-add-dataset-to-dashb…
narsaynorath 49be8d1
Merge branch 'master' into nar/feat/tracemetrics-add-dataset-to-dashb…
narsaynorath 5d9c904
Add metrics dataset and enable saving/editing
narsaynorath d15c6c1
disable loading until aggregate is filled in
narsaynorath 53e9d24
Ensure aggregate is always a valid option, change it if not
narsaynorath 3473331
Simplify dataset config
narsaynorath 8052033
Merge branch 'master' into nar/feat/tracemetrics-add-dataset-to-dashb…
narsaynorath d47950e
Cleanup
narsaynorath 32ca634
cleanup comments
narsaynorath 94f644d
Use empty const
narsaynorath dbefbf1
reorder hook args to avoid diff
narsaynorath fd6ee36
feat(tracemetrics): Add dataset to dashboards
narsaynorath f88be44
re-add select wrapper
narsaynorath 28e9c9d
Small fixes
narsaynorath 613a468
only encode tracemetrics if applicable
narsaynorath 5a56f44
fix test
narsaynorath dd95ed1
Allow grouping by aggregate metric
narsaynorath 1251e59
fix orderby when adding
narsaynorath 442dc37
Update comment for timeseries sort options
narsaynorath 2f022cb
Remove duplication and use flex component
narsaynorath 434cdb0
Re-add timeseries sort options
narsaynorath fddc171
fix visualize deletion for tracemetric
narsaynorath 09534d1
cleanup
narsaynorath 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
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
270 changes: 270 additions & 0 deletions
270
static/app/views/dashboards/datasetConfig/traceMetrics.tsx
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,270 @@ | ||
| import pickBy from 'lodash/pickBy'; | ||
|
|
||
| import {doEventsRequest} from 'sentry/actionCreators/events'; | ||
| import type {ApiResult, Client} from 'sentry/api'; | ||
| import type {PageFilters, SelectValue} from 'sentry/types/core'; | ||
| import type {TagCollection} from 'sentry/types/group'; | ||
| import type {Organization} from 'sentry/types/organization'; | ||
| import type {CustomMeasurementCollection} from 'sentry/utils/customMeasurements/customMeasurements'; | ||
| import {parseFunction, type QueryFieldValue} from 'sentry/utils/discover/fields'; | ||
| import {DiscoverDatasets} from 'sentry/utils/discover/types'; | ||
| import type {MEPState} from 'sentry/utils/performance/contexts/metricsEnhancedSetting'; | ||
| import type {OnDemandControlContext} from 'sentry/utils/performance/contexts/onDemandControl'; | ||
| import type {EventsTimeSeriesResponse} from 'sentry/utils/timeSeries/useFetchEventsTimeSeries'; | ||
| import usePageFilters from 'sentry/utils/usePageFilters'; | ||
| import { | ||
| type DatasetConfig, | ||
| type SearchBarData, | ||
| type SearchBarDataProviderProps, | ||
| type WidgetBuilderSearchBarProps, | ||
| } from 'sentry/views/dashboards/datasetConfig/base'; | ||
| import { | ||
| getTableSortOptions, | ||
| getTimeseriesSortOptions, | ||
| } from 'sentry/views/dashboards/datasetConfig/errorsAndTransactions'; | ||
| import {combineBaseFieldsWithTags} from 'sentry/views/dashboards/datasetConfig/utils/combineBaseFieldsWithEapTags'; | ||
| import {getSeriesRequestData} from 'sentry/views/dashboards/datasetConfig/utils/getSeriesRequestData'; | ||
| import {useHasTraceMetricsDashboards} from 'sentry/views/dashboards/hooks/useHasTraceMetricsDashboards'; | ||
| import {DisplayType, type Widget, type WidgetQuery} from 'sentry/views/dashboards/types'; | ||
| import {useWidgetBuilderContext} from 'sentry/views/dashboards/widgetBuilder/contexts/widgetBuilderContext'; | ||
| import {formatTimeSeriesLabel} from 'sentry/views/dashboards/widgets/timeSeriesWidget/formatters/formatTimeSeriesLabel'; | ||
| import type {FieldValueOption} from 'sentry/views/discover/table/queryField'; | ||
| import {FieldValueKind} from 'sentry/views/discover/table/types'; | ||
| import { | ||
| TraceItemSearchQueryBuilder, | ||
| useSearchQueryBuilderProps, | ||
| } from 'sentry/views/explore/components/traceItemSearchQueryBuilder'; | ||
| import {useTraceItemAttributesWithConfig} from 'sentry/views/explore/contexts/traceItemAttributeContext'; | ||
| import type {SamplingMode} from 'sentry/views/explore/hooks/useProgressiveQuery'; | ||
| import {TraceItemDataset} from 'sentry/views/explore/types'; | ||
|
|
||
| // This is a placeholder that currently signals that no metric is selected | ||
| // When the metrics are loaded up, the first metric is selected and this will be filled out | ||
| export const EMPTY_METRIC_SELECTION = 'avg(value,,,-)'; | ||
|
|
||
| const DEFAULT_WIDGET_QUERY: WidgetQuery = { | ||
| name: '', | ||
| fields: [], | ||
| columns: [], | ||
| fieldAliases: [], | ||
| aggregates: [EMPTY_METRIC_SELECTION], | ||
| conditions: '', | ||
| orderby: '', | ||
| }; | ||
|
|
||
| const DEFAULT_FIELD: QueryFieldValue = { | ||
| function: ['avg', 'value', undefined, undefined], | ||
| kind: FieldValueKind.FUNCTION, | ||
| }; | ||
|
|
||
| function TraceMetricsSearchBar({ | ||
| widgetQuery, | ||
| onSearch, | ||
| portalTarget, | ||
| onClose, | ||
| }: Pick< | ||
| WidgetBuilderSearchBarProps, | ||
| 'widgetQuery' | 'onSearch' | 'portalTarget' | 'onClose' | ||
| >) { | ||
| const { | ||
| selection: {projects}, | ||
| } = usePageFilters(); | ||
| const hasTraceMetricsDashboards = useHasTraceMetricsDashboards(); | ||
| const {state: widgetBuilderState} = useWidgetBuilderContext(); | ||
|
|
||
| // TODO: Make decision on how filtering works with multiple trace metrics | ||
| // We should probably limit it to only one metric for now because there's no way | ||
| // to filter by multiple metrics at the same time, unless the filter _ONLY_ includes | ||
| // tags for both metrics. | ||
| const traceMetric = widgetBuilderState.traceMetrics?.[0]; | ||
|
|
||
| const traceItemAttributeConfig = { | ||
| traceItemType: TraceItemDataset.TRACEMETRICS, | ||
| enabled: hasTraceMetricsDashboards, | ||
| }; | ||
|
|
||
| const {attributes: stringAttributes, secondaryAliases: stringSecondaryAliases} = | ||
| useTraceItemAttributesWithConfig(traceItemAttributeConfig, 'string'); | ||
| const {attributes: numberAttributes, secondaryAliases: numberSecondaryAliases} = | ||
| useTraceItemAttributesWithConfig(traceItemAttributeConfig, 'number'); | ||
|
|
||
| return ( | ||
| <TraceItemSearchQueryBuilder | ||
| initialQuery={widgetQuery.conditions} | ||
| onSearch={onSearch} | ||
| itemType={TraceItemDataset.TRACEMETRICS} | ||
| numberAttributes={numberAttributes} | ||
| stringAttributes={stringAttributes} | ||
| numberSecondaryAliases={numberSecondaryAliases} | ||
| stringSecondaryAliases={stringSecondaryAliases} | ||
| searchSource="dashboards" | ||
| projects={projects} | ||
| portalTarget={portalTarget} | ||
| onChange={(query, state) => { | ||
| onClose?.(query, {validSearch: state.queryIsValid}); | ||
| }} | ||
| namespace={traceMetric?.name} | ||
| /> | ||
| ); | ||
| } | ||
|
|
||
| function useTraceMetricsSearchBarDataProvider( | ||
| props: SearchBarDataProviderProps | ||
| ): SearchBarData { | ||
| const {pageFilters, widgetQuery} = props; | ||
| const hasTraceMetricsDashboards = useHasTraceMetricsDashboards(); | ||
|
|
||
| const traceItemAttributeConfig = { | ||
| traceItemType: TraceItemDataset.TRACEMETRICS, | ||
| enabled: hasTraceMetricsDashboards, | ||
| }; | ||
|
|
||
| const {attributes: stringAttributes, secondaryAliases: stringSecondaryAliases} = | ||
| useTraceItemAttributesWithConfig(traceItemAttributeConfig, 'string'); | ||
| const {attributes: numberAttributes, secondaryAliases: numberSecondaryAliases} = | ||
| useTraceItemAttributesWithConfig(traceItemAttributeConfig, 'number'); | ||
|
|
||
| const {filterKeys, filterKeySections, getTagValues} = useSearchQueryBuilderProps({ | ||
| itemType: TraceItemDataset.TRACEMETRICS, | ||
| numberAttributes, | ||
| stringAttributes, | ||
| numberSecondaryAliases, | ||
| stringSecondaryAliases, | ||
| searchSource: 'dashboards', | ||
| initialQuery: widgetQuery?.conditions ?? '', | ||
| projects: pageFilters.projects, | ||
| }); | ||
|
|
||
| return { | ||
| getFilterKeySections: () => filterKeySections, | ||
| getFilterKeys: () => filterKeys, | ||
| getTagValues, | ||
| }; | ||
| } | ||
|
|
||
| function prettifySortOption(option: SelectValue<string>) { | ||
| const parsedFunction = parseFunction(option.value); | ||
| if (parsedFunction) { | ||
| return `${parsedFunction.name}(${parsedFunction.arguments[1] ?? '…'})`; | ||
| } | ||
| return option.label; | ||
| } | ||
|
|
||
| export const TraceMetricsConfig: DatasetConfig<EventsTimeSeriesResponse, never> = { | ||
| defaultField: DEFAULT_FIELD, | ||
| defaultWidgetQuery: DEFAULT_WIDGET_QUERY, | ||
| enableEquations: false, | ||
| SearchBar: TraceMetricsSearchBar, | ||
| useSearchBarDataProvider: useTraceMetricsSearchBarDataProvider, | ||
| filterSeriesSortOptions, | ||
| getTableFieldOptions: getPrimaryFieldOptions, | ||
| getTimeseriesSortOptions: (organization, widgetQuery, tags) => | ||
| getTimeseriesSortOptions(organization, widgetQuery, tags, getPrimaryFieldOptions), | ||
| // We've forced the sort options to use the table sort options UI because | ||
| // we only want to allow sorting by selected aggregates. | ||
| getTableSortOptions: (organization, widgetQuery) => | ||
| getTableSortOptions(organization, widgetQuery).map(option => ({ | ||
| label: prettifySortOption(option), | ||
| value: option.value, | ||
| })), | ||
| getGroupByFieldOptions, | ||
| supportedDisplayTypes: [DisplayType.AREA, DisplayType.BAR, DisplayType.LINE], | ||
| getSeriesRequest, | ||
| transformTable: () => ({data: []}), | ||
| transformSeries: (data, _widgetQuery) => | ||
| data.timeSeries.map(timeSeries => { | ||
| const func = parseFunction(timeSeries.yAxis); | ||
| if (func) { | ||
| timeSeries.yAxis = `${func.name}(${func.arguments[1] ?? '…'})`; | ||
| } | ||
| return { | ||
| data: timeSeries.values.map(value => ({ | ||
| name: value.timestamp / 1000, // Account for microseconds to milliseconds precision | ||
| value: value.value ?? 0, | ||
| })), | ||
| seriesName: formatTimeSeriesLabel(timeSeries), | ||
| }; | ||
| }), | ||
| }; | ||
|
|
||
| function getPrimaryFieldOptions( | ||
| organization: Organization, | ||
| tags?: TagCollection, | ||
| _customMeasurements?: CustomMeasurementCollection | ||
| ): Record<string, FieldValueOption> { | ||
| return combineBaseFieldsWithTags(organization, tags, {}); | ||
| } | ||
|
|
||
| function filterYAxisOptions() { | ||
| return function (option: FieldValueOption) { | ||
| return option.value.kind === FieldValueKind.FUNCTION; | ||
| }; | ||
| } | ||
|
|
||
| function getGroupByFieldOptions( | ||
| organization: Organization, | ||
| tags?: TagCollection, | ||
| customMeasurements?: CustomMeasurementCollection | ||
| ) { | ||
| const primaryFieldOptions = getPrimaryFieldOptions( | ||
| organization, | ||
| tags, | ||
| customMeasurements | ||
| ); | ||
| const yAxisFilter = filterYAxisOptions(); | ||
| const filterGroupByOptions = (option: FieldValueOption) => !yAxisFilter(option); | ||
|
|
||
| return pickBy(primaryFieldOptions, filterGroupByOptions); | ||
| } | ||
|
|
||
| function getSeriesRequest( | ||
| api: Client, | ||
| widget: Widget, | ||
| queryIndex: number, | ||
| organization: Organization, | ||
| pageFilters: PageFilters, | ||
| _onDemandControlContext?: OnDemandControlContext, | ||
| referrer?: string, | ||
| _mepSetting?: MEPState | null, | ||
| samplingMode?: SamplingMode | ||
| ) { | ||
| const requestData = getSeriesRequestData( | ||
| widget, | ||
| queryIndex, | ||
| organization, | ||
| pageFilters, | ||
| DiscoverDatasets.TRACEMETRICS, | ||
| referrer | ||
| ); | ||
|
|
||
| requestData.generatePathname = () => | ||
| `/organizations/${organization.slug}/events-timeseries/`; | ||
|
|
||
| if ( | ||
| [DisplayType.LINE, DisplayType.AREA, DisplayType.BAR].includes(widget.displayType) && | ||
| (widget.queries[0]?.columns?.length ?? 0) > 0 | ||
| ) { | ||
| requestData.queryExtras = { | ||
| ...requestData.queryExtras, | ||
| groupBy: widget.queries[0]!.columns, | ||
| }; | ||
| } | ||
|
|
||
| if (samplingMode) { | ||
| requestData.sampling = samplingMode; | ||
| } | ||
|
|
||
| return doEventsRequest<true>(api, requestData) as unknown as Promise< | ||
| ApiResult<EventsTimeSeriesResponse> | ||
| >; | ||
| } | ||
|
|
||
| function filterSeriesSortOptions(columns: Set<string>) { | ||
| return (option: FieldValueOption) => { | ||
| if (option.value.kind === FieldValueKind.FUNCTION) { | ||
| return true; | ||
| } | ||
|
|
||
| return columns.has(option.value.meta.name); | ||
| }; | ||
| } | ||
Oops, something went wrong.
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.