Skip to content
soumajitghPublic

About

Golang-based Watchdog service for OBA REST API servers

Resources

Code of conduct

Contributing

Stars

0 stars

Watchers

0 watching

Forks

 
 

Repository files navigation

Watchdog

Coverage Status

Watchdog is a Go-based service that monitors OneBusAway (OBA) REST API servers. It exposes a comprehensive set of Prometheus metrics for monitoring:

  • GTFS Static and GTFS-RT data integrity
  • Vehicle telemetry
  • Agency and stop coverage
  • Overall operational health See the full list of metrics and interpretation guide here, and the client freshness contract in docs/FRESHNESS.md.

Requirements

  • Go 1.27.2+

Setup

Configuration

Watchdog requires a configuration file (config.json) before running. Even placeholder data is necessary to start the service.

Example config.json

[
  {
    "server_name": "Test Server 1",
    "oba_base_url": "https://test1.example.com",
    "oba_api_key": "test-key-1",
    "gtfs_static_feeds": ["https://gtfs1.example.com"],
    "gtfs_rt_feeds": [{
      "trip_update_url": "https://trip1.example.com",
      "vehicle_position_url": "https://vehicle1.example.com",
      "gtfs_rt_api_key": "api-key-1",
      "gtfs_rt_api_value": "api-value-1"
    }]
  }
]

The top-level agency_id decides what each entry observes: set it to track that agency. OBA API probes, static data, and realtime feed data are scoped to that agency; leave it out to track the server and every agency it serves. See Two observation modes below for both config shapes.

Feed contract and migration

Each entry requires exactly one URL in gtfs_static_feeds and one source in gtfs_rt_feeds. The array field names remain unchanged, so existing single-feed v2 and legacy v1 configurations continue to work. A realtime source may include vehicle-position and trip-update URLs; Watchdog currently monitors vehicle positions.

Both feeds must already use the final IDs served by the OBA backend. A merged static feed may contain several agencies. Watchdog preserves IDs and does not merge sources or combine stops.

Vehicle-position vehicle IDs must be unique across the entire merged realtime feed, including across agencies. If two source agencies each use vehicle ID 101 for different vehicles, the upstream merger must give those vehicles distinct final IDs. A trip-update entity may reference the same vehicle ID as its vehicle-position entity; that reference is not a duplicate vehicle position.

To upgrade an existing multi-feed entry:

  1. Configure an external merger to produce the static and realtime artifacts consumed by your OBA backend.
  2. Replace gtfs_static_feeds with the single merged static URL and gtfs_rt_feeds with the single merged realtime source, including its authentication fields.
  3. Update any configured agency_id to its final ID in the merged feed.

Multiple sources are rejected with a migration error. Watchdog does not keep the first source automatically. Invalid entries follow the existing config-loading policy and are excluded while valid entries continue to load. Check the dropped-server diagnostics and watchdog_configured_servers after upgrading.

Duplicate primary IDs in the static tables Watchdog consumes reject the new static snapshot, retain the last good snapshot, and produce a Sentry event and gtfs_static_feed_failure_info{reason="duplicate_id"}. Realtime duplicate entity IDs or vehicle-position IDs produce a Sentry event and gtfs_rt_feed_duplicate_id=1. Every entity carrying a conflicting ID is excluded, and the remaining entities are published as a successful fetch with gtfs_rt_feed_fetch_success=1. No duplicate winner is selected. Transport or decoding failures still preserve the cached realtime snapshot without processing it as a new observation.

Ways to Provide the Config File

1. Local Configuration (recommended for development)

  • Copy or rename config.json.template → config.json
  • Fill in your server values
  • Run with:
go run ./cmd/watchdog/ --config-file path/to/config.json

Note:

  • ⚠️The file must be named config.json
  • config.json is Git-ignored (to protect secrets)

2. Remote Configuration (recommended for production)

  • Prepare config.json as above
  • Host it publicly (or on a private server)
  • Run with:
go run ./cmd/watchdog/ --config-url http://example.com/config.json

If authentication is required, set:

export CONFIG_AUTH_USER="username"
export CONFIG_AUTH_PASS="password"

Two observation modes: agency vs. server

There are two ways Watchdog can observe an OBA deployment. The choice is made per entry in config.json by whether you set agency_id — every other field (server_name, oba_base_url, oba_api_key, gtfs_static_feeds, gtfs_rt_feeds) is identical in both modes. Setting agency_id selects agency observation mode; agency_name must also be provided.

  • Agency mode — agency_id is set. Watchdog monitors only that one agency.
  • Server mode (recommended) — agency_id is unset. Watchdog monitors the whole server and every agency it currently serves.

Observe a single agency — set agency_id (with its agency_name) on the entry. Watchdog uses that agency for agency-specific OBA API probes and retains only static and realtime data attributed to it:

