Skip to content

Commit f56f00d

Browse files
committed
feat: add metrics
1 parent d2f5fab commit f56f00d

File tree

4 files changed

+34
-0
lines changed

4 files changed

+34
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ A reverse proxy service that terminates TLS and exposes an AMD SEV-SNP attestati
1515
```yaml
1616
domain: example.com # Domain name for TLS certificate (leave empty to generate a self-signed certificate)
1717
upstream-port: 8080 # Required: upstream HTTP port
18+
metrics-port: 8081 # Optional: Prometheus metrics port (disabled if empty)
1819
listen-port: 443 # Port to listen on (default: 443)
1920
paths: # Optional: List of allowed paths (default: all)
2021
- /api/v1

Diff for: go.mod

+8
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,35 @@ require (
66
github.com/caddyserver/certmagic v0.21.7
77
github.com/creasty/defaults v1.8.0
88
github.com/google/go-sev-guest v0.12.1
9+
github.com/prometheus/client_golang v1.15.1
910
github.com/sirupsen/logrus v1.9.1
1011
github.com/tinfoilanalytics/verifier v0.0.9
1112
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac
1213
gopkg.in/yaml.v3 v3.0.1
1314
)
1415

1516
require (
17+
github.com/beorn7/perks v1.0.1 // indirect
1618
github.com/blocky/nitrite v0.0.1 // indirect
1719
github.com/caddyserver/zerossl v0.1.3 // indirect
20+
github.com/cespare/xxhash/v2 v2.2.0 // indirect
1821
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
1922
github.com/go-jose/go-jose/v4 v4.0.2 // indirect
23+
github.com/golang/protobuf v1.5.4 // indirect
2024
github.com/google/go-configfs-tsm v0.2.2 // indirect
2125
github.com/google/go-containerregistry v0.20.2 // indirect
2226
github.com/google/logger v1.1.1 // indirect
2327
github.com/google/uuid v1.6.0 // indirect
2428
github.com/klauspost/cpuid/v2 v2.2.9 // indirect
2529
github.com/letsencrypt/boulder v0.0.0-20240620165639-de9c06129bec // indirect
2630
github.com/libdns/libdns v0.2.2 // indirect
31+
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
2732
github.com/mholt/acmez/v3 v3.0.1 // indirect
2833
github.com/miekg/dns v1.1.62 // indirect
2934
github.com/opencontainers/go-digest v1.0.0 // indirect
35+
github.com/prometheus/client_model v0.4.0 // indirect
36+
github.com/prometheus/common v0.42.0 // indirect
37+
github.com/prometheus/procfs v0.9.0 // indirect
3038
github.com/rogpeppe/go-internal v1.13.1 // indirect
3139
github.com/secure-systems-lab/go-securesystemslib v0.8.0 // indirect
3240
github.com/sigstore/sigstore v1.8.9 // indirect

Diff for: go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
2424
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
2525
github.com/go-test/deep v1.1.1 h1:0r/53hagsehfO4bzD2Pgr/+RgHqhmf+k1Bpse2cTu1U=
2626
github.com/go-test/deep v1.1.1/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncVwcsE=
27+
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
2728
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
2829
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
2930
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
@@ -109,6 +110,7 @@ golang.org/x/mod v0.22.0 h1:D4nJWe9zXqHOmWqj4VMOJhvzj7bEZg4wEYa759z1pH4=
109110
golang.org/x/mod v0.22.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
110111
golang.org/x/net v0.34.0 h1:Mb7Mrk043xzHgnRM88suvJFwzVrRfHEHJEl5/71CKw0=
111112
golang.org/x/net v0.34.0/go.mod h1:di0qlW3YNM5oh6GqDGQr92MyTozJPmybPK4Ev/Gm31k=
113+
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
112114
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
113115
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
114116
golang.org/x/sys v0.0.0-20210426230700-d19ff857e887/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

Diff for: main.go

+23
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import (
1616
"github.com/creasty/defaults"
1717
"github.com/google/go-sev-guest/abi"
1818
"github.com/google/go-sev-guest/client"
19+
"github.com/prometheus/client_golang/prometheus"
20+
"github.com/prometheus/client_golang/prometheus/promhttp"
1921
log "github.com/sirupsen/logrus"
2022
"golang.org/x/time/rate"
2123
"gopkg.in/yaml.v3"
@@ -28,6 +30,7 @@ var version = "dev"
2830
var config struct {
2931
Domain string `yaml:"domain"`
3032
ListenPort int `yaml:"listen-port" default:"443"`
33+
MetricsPort int `yaml:"metrics-port"`
3134
UpstreamPort int `yaml:"upstream-port"`
3235
Paths []string `yaml:"paths"`
3336
KeyServer string `yaml:"key-server"`
@@ -103,6 +106,16 @@ func main() {
103106

104107
mux := http.NewServeMux()
105108

109+
requestsMetric := prometheus.NewCounterVec(
110+
prometheus.CounterOpts{
111+
Name: "sev_shim_proxy_requests_total",
112+
Help: "Number of HTTP requests",
113+
},
114+
[]string{},
115+
)
116+
r := prometheus.NewRegistry()
117+
r.MustRegister(requestsMetric)
118+
106119
// Request TLS certificate
107120
var tlsConfig *tls.Config
108121
if config.Domain != "" {
@@ -159,6 +172,8 @@ func main() {
159172
}
160173

161174
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
175+
requestsMetric.WithLabelValues().Inc()
176+
162177
auth := r.Header.Get("Authorization")
163178

164179
if config.KeyServer != "" {
@@ -230,6 +245,14 @@ func main() {
230245
json.NewEncoder(w).Encode(att)
231246
})
232247

248+
if config.MetricsPort > 0 {
249+
log.Printf("Starting metrics server on port %d", config.MetricsPort)
250+
go func() {
251+
listenAddr := fmt.Sprintf(":%d", config.MetricsPort)
252+
log.Fatal(http.ListenAndServe(listenAddr, promhttp.HandlerFor(r, promhttp.HandlerOpts{})))
253+
}()
254+
}
255+
233256
listenAddr := fmt.Sprintf(":%d", config.ListenPort)
234257
httpServer := &http.Server{
235258
Addr: listenAddr,

0 commit comments

Comments
 (0)