Repository navigation
feat(infra): declare Hazel's production monitoring in Maple #333
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
Merged
Changes from all commits
Commits
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,172 @@ | ||
| /** | ||
| * Hazel's production monitoring in Maple, declared next to the Workers it watches: per-service | ||
| * alert rules and a services dashboard over the telemetry `hazelTelemetry` exports (`./maple.ts`). | ||
| * | ||
| * Alerts post into a Hazel channel through Hazel's own Maple integration: create the channel's | ||
| * Maple webhook in Hazel (Integrations → Maple) and set its URL as `MAPLE_ALERTS_WEBHOOK_URL`. | ||
| * Without it the rules are not declared, since a rule with no destination notifies nobody. | ||
| * | ||
| * Rules are `builder_query`s so they can scope to `deployment.environment = "production"`: preview | ||
| * deploys report under the same service names. | ||
| */ | ||
| import * as Maple from "@maple-dev/alchemy" | ||
| import type * as Output from "alchemy/Output" | ||
| import { Effect, Option } from "effect" | ||
| import { optionalString } from "../config-helpers.ts" | ||
|
|
||
| /** The Workers that export through the Maple SDK (`hazelTelemetry`), by `service.name`. */ | ||
| export const HAZEL_TELEMETRY_SERVICES = ["api", "electric-proxy", "bot-gateway"] as const | ||
|
|
||
| const PRODUCTION = `deployment.environment = "production"` | ||
|
|
||
| type TraceAggregation = "count" | "error_rate" | "p95_duration" | "p99_duration" | ||
|
|
||
| /** A traces query-builder draft, the shape alert rules and dashboard widgets share. */ | ||
| const tracesQuery = (options: { | ||
| readonly where: string | ||
| readonly aggregation: TraceAggregation | ||
| readonly groupBy?: ReadonlyArray<string> | ||
| }) => ({ | ||
| id: "q1", | ||
| name: "A", | ||
| enabled: true, | ||
| hidden: false, | ||
| whereClause: options.where, | ||
| aggregation: options.aggregation, | ||
| stepInterval: "", | ||
| orderByDirection: "desc", | ||
| addOns: { | ||
| groupBy: options.groupBy !== undefined, | ||
| having: false, | ||
| orderBy: false, | ||
| limit: false, | ||
| legend: false, | ||
| }, | ||
| groupBy: options.groupBy ?? [], | ||
| having: "", | ||
| orderBy: "", | ||
| limit: "", | ||
| legend: "", | ||
| dataSource: "traces", | ||
| }) | ||
|
|
||
| /** Root spans of one service in production: request-level signals, not internal calls. */ | ||
| const serviceRequests = (service: string) => | ||
| `service.name = "${service}" AND ${PRODUCTION} AND root_only = "true"` | ||
|
|
||
| const serviceAlerts = (service: string, destinationId: Output.Output<string>) => | ||
| Effect.all([ | ||
| Maple.AlertRule(`${service}-error-rate`, { | ||
| name: `Hazel ${service}: error rate above 5%`, | ||
| severity: "critical", | ||
| signal_type: "builder_query", | ||
| query_builder_draft: tracesQuery({ where: serviceRequests(service), aggregation: "error_rate" }), | ||
| comparator: "gt", | ||
| threshold: 0.05, | ||
| window_minutes: 5, | ||
| minimum_sample_count: 20, | ||
| consecutive_breaches_required: 2, | ||
| destination_ids: [destinationId], | ||
| tags: ["hazel", service], | ||
| }), | ||
| Maple.AlertRule(`${service}-p95-latency`, { | ||
| name: `Hazel ${service}: p95 latency above 2s`, | ||
| severity: "warning", | ||
| signal_type: "builder_query", | ||
| query_builder_draft: tracesQuery({ | ||
| where: serviceRequests(service), | ||
| aggregation: "p95_duration", | ||
| }), | ||
| comparator: "gt", | ||
| threshold: 2000, | ||
| window_minutes: 10, | ||
| minimum_sample_count: 20, | ||
| consecutive_breaches_required: 2, | ||
| destination_ids: [destinationId], | ||
| tags: ["hazel", service], | ||
| }), | ||
| ]) | ||
|
|
||
| const chart = (options: { | ||
| readonly id: string | ||
| readonly title: string | ||
| readonly aggregation: TraceAggregation | ||
| readonly unit: string | ||
| readonly x: number | ||
| readonly y: number | ||
| }) => ({ | ||
| id: options.id, | ||
| visualization: "chart", | ||
| dataSource: { | ||
| kind: "query", | ||
| resultShape: "timeseries", | ||
| queries: [ | ||
| tracesQuery({ | ||
| where: `${PRODUCTION} AND root_only = "true"`, | ||
| aggregation: options.aggregation, | ||
| groupBy: ["service.name"], | ||
| }), | ||
| ], | ||
| }, | ||
| display: { | ||
| title: options.title, | ||
| chartId: "query-builder-line", | ||
| unit: options.unit, | ||
| chartPresentation: { legend: "visible" }, | ||
| }, | ||
| layout: { x: options.x, y: options.y, w: 6, h: 6, minW: 2, minH: 2 }, | ||
| }) | ||
|
|
||
| /** Golden signals of every Hazel service in production, one line per `service.name`. */ | ||
| const servicesDashboard = Maple.Dashboard("services", { | ||
| name: "Hazel services", | ||
| description: | ||
| "Production golden signals of the Hazel Workers (api, electric-proxy, bot-gateway) over root spans. Managed by alchemy.run.ts: edits in the UI are overwritten on deploy.", | ||
| tags: ["hazel"], | ||
| time_range: { type: "relative", value: "12h" }, | ||
| widgets: [ | ||
| chart({ id: "throughput", title: "Requests", aggregation: "count", unit: "number", x: 0, y: 0 }), | ||
| chart({ | ||
| id: "error-rate", | ||
| title: "Error rate", | ||
| aggregation: "error_rate", | ||
| unit: "percent", | ||
| x: 6, | ||
| y: 0, | ||
| }), | ||
| chart({ | ||
| id: "p95", | ||
| title: "p95 latency", | ||
| aggregation: "p95_duration", | ||
| unit: "duration_ms", | ||
| x: 0, | ||
| y: 6, | ||
| }), | ||
| chart({ | ||
| id: "p99", | ||
| title: "p99 latency", | ||
| aggregation: "p99_duration", | ||
| unit: "duration_ms", | ||
| x: 6, | ||
| y: 6, | ||
| }), | ||
| ], | ||
| }) | ||
|
|
||
| /** Declare Hazel's Maple dashboard and, when a Hazel channel webhook is configured, its alerts. */ | ||
| export const HazelMonitoring = Effect.gen(function* () { | ||
| const dashboard = yield* servicesDashboard | ||
| const webhookUrl = yield* optionalString("MAPLE_ALERTS_WEBHOOK_URL") | ||
| if (Option.isNone(webhookUrl)) return { dashboardId: dashboard.dashboardId, alertRules: 0 } | ||
|
|
||
| // Hazel's incoming Maple webhook (`/webhooks/incoming/:id/:token/maple`) renders the alert payload. | ||
| const destination = yield* Maple.AlertDestination("hazel-alerts", { | ||
| type: "webhook", | ||
| name: "Hazel #alerts", | ||
| url: webhookUrl.value.trim(), | ||
| }) | ||
| const rules = yield* Effect.forEach(HAZEL_TELEMETRY_SERVICES, (service) => | ||
| serviceAlerts(service, destination.destinationId), | ||
| ) | ||
| return { dashboardId: dashboard.dashboardId, alertRules: rules.flat().length } | ||
| }) | ||
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.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Limit the dashboard query to the three Hazel services.
If another service sends production root spans to the same Maple organization, this query adds that service to all four charts. The dashboard then shows more than the three services named in its description. Add a
service.namefilter usingHAZEL_TELEMETRY_SERVICES, while retaining the production and root-span filters.🤖 Prompt for AI Agents