{
  "server_name": "Agency Example",
  "agency_name": "Test Agency",
  "agency_id": "agency-1",
  "oba_base_url": "https://test1.example.com",
  "oba_api_key": "test-key-1",
  "gtfs_static_feeds": ["https://gtfs1.example.com"],
  "gtfs_rt_feeds": [{
    "trip_update_url": "https://trip1.example.com",
    "vehicle_position_url": "https://vehicle1.example.com",
    "gtfs_rt_api_key": "api-key-1",
    "gtfs_rt_api_value": "api-value-1"
  }]
}

Observe the whole server — omit agency_id from the entry. Watchdog tracks the server and every agency it currently serves:

{
  "server_name": "Server Example",
  "oba_base_url": "https://test2.example.com",
  "oba_api_key": "test-key-2",
  "gtfs_static_feeds": ["https://gtfs2.example.com"],
  "gtfs_rt_feeds": [{
    "trip_update_url": "https://trip2.example.com",
    "vehicle_position_url": "https://vehicle2.example.com",
    "gtfs_rt_api_key": "api-key-2",
    "gtfs_rt_api_value": "api-value-2"
  }]
}
Agency mode Server mode
agency_id set (paired with required agency_name) unset
Scope one agency whole OBA deployment
Agency set comes from the config entry each static feed's agency.txt, filtered by /api/where/metrics.json agency coverage
Typical source hand-written config OBACloud service discovery (one entry per OBA API server)

Agency mode — agency_id is set. Watchdog monitors only that one agency; today's per-agency pipeline runs once per tick for that agency. agency_name is required (paired with agency_id).

Server mode — agency_id is absent. Watchdog probes /api/where/metrics.json every tick to learn which agencies are present in OBA's loaded static coverage, cross-references those agency IDs against the static feeds' agency.txt declarations, and runs the per-agency pipeline for every agency that has BOTH a static bundle AND is reported by OBA.

Multi-agency static feeds are accepted in server mode: one consolidated bundle pointer-shared across the serverKeys of every agency declared in its agency.txt. Agency mode instead stores an owned snapshot containing only the configured agency's routes, referenced services, stops, and parent stations. Feed-to-agency mapping metrics describe the agencies declared in the upstream artifact's agency.txt.

Static coverage signals

These metrics expose static agency coverage and source-feed mapping health:

  • gtfs_static_agency_reported_by_oba{agency_id, agency_name, server_name, server_url} — 1 if the agency has a static bundle AND is in /api/where/metrics.json entry.AgencyIDs in the current scrape; 0 otherwise.
  • gtfs_static_feed_agency_mapping_info{feed_url, agency_id, agency_name, server_name, server_url} — always 1, with one series for each proven source-feed relationship. An explicit multi-agency feed produces one series per declared agency in server mode. Agency mode emits only its configured agency; a sole agency with omitted agency_id is associated with that configured agency as allowed by GTFS.
  • gtfs_static_feed_agency_mapping_failure{feed_url, server_name, server_url, reason} — always 1 when a parsed feed cannot be fully mapped. reason is one of missing_agency, ambiguous_agency, or unknown_agency.

Vehicle attribution in server-mode

In agency mode the RT fetch resolves each vehicle by route or trip and stores only vehicles attributed to the configured agency_id. Foreign or conflicting vehicles are dropped before the metric pass. Vehicles that cannot be resolved are kept when the static feed names only the configured agency, so quality checks can still report them. Until the first static snapshot loads, the realtime feed is stored unfiltered.

In server mode the single upstream-merged realtime feed can carry vehicles from several agencies. Watchdog attributes each vehicle using the route and trip IDs in the static feed, then checks that OBA reports the agency as live. Unknown IDs, conflicting route/trip ownership, and agencies absent from OBA coverage remain unattributed. Duplicate static primary IDs reject the new snapshot before publication.

Backward Compatibility (v1 → v2)

This is a deliberate breaking change to the existing v2 format. Existing v2 entries without server_name become invalid — operators must add the field. The legacy v1 array-of-flat-objects format continues to work: id (int) is still ignored, name is repurposed as server_name, and a v1 entry with agency_id populates agency_name from name so the entry remains agency-scoped.

Watchdog used to accept a flat, single-server config schema. Legacy (v1) configs are still supported: they're silently converted to the current array-based schema (v2) at load time, so upgrading doesn't require changing your config or interrupt monitoring.

When a v1 entry is loaded, Watchdog maps it like this:

  • name → server_name; when agency_id is present, it also populates agency_name
  • gtfs_url → a single-entry gtfs_static_feeds
  • vehicle_position_url / trip_update_url → the matching gtfs_rt_feeds entries
  • gtfs_rt_api_key / gtfs_rt_api_value → the per-feed auth fields
  • id → ignored

One caveat: v1 and v2 fields can't be mixed in the same entry. If an entry contains both schemas (e.g. a legacy gtfs_url alongside gtfs_static_feeds), it's rejected and reported to Sentry. Each entry must use one schema or the other.

Both v1 and v2 configurations accept one static feed and one realtime source per entry. See Feed contract and migration when upgrading a v2 entry that previously listed multiple feeds.

