Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion apps/alerting/src/scheduled.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { buildLayer, catchTickFailure, selectScheduledProgram, type ScheduledTic

const cronCases = [
["*/5 * * * *", ["anomaly", "cloudflareAnalytics", "planetScale"]],
["*/15 * * * *", ["digest"]],
["*/15 * * * *", ["digest", "googleAnalytics"]],
["0 * * * *", ["serviceMapRollup"]],
["* * * * *", ["alert", "error", "escalation", "fixVerification"]],
] as const
Expand All @@ -27,6 +27,7 @@ describe("alerting Effect root", () => {
error: tick("error"),
escalation: tick("escalation"),
fixVerification: tick("fixVerification"),
googleAnalytics: tick("googleAnalytics"),
planetScale: tick("planetScale"),
serviceMapRollup: tick("serviceMapRollup"),
} satisfies ScheduledTickPrograms
Expand Down Expand Up @@ -56,6 +57,7 @@ describe("alerting Effect root", () => {
error: errorGate.await.pipe(Effect.andThen(record("error"))),
escalation: record("escalation"),
fixVerification: record("fixVerification"),
googleAnalytics: record("googleAnalytics"),
planetScale: record("planetScale"),
serviceMapRollup: record("serviceMapRollup"),
} satisfies ScheduledTickPrograms
Expand All @@ -81,6 +83,7 @@ describe("alerting Effect root", () => {
error: tick("error"),
escalation: tick("escalation"),
fixVerification: tick("fixVerification"),
googleAnalytics: tick("googleAnalytics"),
planetScale: tick("planetScale"),
serviceMapRollup: tick("serviceMapRollup"),
} satisfies ScheduledTickPrograms
Expand Down
31 changes: 30 additions & 1 deletion apps/alerting/src/scheduled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import {
ErrorsService,
EscalationService,
FixVerificationTickService,
GoogleAnalyticsOAuthService,
GoogleAnalyticsService,
HazelOAuthService,
layerPg,
NotificationDispatcher,
Expand Down Expand Up @@ -236,6 +238,12 @@ export const buildLayer = (env: AlertingWorkerEnv) => {
),
)

const GoogleAnalyticsOAuthServiceLive = GoogleAnalyticsOAuthService.layer.pipe(Layer.provide(BaseLive))

const GoogleAnalyticsServiceLive = GoogleAnalyticsService.layer.pipe(
Layer.provide(Layer.mergeAll(BaseLive, GoogleAnalyticsOAuthServiceLive, OrgIngestKeysServiceLive)),
)

const PlanetScaleOAuthServiceLive = PlanetScaleOAuthService.layer.pipe(Layer.provide(BaseLive))

const PlanetScaleServiceLive = PlanetScaleService.layer.pipe(
Expand All @@ -246,6 +254,7 @@ export const buildLayer = (env: AlertingWorkerEnv) => {
AlertsServiceLive,
AnomalyDetectionServiceLive,
CloudflareAnalyticsServiceLive,
GoogleAnalyticsServiceLive,
PlanetScaleServiceLive,
DigestServiceLive,
ErrorsServiceLive,
Expand Down Expand Up @@ -420,6 +429,21 @@ const cloudflareAnalyticsTick = makeTick(
}),
)

/**
* Runs on the 15-minute cron, not Cloudflare's 5-minute one. GA4 does not update fast enough to
* reward a tighter cadence, and every tick spends Data API quota tokens per property.
*/
const googleAnalyticsTick = makeTick(
GoogleAnalyticsService.use((analytics) => analytics.pollAllOrgs()),
"google_analytics",
(result) => ({
properties: result.properties,
rowsIngested: result.rowsIngested,
skipped: result.skipped,
failures: result.failures,
}),
)

const planetScaleTick = makeTick(
PlanetScaleService.use((planetscale) => planetscale.pollAllOrgs()),
"planetscale",
Expand All @@ -443,6 +467,7 @@ export interface ScheduledTickPrograms<R = never> {
readonly error: Effect.Effect<void, never, R>
readonly escalation: Effect.Effect<void, never, R>
readonly fixVerification: Effect.Effect<void, never, R>
readonly googleAnalytics: Effect.Effect<void, never, R>
readonly planetScale: Effect.Effect<void, never, R>
readonly serviceMapRollup: Effect.Effect<void, never, R>
}
Expand All @@ -463,7 +488,9 @@ export const selectScheduledProgram = <R>(
discard: true,
}),
),
Match.when("*/15 * * * *", () => ticks.digest),
Match.when("*/15 * * * *", () =>
Effect.all([ticks.digest, ticks.googleAnalytics], { concurrency: 2, discard: true }),
),
Match.when("0 * * * *", () => ticks.serviceMapRollup),
Match.when("* * * * *", () =>
// `fixVerification` is chained onto `error` rather than listed beside it:
Expand Down Expand Up @@ -493,6 +520,7 @@ type ScheduledServices =
| ErrorsService
| EscalationService
| FixVerificationTickService
| GoogleAnalyticsService
| PlanetScaleService
| ServiceMapRollupService

Expand All @@ -504,6 +532,7 @@ export const scheduledTicks: ScheduledTickPrograms<ScheduledServices> = {
error: errorTick,
escalation: escalationTick,
fixVerification: fixVerificationTick,
googleAnalytics: googleAnalyticsTick,
planetScale: planetScaleTick,
serviceMapRollup: serviceMapRollupTick,
}
Expand Down
8 changes: 5 additions & 3 deletions apps/alerting/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
appUrlsEnv,
authEnv,
cloudflareOAuthEnv,
googleAnalyticsOAuthEnv,
ingestKeyCryptoEnv,
merge,
optionalPlain,
Expand Down Expand Up @@ -105,12 +106,13 @@ const configuredEnv = (stage: MapleStage) =>
optionalSecret("AUTUMN_SECRET_KEY"),
optionalSecret("INTERNAL_SERVICE_TOKEN"),
// The alerting worker is where incidents open and resolve, so it is the one
// that sends push (platform/Apns.ts) — and it runs the Cloudflare analytics
// and PlanetScale inventory pollers, each of which resolves and refreshes
// per-org OAuth tokens with the same config the api worker uses.
// that sends push (platform/Apns.ts) — and it runs the Cloudflare analytics,
// PlanetScale inventory and Google Analytics pollers, each of which resolves
// and refreshes per-org OAuth tokens with the same config the api worker uses.
apnsEnv,
cloudflareOAuthEnv,
planetScaleOAuthEnv,
googleAnalyticsOAuthEnv,
)

/**
Expand Down
2 changes: 2 additions & 0 deletions apps/api/src/alerting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export { BucketCacheService } from "@maple/query-engine/caching"
export { CacheBackendLive } from "@/platform/CacheBackendLive"
export { CloudflareAnalyticsService } from "./services/integrations/CloudflareAnalyticsService"
export { CloudflareOAuthService } from "./services/auth/CloudflareOAuthService"
export { GoogleAnalyticsService } from "./services/integrations/GoogleAnalyticsService"
export { GoogleAnalyticsOAuthService } from "./services/auth/GoogleAnalyticsOAuthService"
export { ErrorActorsService } from "./services/errors/ErrorActorsService"
export { ErrorIssueReadModelsService } from "./services/errors/ErrorIssueReadModelsService"
export { ErrorIssueWorkflowService } from "./services/errors/ErrorIssueWorkflowService"
Expand Down
3 changes: 3 additions & 0 deletions apps/api/src/dashboard-templates/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { kubernetesPodTemplate } from "./infrastructure/kubernetes-pod"
import { kafkaTemplate } from "./messaging/kafka"
import { natsTemplate } from "./messaging/nats"
import { rabbitmqTemplate } from "./messaging/rabbitmq"
import { googleAnalyticsTemplate } from "./product/google-analytics"
import type { TemplateDefinition, TemplateMetadata, TemplatePreviewWidget } from "./types"

export const DASHBOARD_TEMPLATES: ReadonlyArray<TemplateDefinition> = [
Expand Down Expand Up @@ -53,6 +54,8 @@ export const DASHBOARD_TEMPLATES: ReadonlyArray<TemplateDefinition> = [
kafkaTemplate,
natsTemplate,
rabbitmqTemplate,
// Product
googleAnalyticsTemplate,
]

const TEMPLATE_BY_ID = new Map<string, TemplateDefinition>(DASHBOARD_TEMPLATES.map((t) => [t.id, t]))
Expand Down
Loading
Loading