Billing API for generating invoice data from usage
  • Go 80.9%
  • Go Template 11.3%
  • Dockerfile 7.8%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
2026-08-29 18:54:17 +02:00
.forgejo/workflows fix secret names 2026-08-21 22:52:59 +02:00
billing-api play around with different error handeling and logging 2026-08-29 18:53:46 +02:00
charts/billing-api add helm chart 2026-08-21 23:54:04 +02:00
LICENSE Initial commit 2026-08-21 11:52:48 +00:00
README.md updte readme 2026-08-29 18:54:17 +02:00

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

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

  1. 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
      
  2. 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"
    
  3. Run swag init --parseDependency --parseInternal -g main.go to generate docs

  4. Run go build to build the program

  5. Run go run main.go to start the server on localhost:3000