The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | --- |
| 2 | title: Logging & Monitoring |
| 3 | description: Declare uptime checks, alert policies, notification channels, and logs-based metrics in the same Stack as the services they watch. |
| 4 | --- |
| 5 | |
| 6 | Cloud Run sends everything your service logs to Cloud Logging, with no |
| 7 | setup. Declare the checks and alerts that watch it next to the service |
| 8 | itself. |
| 9 | |
| 10 | ## Log from your code |
| 11 | |
| 12 | `Effect.log` goes to Cloud Logging: |
| 13 | |
| 14 | ```typescript |
| 15 | yield* Effect.log(`order ${id} confirmed`); |
| 16 | yield* Effect.logWarning(`retrying ${id}`); |
| 17 | ``` |
| 18 | |
| 19 | ## Check that the service is up |
| 20 | |
| 21 | An uptime check probes a URL from several regions: |
| 22 | |
| 23 | ```typescript |
| 24 | Effect.gen(function* () { |
| 25 | const api = yield* Api; |
| 26 | |
| 27 | const check = yield* GCP.Monitoring.UptimeCheckConfig("ApiUp", { |
| 28 | httpCheck: { path: "/", useSsl: true, validateSsl: true }, |
| 29 | monitoredResource: { |
| 30 | type: "uptime_url", |
| 31 | labels: { host: Output.map(api.uri, (uri) => new URL(uri!).host) }, |
| 32 | }, |
| 33 | period: "300s", |
| 34 | }); |
| 35 | }), |
| 36 | ``` |
| 37 | |
| 38 | ## Get notified |
| 39 | |
| 40 | Create a channel to send alerts to: |
| 41 | |
| 42 | ```typescript |
| 43 | const oncall = yield* GCP.Monitoring.NotificationChannel("Oncall", { |
| 44 | type: "email", |
| 45 | labels: { email_address: "oncall@example.com" }, |
| 46 | }); |
| 47 | ``` |
| 48 | |
| 49 | ## Alert on a metric |
| 50 | |
| 51 | An alert policy opens an incident when a condition holds. This one |
| 52 | fires when the service answers with 5xx errors: |
| 53 | |
| 54 | ```typescript |
| 55 | yield* GCP.Monitoring.AlertPolicy("ApiErrors", { |
| 56 | combiner: "OR", |
| 57 | conditions: [ |
| 58 | { |
| 59 | displayName: "5xx responses", |
| 60 | conditionThreshold: { |
| 61 | filter: |
| 62 | 'resource.type = "cloud_run_revision" AND metric.type = "run.googleapis.com/request_count" AND metric.labels.response_code_class = "5xx"', |
| 63 | comparison: "COMPARISON_GT", |
| 64 | thresholdValue: 5, |
| 65 | duration: "60s", |
| 66 | aggregations: [{ alignmentPeriod: "60s", perSeriesAligner: "ALIGN_RATE" }], |
| 67 | }, |
| 68 | }, |
| 69 | ], |
| 70 | notificationChannels: [oncall.name], |
| 71 | }); |
| 72 | ``` |
| 73 | |
| 74 | ## Count log lines |
| 75 | |
| 76 | A logs-based metric turns matching log entries into a metric you can |
| 77 | chart and alert on: |
| 78 | |
| 79 | ```typescript |
| 80 | const errors = yield* GCP.Logging.Metric("Errors", { |
| 81 | filter: 'resource.type = "cloud_run_revision" AND severity >= ERROR', |
| 82 | description: "error log entries", |
| 83 | }); |
| 84 | ``` |
| 85 | |
| 86 | ## Route logs elsewhere |
| 87 | |
| 88 | A sink copies matching entries to another destination, such as a log |
| 89 | bucket or BigQuery: |
| 90 | |
| 91 | ```typescript |
| 92 | yield* GCP.Logging.Sink("ErrorArchive", { |
| 93 | destination: |
| 94 | "logging.googleapis.com/projects/my-project/locations/global/buckets/_Default", |
| 95 | filter: "severity >= ERROR", |
| 96 | }); |
| 97 | ``` |
| 98 | |
| 99 | ## Where next |
| 100 | |
| 101 | - [Observability](/testing/observability) — tracing Effect programs. |
| 102 | - [Monitoring reference](/providers/gcp/monitoring#alertpolicy) and |
| 103 | [Logging reference](/providers/gcp/logging#metric) — every prop. |