{"id":39075480,"url":"https://github.com/cloud-gov/billing","last_synced_at":"2026-01-17T18:26:22.451Z","repository":{"id":286642068,"uuid":"962008115","full_name":"cloud-gov/billing","owner":"cloud-gov","description":null,"archived":false,"fork":false,"pushed_at":"2025-12-31T19:40:32.000Z","size":487,"stargazers_count":2,"open_issues_count":6,"forks_count":1,"subscribers_count":5,"default_branch":"main","last_synced_at":"2026-01-02T04:27:32.984Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cloud-gov.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":"CODEOWNERS","security":"SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-04-07T14:04:03.000Z","updated_at":"2025-11-20T17:15:35.000Z","dependencies_parsed_at":null,"dependency_job_id":"302ebb93-79db-499d-bff2-d3307b30561d","html_url":"https://github.com/cloud-gov/billing","commit_stats":null,"previous_names":["cloud-gov/billing"],"tags_count":0,"template":false,"template_full_name":"cloud-gov/.github","purl":"pkg:github/cloud-gov/billing","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cloud-gov%2Fbilling","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cloud-gov%2Fbilling/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cloud-gov%2Fbilling/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cloud-gov%2Fbilling/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cloud-gov","download_url":"https://codeload.github.com/cloud-gov/billing/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cloud-gov%2Fbilling/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28515468,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-17T17:57:59.192Z","status":"ssl_error","status_checked_at":"2026-01-17T17:57:52.527Z","response_time":85,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2026-01-17T18:26:21.691Z","updated_at":"2026-01-17T18:26:22.428Z","avatar_url":"https://github.com/cloud-gov.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Billing service\n\nThe billing service tracks customer usage of Cloud.gov products for billing purposes.\n\n## Development\n\nPrerequisite: Docker must be installed and running.\n\nCopy `docker.env.example` to `docker.env` and fill in any missing values.\n\nSet up the database:\n\n```sh\nmake migrate\n```\n\nMost development tasks can be achieved using the Make targets defined in [Makefile](./Makefile). See the Makefile for the full list.\n\n```sh\nmake watchgen # Watch .sql files for changes. On change, regenerate database Go bindings with sqlc. Consider running this in a separate shell at the same time as 'make watch'.\nmake watch # Watch .go files for changes. On change, recompile and restart the server.\nmake clean # Shut down the database if it is running and clean binary artifacts.\nmake psql # Connect to the local database.\n```\n\nTo run the tests:\n\n```sh\nmake test # Run unit tests.\nmake test-db # Run database tests.\nmake psql-testdb # Connect to the test database.\n```\n\nMake request to the locally running server:\n\n```sh\n# Requires `cf-uaac` and will attempt to install if missing.\nmake jwt # Get a token from the configured UAA, based on OIDC_ISSUER host. Requires CF_CLIENT_ID and CF_CLIENT_SECRET to be set.\ncurl -H \"Authorization: bearer $(cat jwt.txt)\" localhost:8080/some/path # Make a request with the authentication header set.\n```\n\n### Cloud Foundry\n\nThe application uses service account credentials to authenticate to the Cloud Foundry API (CAPI). Set `CF_CLIENT_ID` and `CF_CLIENT_SECRET` using credentials from CredHub before starting the application with `make watch`.\n\n### Database\n\nNote that table name `schema_version` is reserved by tern for tracking migration status.\n\nConnect to the database in a CF environment with [cf-service-connect](https://github.com/cloud-gov/cf-service-connect):\n\n```\ncf connect-to-service billing billing-db\n```\n\n### Dependencies\n\nWhy does this project use the packages that it does?\n\n- `jackc/pgx` is used because sqlc supports it, and unlike `database/sql` it supports COPY and the postgres binary protocol, which is faster than the SQL textual protocol.\n- `jackc/tern` is used for migrations because we already use another package by `jackc`, `pgx`, and sqlc supports it.\n- `coreos/go-oidc` is used for JWT validation over alternatives because it is maintained by a reputable organization (CoreOS) and supports JWKS discovery.\n\n### Environment variables\n\n- Postgres and AWS use their conventional environment variables for configuration. See [Postgres docs](https://www.postgresql.org/docs/current/libpq-envars.html) and [AWS docs](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-envvars.html).\n\n### River Queue\n\nBilling service uses [River](https://riverqueue.com/docs) for transactional job queuing.\n\nTips:\n\n- If you have to pass dependencies to a River worker, create a `NewXYZWorker` function that accepts those dependencies and sets them as private package vars. Do not pass dependencies as job args.\n  - River job args are serialized to JSON, stored in the database, and deserialized to be run; dependencies like API clients and loggers may not fully serialize their internal state, resulting in nil pointer panics when they are unmarshalled and used.\n  - Additionally, dependencies may have sensitive internal information that should not be persisted to the database.\n\n### Testing\n\nTests follow these naming conventions:\n\n- `TestDB*`: Database tests. Run with `make test-db`, which starts a fresh postgres Docker instance and migrates it to the latest migration.\n\n## Packages\n\nThe program has the following structure:\n\n```\nsql/          # Source SQL for sqlc to convert into Go.\n  init/       # Database creation must be separate from migrations so Tern has something to connect to.\n  migrations/ # Schema for billing service tables.\n  queries/    # Queries for billing service tables.\ninternal/     # All non-exported Go code for the service.\n  api/        # The HTTP API surface of the application.\n  db/         # Destination directory for sqlc-generated Go code.\n  jobs/       # River jobs for performing asynchronous work.\n  server/     # Code for managing the web server lifecycle.\n  usage/      # Code for reading usage information from Cloud.gov systems.\ngen.go        # Program-scope go:generate directives.\nmain.go       # Entrypoint for the server program.\n```\n\n## Design Notes\n\n### Collecting data\n\nUsage data is always persisted to the database, even if partial. The schema is informed by this need. For example, when we take a measurement for a `resource` but do not have a corresponding `resource_kind` in the database, we create an empty `resource_kind` record and will later ask the billing team to fill in the details. Our goal is to never lose usage data.\n\n### Time\n\nFor business operations like posting usage to customer accounts, use the timezone for `America/New_York`. This aligns with other Cloud.gov business processes; for example, Cloud.gov agreements are considered to execute in Eastern Time.\n\nFor all other operations, use UTC. For instance, when a usage reading is taken, the timestamp is captured in UTC.\n\n## Known Limitations\n\n- If we miss a reading, the customer is not charged for that hour. We could fix this by interpolating usage based on measurements taken before and after the gap.\n- Prices could be set differently to avoid rounding. Our pricing table prices many resources per month of usage: X credits per 730 hours of use, a normalized month. However, we measure usage hourly. To calculate the credits for a measurement, we solve `measurement.value * price.amount_microcredits / price.unit`. Dividing by unit can force us to round: For instance, `1,000,000 microcredits/month / 730 hours = 1,369.8630136986 microcredits`, which must be rounded. We could avoid this by pricing resources in the same unit we measure: Per hour. Then, we would calculate credits for a measurement with `measurement.value * price.amount_microcredits`, avoiding division.\n\n## References\n\n- [Go.dev: Organizing a Go module](https://go.dev/doc/modules/layout) explains the basic, conventional layout of Go programs.\n- [Grafana: How I write HTTP services in Go after 13 years](https://grafana.com/blog/2024/02/09/how-i-write-http-services-in-go-after-13-years) is a source for much of the basic structure of the service.\n- [Go Wiki: Go Code Review Comments](https://go.dev/wiki/CodeReviewComments)\n- [The Go Blog: Package Names](https://go.dev/blog/package-names)\n- [brandur.org: How We Went All In on sqlc/pgx for Postgres + Go](https://brandur.org/sqlc#caveats)\n- [An Elegant DB Schema for Double-Entry Accounting](https://web.archive.org/web/20220901165809/https://www.journalize.io/blog/an-elegant-db-schema-for-double-entry-accounting) is the basis for the double-entry accounting schema. (Our schema has some differences.)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcloud-gov%2Fbilling","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcloud-gov%2Fbilling","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcloud-gov%2Fbilling/lists"}