Repository navigation
feat(dashboards): chart Google Cloud metrics live with PromQL #1480
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
Open
JeremyFunk
wants to merge
23
commits into
feat/gcp-live-query-access
Choose a base branch
from
feat/gcp-promql-widget
base: feat/gcp-live-query-access
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,265
−70
Open
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
4d61ed6
feat(dashboards): chart Google Cloud metrics live with PromQL
JeremyFunk 9d824ec
Merge remote-tracking branch 'origin/feat/gcp-promql-query' into feat…
JeremyFunk f962626
docs(gcp): the 500-series cap on live queries
JeremyFunk 128dc95
Merge remote-tracking branch 'origin/feat/gcp-live-query-access' into…
JeremyFunk 1849d85
docs(gcp): live queries follow the Key metrics naming
JeremyFunk d84c296
fix(dashboards): review fixes for PromQL widgets
JeremyFunk 36f50ad
Merge remote-tracking branch 'origin/feat/gcp-live-query-access' into…
JeremyFunk 7c45912
perf(web): load the PromQL widget module on first use, not at startup
JeremyFunk 3372d50
feat(dashboards): PromQL starter queries keep each project apart
JeremyFunk 24cd5c7
Merge remote-tracking branch 'origin/feat/gcp-live-query-access' into…
JeremyFunk bad06da
Merge branch 'feat/gcp-live-query-access' into feat/gcp-promql-widget
JeremyFunk d512231
Merge branch 'feat/gcp-live-query-access' into feat/gcp-promql-widget
JeremyFunk bc3e84b
Merge branch 'feat/gcp-live-query-access' into feat/gcp-promql-widget
JeremyFunk 830d90a
Merge branch 'feat/gcp-live-query-access' into feat/gcp-promql-widget
JeremyFunk b4105f3
Merge branch 'feat/gcp-live-query-access' into feat/gcp-promql-widget
JeremyFunk b30cd15
Merge feat/gcp-live-query-access into feat/gcp-promql-widget
JeremyFunk 7a655ed
Merge remote-tracking branch 'origin/feat/gcp-live-query-access' into…
JeremyFunk ba9ef96
Merge remote-tracking branch 'origin/feat/gcp-live-query-access' into…
JeremyFunk a06cada
fix(ci): raise the web bundle budget for #1480
JeremyFunk 75e4b14
fix(ci): 1 KB headroom on the bundle budget; CI measures the PR merge…
JeremyFunk 27385c0
fix(ci): resolve the bundle budget merge
JeremyFunk 65b0807
Merge branch 'feat/gcp-live-query-access' into feat/gcp-promql-widget
JeremyFunk bac2dcc
Merge branch 'feat/gcp-live-query-access' into feat/gcp-promql-widget
JeremyFunk 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
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,67 @@ | ||
| import { Effect, Schema } from "effect" | ||
| import { GcpPromQlRangeRequest } from "@maple/domain/http" | ||
| import { promQlRangeInput, promQlWidgetData } from "@maple/query-engine" | ||
| import { MapleInternalAtomClient } from "@/lib/services/common/internal-atom-client" | ||
| import { | ||
| WarehouseDateTimeString, | ||
| decodeInput, | ||
| invalidWarehouseInput, | ||
| runWarehouseQuery, | ||
| } from "@/api/warehouse/effect-utils" | ||
|
|
||
| // Live Google Cloud Monitoring reads through the API's PromQL proxy (widget data source | ||
| // `gcp_promql`). Nothing comes from the warehouse; the API edge-caches each answer. | ||
|
|
||
| const PromQlChartInput = Schema.Struct({ | ||
| promql: Schema.String, | ||
| projects: Schema.optional(Schema.Array(Schema.String)), | ||
| legend: Schema.optional(Schema.String), | ||
| startTime: WarehouseDateTimeString, | ||
| endTime: WarehouseDateTimeString, | ||
| }) | ||
|
|
||
| /** A PromQL range query as chart rows, plus a one-line notice when some projects could not be read. */ | ||
| export const getGcpPromQlChart = Effect.fn("Integrations.getGcpPromQlChart")(function* ({ | ||
| data, | ||
| }: { | ||
| data: (typeof PromQlChartInput)["Encoded"] | ||
| }) { | ||
| const input = yield* decodeInput(PromQlChartInput, data, "getGcpPromQlChart") | ||
| // A bad project id or an over-long query is the author's to fix, not a support case. | ||
| const payload = yield* Schema.decodeUnknownEffect(GcpPromQlRangeRequest)( | ||
| promQlRangeInput(input, input), | ||
| ).pipe(Effect.catch((error) => invalidWarehouseInput("gcpPromQlRangeRequest", error.message))) | ||
| const result = yield* runWarehouseQuery("gcpPromQlQueryRange", () => | ||
| Effect.gen(function* () { | ||
| const client = yield* MapleInternalAtomClient | ||
| return yield* client.integrations.gcpPromQlQueryRange({ payload }) | ||
| }), | ||
| ) | ||
| return promQlWidgetData(result, input.legend, true) | ||
| }) | ||
|
|
||
| /** Metric names containing `search`, for the PromQL editor's metric picker. */ | ||
| export const getGcpMetricSearch = Effect.fn("Integrations.getGcpMetricSearch")(function* ({ | ||
| data, | ||
| }: { | ||
| data: { readonly search: string } | ||
| }) { | ||
| return yield* runWarehouseQuery("gcpMetricSearch", () => | ||
| Effect.gen(function* () { | ||
| const client = yield* MapleInternalAtomClient | ||
| return yield* client.integrations.gcpMetricSearch({ query: { search: data.search, limit: 50 } }) | ||
| }), | ||
| ) | ||
| }) | ||
|
|
||
| /** What a starter query needs to know about a metric. */ | ||
| export const getGcpMetricDescriptor = Effect.fn("Integrations.getGcpMetricDescriptor")(function* ( | ||
| type: string, | ||
| ) { | ||
| return yield* runWarehouseQuery("gcpMetricDescribe", () => | ||
| Effect.gen(function* () { | ||
| const client = yield* MapleInternalAtomClient | ||
| return yield* client.integrations.gcpMetricDescribe({ query: { type } }) | ||
| }), | ||
| ) | ||
| }) |
207 changes: 207 additions & 0 deletions
207
apps/web/src/components/dashboard-builder/config/promql-editor-panel.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,207 @@ | ||
| import { type Dispatch, type SetStateAction, useDeferredValue, useState } from "react" | ||
| import { Effect, Exit } from "effect" | ||
| import { promQlStarterQuery } from "@maple/query-engine" | ||
| import { Badge } from "@maple/ui/components/ui/badge" | ||
| import { Button } from "@maple/ui/components/ui/button" | ||
| import { | ||
| Combobox, | ||
| ComboboxContent, | ||
| ComboboxInput, | ||
| ComboboxItem, | ||
| ComboboxList, | ||
| } from "@maple/ui/components/ui/combobox" | ||
| import { EmptyMessage } from "@maple/ui/components/ui/empty" | ||
| import { Field, FieldLabel } from "@maple/ui/components/ui/field" | ||
| import { Input } from "@maple/ui/components/ui/input" | ||
| import { TONE_TEXT } from "@maple/ui/lib/tone" | ||
| import { cn } from "@maple/ui/lib/utils" | ||
| import { getGcpMetricDescriptor } from "@/api/warehouse/gcp-promql" | ||
| import { useAsyncAction } from "@/hooks/use-mutation-action" | ||
| import { displayError } from "@/lib/error-messages" | ||
| import { Result, useAtomValue } from "@/lib/effect-atom" | ||
| import { disabledResultAtom } from "@/lib/services/atoms/disabled-result-atom" | ||
| import { gcpMetricSearchResultAtom } from "@/lib/services/atoms/warehouse-query-atoms" | ||
|
|
||
| /** The editor's form. `projects` stays the typed text until it is saved. */ | ||
| export interface PromQlDraft { | ||
| query: string | ||
| projects: string | ||
| legend: string | ||
| } | ||
|
|
||
| /** Project ids from the comma- or space-separated field; none means every project. */ | ||
| export const parsePromQlProjects = (text: string): ReadonlyArray<string> => | ||
| text.split(/[\s,]+/u).filter((projectId) => projectId !== "") | ||
|
|
||
| interface PromQlEditorPanelProps { | ||
| draft: PromQlDraft | ||
| onDraftChange: Dispatch<SetStateAction<PromQlDraft>> | ||
| onRunPreview: () => void | ||
| /** The draft differs from what the preview last ran. */ | ||
| stale: boolean | ||
| /** The preview's failure, e.g. Google's message for a query it refused. */ | ||
| error?: { title: string; message?: string } | ||
| } | ||
|
|
||
| export function PromQlEditorPanel({ | ||
| draft, | ||
| onDraftChange, | ||
| onRunPreview, | ||
| stale, | ||
| error, | ||
| }: PromQlEditorPanelProps) { | ||
| const [search, setSearch] = useState("") | ||
| const deferredSearch = useDeferredValue(search.trim()) | ||
| const searchResult = useAtomValue( | ||
| deferredSearch === "" | ||
| ? disabledResultAtom() | ||
| : gcpMetricSearchResultAtom({ data: { search: deferredSearch } }), | ||
| ) | ||
| const metrics = Result.builder(searchResult) | ||
| .onSuccess((response) => (response as { metrics: ReadonlyArray<string> }).metrics) | ||
| .orElse((): ReadonlyArray<string> => []) | ||
| const searchHint = | ||
| deferredSearch === "" | ||
| ? "Type part of a metric name." | ||
| : Result.builder(searchResult) | ||
| .onError((searchError) => displayError(searchError).message) | ||
| .onSuccess(() => `No metric name contains "${deferredSearch}".`) | ||
| .orElse(() => "Searching...") | ||
|
|
||
| const [starterError, setStarterError] = useState<string | null>(null) | ||
| const [insertStarter, inserting] = useAsyncAction(async (metric: string) => { | ||
| setStarterError(null) | ||
| const exit = await Effect.runPromiseExit(getGcpMetricDescriptor(metric)) | ||
| if (Exit.isFailure(exit)) { | ||
| setStarterError(displayError(exit.cause).message) | ||
| return | ||
| } | ||
| const query = promQlStarterQuery(metric, exit.value) | ||
| onDraftChange((current) => ({ ...current, query })) | ||
| }) | ||
|
|
||
| return ( | ||
| <div className="space-y-3"> | ||
| <div className="border rounded-md"> | ||
| <div className="flex items-center gap-2 px-3 py-2 bg-muted/30"> | ||
| <Badge | ||
| variant="outline" | ||
| className="font-mono text-2xs text-white border-0 shrink-0 bg-primary/80" | ||
| > | ||
| promql | ||
| </Badge> | ||
| <span className="text-2xs text-muted-foreground shrink-0"> | ||
| Google Cloud Monitoring, live | ||
| </span> | ||
| <div className="flex-1" /> | ||
| {error && ( | ||
| <span | ||
| className={cn("text-2xs truncate", TONE_TEXT.crit)} | ||
| title={error.message ?? error.title} | ||
| > | ||
| {error.title} | ||
| </span> | ||
| )} | ||
| </div> | ||
|
|
||
| <div className="p-3 space-y-3"> | ||
| <Field className="gap-1.5"> | ||
| <FieldLabel className="font-normal text-2xs text-muted-foreground"> | ||
| Start from a metric: picking one replaces the query below | ||
| </FieldLabel> | ||
| <Combobox<string | null> | ||
| value={null} | ||
| onValueChange={(metric) => { | ||
| if (typeof metric === "string") void insertStarter(metric) | ||
| }} | ||
| > | ||
| <ComboboxInput | ||
| placeholder={ | ||
| inserting | ||
| ? "Writing a starter query..." | ||
| : "Search metrics, e.g. run.googleapis.com" | ||
| } | ||
| className="h-8 text-xs" | ||
| disabled={inserting} | ||
| onChange={(e) => setSearch(e.target.value)} | ||
| /> | ||
| <ComboboxContent> | ||
| {metrics.length === 0 ? ( | ||
| <EmptyMessage className="px-3 py-4">{searchHint}</EmptyMessage> | ||
| ) : ( | ||
| <ComboboxList> | ||
| {metrics.map((metric) => ( | ||
| <ComboboxItem | ||
| key={metric} | ||
| value={metric} | ||
| className="font-mono text-xs" | ||
| > | ||
| {metric} | ||
| </ComboboxItem> | ||
| ))} | ||
| </ComboboxList> | ||
| )} | ||
| </ComboboxContent> | ||
| </Combobox> | ||
| {starterError !== null && ( | ||
| <span className={cn("text-2xs", TONE_TEXT.crit)}>{starterError}</span> | ||
| )} | ||
| </Field> | ||
|
|
||
| <textarea | ||
| aria-label="PromQL query" | ||
| value={draft.query} | ||
| onChange={(e) => onDraftChange({ ...draft, query: e.target.value })} | ||
| spellCheck={false} | ||
| placeholder={'sum by (service) (rate({"run.googleapis.com/request_count"}[5m]))'} | ||
| className="w-full bg-transparent font-mono text-xs leading-5 border border-border rounded-sm px-2 py-1.5 min-h-[120px] resize-y outline-none focus:ring-1 focus:ring-foreground/20" | ||
| /> | ||
| {error?.message && ( | ||
| <p className="font-mono text-2xs text-muted-foreground whitespace-pre-wrap break-words"> | ||
| {error.message} | ||
| </p> | ||
| )} | ||
|
|
||
| <div className="grid grid-cols-2 gap-3"> | ||
| <Field className="gap-1.5"> | ||
| <FieldLabel className="font-normal text-2xs text-muted-foreground"> | ||
| Projects (empty: every project, up to 20) | ||
| </FieldLabel> | ||
| <Input | ||
| value={draft.projects} | ||
| onChange={(e) => onDraftChange({ ...draft, projects: e.target.value })} | ||
| placeholder="shop-prod, shop-staging" | ||
| className="h-8 font-mono text-xs" | ||
| /> | ||
| </Field> | ||
| <Field className="gap-1.5"> | ||
| <FieldLabel className="font-normal text-2xs text-muted-foreground"> | ||
| Legend (labels as {"{{label}}"}) | ||
| </FieldLabel> | ||
| <Input | ||
| value={draft.legend} | ||
| onChange={(e) => onDraftChange({ ...draft, legend: e.target.value })} | ||
| placeholder="{{service}}" | ||
| className="h-8 font-mono text-xs" | ||
| /> | ||
| </Field> | ||
| </div> | ||
| </div> | ||
| </div> | ||
|
|
||
| <div className="flex items-center gap-3"> | ||
| <Button size="sm" onClick={onRunPreview} disabled={draft.query.trim() === ""}> | ||
| Run preview | ||
| </Button> | ||
| {stale && ( | ||
| <span className={cn("text-2xs", TONE_TEXT.warn)}> | ||
| Edited since the last run: the preview applies on Run preview | ||
| </span> | ||
| )} | ||
| <span className="text-2xs text-muted-foreground ml-auto"> | ||
| Google bills the series a read returns to your project. | ||
| </span> | ||
| </div> | ||
| </div> | ||
| ) | ||
| } |
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.
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.
Warning
promQlQueriesskips sparkline sources, so a variable can escape the guardF1 · Warning · security
The share executes
widget.display?.sparkline?.dataSourcewhen the request asks forsource: "sparkline"(DashboardWidgetDataService.ts:267, and the source is whatever the unauthenticated caller sent atshare.http.ts:349), but this list walkswidget.dataSourcealone, so a variable referenced only by a sparkline's PromQL never enterspromQlNamesandpromQlUnsafestays false (share-variables.ts:150). The viewer's value then needs only to be one of the options the warehouse listed, and a telemetry value containing a quote reaches thepromqlbranch ofinterpolateStringunescaped — plainly, or as every option of an All joined with|— rewriting the author's matcher on a public link. Collect both data sources here.🤖 Prompt to fix with an AI agent