Billing API for generating invoice data from usage
- Go 80.9%
- Go Template 11.3%
- Dockerfile 7.8%
| Filename | Latest commit message | Latest commit date |
|---|---|---|
| .forgejo/workflows | ||
| billing-api | ||
| charts/billing-api | ||
| LICENSE | ||
| README.md | ||
Billing API
Tools to generate Invoices based on usage.
Idea:
This application should be able to programmatically measure resource usage (CPU/Memory/Storage) via Prometheus & Loki on a per-tenant basis and generate the appropriate InvoiceNinja invoices.
Needed endpoints:
- List clients
- Get client by name
- Create new client
- Generate Invoice
- Get start + end time for service/resource (Loki K8s event log)
- Get usage over period for metric
- Generic LogQL Query
- Push generated cost data back to Promtheus/Loki
Components
- http-swagger: generates testable API docs
- Chi: FastAPI-like framework for golang
- Yq: Query JSON/YAML data without needing structs.
- Go-Invoice-Ninja: Interact with Invoice-Ninja using Go.
- Prometheus/client_golang: Query usage metrics from the observability stack
Endpoints:
- Docs:
http://localhost:3000/swagger - Metrics:
http://localhost:3000/metrics - Ready:
http://localhost:3000/ready
Docker
kubectl port-forward svc/mimir-gateway 8080:80 \
-n monitoring \
--address 192.168.1.201
kubectl port-forward svc/loki-query-frontend 3100:3100 \
-n monitoring \
--address 192.168.1.201
kubectl port-forward svc/smol-invoiceninja-nginx 8888:80 \
-n default \
--address 192.168.1.201
docker build . -f Dockerfile -t billing-api
docker run -it -p 3000:3000 \
-e PROMETHEUS_ENDPOINT="http://192.168.1.201:8080/prometheus" \
-e LOKI_ENPOINT="http://192.168.1.201:3100" \
-e INVOICE_NINJA_ENDPOINT="http://192.168.1.201:8888" \
-e INVOICE_NINJA_API_KEY="" \
-e LOKI_QUERY_LINE_LIMIT="100" \
billing-api
Development
-
Install deps:
-
Install Go: https://go.dev/doc/install
-
Install Chi:
go get -u github.com/go-chi/chi/v5 -
Install http-swagger:
go install github.com/swaggo/swag/cmd/swag@latest
-
-
Set required Env Vars:
export PROMETHEUS_ENDPOINT="http://127.0.0.1:8080/prometheus" export LOKI_ENDPOINT="http://127.0.0.1:3100" export INVOICE_NINJA_ENDPOINT="http://127.0.0.1:8888" export INVOICE_NINJA_API_KEY="" export LOKI_QUERY_LINE_LIMIT="100" -
Run
swag init --parseDependency --parseInternal -g main.goto generate docs -
Run
go buildto build the program -
Run
go run main.goto start the server onlocalhost:3000