Application Options

  • Fetch Interval → default 30s (--fetch-interval <seconds>)
  • Environment → development (default), staging, production (--env <value>)
  • Port → default 4000 (--port <number>)

⚠️ If running with Docker Compose, Prometheus runs on 9090 and Grafana on 3000. Don’t use those ports.

Environment Variables

  • Sentry DSN
    export SENTRY_DSN="your_sentry_dsn"
  • Config Auth (for remote configs)
    export CONFIG_AUTH_USER="username"
    export CONFIG_AUTH_PASS="password"
  • Investigation database (optional)
    export DATABASE_URL="postgres://user:password@host:5432/watchdog?sslmode=require"

When DATABASE_URL is set, Watchdog connects to PostgreSQL at startup and applies any pending schema migrations. This database will hold durable investigation history (ADR 0001, schema). It is optional: if DATABASE_URL is unset, or the database is unreachable at startup, Watchdog logs it and keeps collecting and exporting Prometheus metrics without it. Docker Compose starts a local PostgreSQL and sets DATABASE_URL for you.

  • Investigation API token (optional)
    export INVESTIGATION_API_TOKEN="$(openssl rand -hex 32)"
    export INVESTIGATION_API_TOKEN_NEXT=""   # set only while rotating

The Investigation API shows which stops, trips and vehicles had problems and when, so every request must send Authorization: Bearer <token>. A missing or wrong token gets 401. Watchdog accepts INVESTIGATION_API_TOKEN and, when set, INVESTIGATION_API_TOKEN_NEXT. When INVESTIGATION_API_TOKEN is unset, the API is disabled: its routes return 404 and Watchdog logs that at startup. Setting only INVESTIGATION_API_TOKEN_NEXT also leaves the API disabled and logs an error. Watchdog never logs the tokens.

To rotate the token without downtime:

  1. Set INVESTIGATION_API_TOKEN_NEXT to the new token and restart Watchdog. Both tokens now work.
  2. Update the client to send the new token.
  3. Move the new token to INVESTIGATION_API_TOKEN, clear INVESTIGATION_API_TOKEN_NEXT, and restart Watchdog. The old token stops working.

Running

It may take a few minutes for Watchdog to start exposing data to Prometheus, since initial setup includes tasks such as downloading the GTFS bundle.

1. Docker Compose (recommended)

Run Watchdog with Prometheus + Grafana:

docker compose up --build

Services:

  • Watchdog → 4000
  • Prometheus → 9090
  • Grafana → 3000

Stop services:

docker compose down

Restart services:

docker compose restart

Grafana auto-loads a Go runtime dashboard. Prometheus is pre-configured to scrape Watchdog.

See Endpoints to access metrics, health checks, Grafana, and Prometheus.

2. Watchdog Only

Local Config

go run ./cmd/watchdog/ --config-file path/to/config.json

Remote Config (with auth)

go run ./cmd/watchdog/ \
  --config-url http://example.com/config.json

See Endpoints to access metrics and health checks.

3. Docker (single container)

Build image

docker build -t watchdog .

Run with local config

docker run -d \
  --name watchdog \
  -v ./config.json:/app/config.json \
  -p 4000:4000 \
  watchdog \
  --config-file /app/config.json

Run with remote config

docker run -d \
  --name watchdog \
  -e CONFIG_AUTH_USER=admin \
  -e CONFIG_AUTH_PASS=password \
  -p 4000:4000 \
  watchdog \
  --config-url http://example.com/config.json

See Endpoints to access metrics and health checks.

Endpoints

During development (using localhost):

During production (replace <server-ip-or-domain>):

  • Watchdog Metrics: http://<server-ip-or-domain>:4000/metrics
  • Watchdog Health Check: http://<server-ip-or-domain>:4000/v1/healthcheck
  • Grafana: http://<server-ip-or-domain>:3000/login
  • Prometheus Targets: http://<server-ip-or-domain>:9090/targets
  • Prometheus Query: http://<server-ip-or-domain>:9090/query

Testing

Unit Tests

go test ./...

Database Tests

The tests in internal/db apply the migrations and check the schema's constraints against a real PostgreSQL. They skip unless WATCHDOG_TEST_DATABASE_URL is set; each test creates and drops its own schema in that database.

WATCHDOG_TEST_DATABASE_URL="postgres://watchdog:watchdog@localhost:5432/watchdog?sslmode=disable" make test-db

After changing a query in internal/db/queries or adding a migration, regenerate the typed Go code with make sqlc.

Integration Tests

  • Copy integration_config.json.template → integration_config.json
  • Fill in OBA server values
  • Run:
go test -tags=integration ./internal/integration \
  -integration-config path/to/integration_config.json

Note:

  • ⚠️ the file must be named integration_config.json
  • It’s Git-ignored for safety

Contributing

Refer to CONTRIBUTING.md for detailed contribution guidelines.

About

Golang-based Watchdog service for OBA REST API servers

Resources

Code of conduct

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages