-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
feat(tracemetrics): Add alert via dropdown in explore #112963
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
k-fish
merged 3 commits into
master
from
feat/tracemetrics/allow-adding-alerts-from-explore
Apr 15, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,11 +7,15 @@ import { | |
| addSuccessMessage, | ||
| } from 'sentry/actionCreators/indicator'; | ||
| import {openSaveQueryModal} from 'sentry/actionCreators/modal'; | ||
| import {usePageFilters} from 'sentry/components/pageFilters/usePageFilters'; | ||
| import {t} from 'sentry/locale'; | ||
| import {defined} from 'sentry/utils'; | ||
| import {trackAnalytics} from 'sentry/utils/analytics'; | ||
| import {parseFunction, prettifyParsedFunction} from 'sentry/utils/discover/fields'; | ||
| import {useLocation} from 'sentry/utils/useLocation'; | ||
| import {useOrganization} from 'sentry/utils/useOrganization'; | ||
| import {useProjects} from 'sentry/utils/useProjects'; | ||
| import {Dataset, EventTypes} from 'sentry/views/alerts/rules/metric/types'; | ||
| import {formatTraceMetricsFunction} from 'sentry/views/dashboards/datasetConfig/traceMetrics'; | ||
| import {getIdFromLocation} from 'sentry/views/explore/contexts/pageParamsContext/id'; | ||
| import {useGetSavedQuery} from 'sentry/views/explore/hooks/useGetSavedQueries'; | ||
|
|
@@ -21,26 +25,35 @@ import {useMultiMetricsQueryParams} from 'sentry/views/explore/metrics/multiMetr | |
| import { | ||
| isVisualize, | ||
| isVisualizeEquation, | ||
| isVisualizeFunction, | ||
| } from 'sentry/views/explore/queryParams/visualize'; | ||
| import {getVisualizeLabel} from 'sentry/views/explore/toolbar/toolbarVisualize'; | ||
| import {TraceItemDataset} from 'sentry/views/explore/types'; | ||
| import {getAlertsUrl} from 'sentry/views/insights/common/utils/getAlertsUrl'; | ||
|
|
||
| import {canUseMetricsSavedQueriesUI} from './metricsFlags'; | ||
| import {canUseMetricsAlertsUI, canUseMetricsSavedQueriesUI} from './metricsFlags'; | ||
|
|
||
| interface UseSaveAsMetricItemsOptions { | ||
| interval: string; | ||
| } | ||
|
|
||
| export function useSaveAsMetricItems(_options: UseSaveAsMetricItemsOptions) { | ||
| export function useSaveAsMetricItems(options: UseSaveAsMetricItemsOptions) { | ||
| const location = useLocation(); | ||
| const organization = useOrganization(); | ||
| const {projects} = useProjects(); | ||
| const pageFilters = usePageFilters(); | ||
| const {saveQuery, updateQuery} = useSaveMetricsMultiQuery(); | ||
| const id = getIdFromLocation(location); | ||
| const {data: savedQuery} = useGetSavedQuery(id); | ||
|
|
||
| const metricQueries = useMultiMetricsQueryParams(); | ||
| const {addToDashboard} = useAddMetricToDashboard(); | ||
|
|
||
| const project = | ||
| projects.length === 1 | ||
| ? projects[0] | ||
| : projects.find(p => p.id === `${pageFilters.selection.projects[0]}`); | ||
|
|
||
| const saveAsItems = useMemo(() => { | ||
| if (!canUseMetricsSavedQueriesUI(organization)) { | ||
| return []; | ||
|
|
@@ -94,7 +107,58 @@ export function useSaveAsMetricItems(_options: UseSaveAsMetricItemsOptions) { | |
| return items; | ||
| }, [id, savedQuery?.isPrebuilt, updateQuery, saveQuery, organization]); | ||
|
|
||
| // TODO: Implement alert functionality when organizations:tracemetrics-alerts flag is enabled | ||
| const saveAsAlertItems = useMemo(() => { | ||
| if (!canUseMetricsAlertsUI(organization)) { | ||
| return []; | ||
| } | ||
|
|
||
| const alertsUrls = metricQueries | ||
| .filter(mq => isVisualizeFunction(mq.queryParams.visualizes[0]!)) | ||
| .map((metricQuery, index) => { | ||
| const visualize = metricQuery.queryParams.visualizes[0]!; | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should prob fix this in another PR to not allow multiple if they are really 1:1 |
||
| const yAxis = isVisualizeFunction(visualize) ? visualize.yAxis : ''; | ||
| const func = parseFunction(yAxis); | ||
| const label = func ? prettifyParsedFunction(func) : yAxis; | ||
| const query = metricQuery.queryParams.query ?? ''; | ||
|
|
||
| return { | ||
| key: `create-alert-${index}`, | ||
| label, | ||
| to: getAlertsUrl({ | ||
| project, | ||
| query, | ||
| pageFilters: pageFilters.selection, | ||
| aggregate: yAxis, | ||
| organization, | ||
| dataset: Dataset.EVENTS_ANALYTICS_PLATFORM, | ||
| interval: options.interval, | ||
| eventTypes: [EventTypes.TRACE_ITEM_METRIC], | ||
| }), | ||
| onAction: () => { | ||
| trackAnalytics('metrics.save_as', { | ||
| save_type: 'alert', | ||
| ui_source: 'searchbar', | ||
| organization, | ||
| }); | ||
| }, | ||
| }; | ||
| }); | ||
|
|
||
| const newAlertLabel = organization.features.includes('workflow-engine-ui') | ||
| ? t('Monitor for') | ||
| : t('Alert for'); | ||
|
|
||
| return [ | ||
| { | ||
| key: 'create-alert', | ||
| label: newAlertLabel, | ||
| textValue: newAlertLabel, | ||
| children: alertsUrls, | ||
| disabled: alertsUrls.length === 0, | ||
| isSubmenu: true, | ||
| }, | ||
| ]; | ||
| }, [metricQueries, organization, project, pageFilters, options.interval]); | ||
|
|
||
| const addToDashboardItems = useMemo(() => { | ||
| return [ | ||
|
|
@@ -137,6 +201,6 @@ export function useSaveAsMetricItems(_options: UseSaveAsMetricItemsOptions) { | |
| }, [addToDashboard, metricQueries]); | ||
|
|
||
| return useMemo(() => { | ||
| return [...saveAsItems, ...addToDashboardItems]; | ||
| }, [saveAsItems, addToDashboardItems]); | ||
| return [...saveAsItems, ...saveAsAlertItems, ...addToDashboardItems]; | ||
| }, [saveAsItems, saveAsAlertItems, addToDashboardItems]); | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: When "All Projects" is selected, the
projectvariable becomesundefined, causing the alert creation URL to lack a pre-selected project, degrading the user experience.Severity: LOW
Suggested Fix
Add an explicit check to handle the "All Projects" case. If
pageFilters.selection.projectsis empty or contains the sentinel value for all projects, do not attempt to find a specific project. Instead, allow the alert creation URL to be generated without a project, or implement logic to handle this case as intended, ensuring the user experience is not degraded.Prompt for AI Agent
Did we get this right? 👍 / 👎 to inform future reviews.