Skip to content

Commit 0fcc5b4

Browse files
committed
chore: harden deployment and tracing
1 parent eb77e3b commit 0fcc5b4

File tree

13 files changed

+199
-60
lines changed

13 files changed

+199
-60
lines changed

.github/workflows/ci.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@ jobs:
2222
uses: actions/setup-go@v5
2323
with:
2424
go-version: ${{ env.GO_VERSION }}
25+
cache: true
2526

2627
- name: Set up Python
2728
uses: actions/setup-python@v5
2829
with:
2930
python-version: ${{ env.PYTHON_VERSION }}
31+
cache: 'pip'
3032

3133
- name: Install Go tools
3234
run: |
@@ -35,6 +37,7 @@ jobs:
3537
3638
- name: Install Python tools
3739
run: |
40+
pip install --upgrade pip
3841
pip install black flake8 isort mypy
3942
4043
- name: Check Go formatting
@@ -84,14 +87,17 @@ jobs:
8487
uses: actions/setup-go@v5
8588
with:
8689
go-version: ${{ env.GO_VERSION }}
90+
cache: true
8791

8892
- name: Set up Python
8993
uses: actions/setup-python@v5
9094
with:
9195
python-version: ${{ env.PYTHON_VERSION }}
96+
cache: 'pip'
9297

9398
- name: Install Python dependencies
9499
run: |
100+
pip install --upgrade pip
95101
pip install -r app/requirements.txt
96102
pip install pytest pytest-cov
97103

cmd/inventory/main.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"time"
88

99
"github.com/EvalOps/keep/pkg/secrets"
10+
"github.com/EvalOps/keep/pkg/telemetry"
1011
serverpkg "github.com/EvalOps/keep/services/inventory/server"
1112
)
1213

@@ -33,12 +34,22 @@ func main() {
3334
RequireMTLS: envOrDefault("INVENTORY_REQUIRE_MTLS", "false") == "true",
3435
}
3536

37+
ctx := context.Background()
38+
if err := telemetry.Init(ctx, telemetry.Config{
39+
Endpoint: os.Getenv("OTEL_EXPORTER_OTLP_ENDPOINT"),
40+
Insecure: os.Getenv("OTEL_EXPORTER_OTLP_INSECURE") == "true",
41+
ServiceName: "inventory",
42+
Environment: envOrDefault("APP_ENV", "development"),
43+
}); err != nil {
44+
log.Printf("telemetry init failed: %v", err)
45+
}
46+
3647
srv, err := serverpkg.NewServer(cfg)
3748
if err != nil {
3849
log.Fatalf("init inventory: %v", err)
3950
}
4051

41-
if err := srv.Start(context.Background()); err != nil {
52+
if err := srv.Start(ctx); err != nil {
4253
log.Fatalf("inventory exit: %v", err)
4354
}
4455
}

deploy/kubernetes/app-deployment.yaml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,32 @@ spec:
1515
containers:
1616
- name: keep-app
1717
image: ghcr.io/example/app:latest
18+
env:
19+
- name: APP_ENV
20+
valueFrom:
21+
configMapKeyRef:
22+
name: keep-config
23+
key: APP_ENV
24+
- name: OTEL_EXPORTER_OTLP_ENDPOINT
25+
value: "http://otel-collector:4318"
1826
ports:
1927
- containerPort: 5000
28+
readinessProbe:
29+
httpGet:
30+
path: /health
31+
port: 5000
32+
initialDelaySeconds: 5
33+
periodSeconds: 10
34+
livenessProbe:
35+
httpGet:
36+
path: /health
37+
port: 5000
38+
initialDelaySeconds: 15
39+
periodSeconds: 20
40+
resources:
41+
requests:
42+
cpu: "100m"
43+
memory: "64Mi"
44+
limits:
45+
cpu: "250m"
46+
memory: "128Mi"

deploy/kubernetes/authz-deployment.yaml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,42 @@ spec:
1515
containers:
1616
- name: authz
1717
image: ghcr.io/example/authz:latest
18+
env:
19+
- name: OPA_URL
20+
valueFrom:
21+
configMapKeyRef:
22+
name: keep-config
23+
key: OPA_URL
24+
- name: INVENTORY_API
25+
valueFrom:
26+
configMapKeyRef:
27+
name: keep-config
28+
key: INVENTORY_API
29+
- name: GOOGLE_CLIENT_ID
30+
valueFrom:
31+
secretKeyRef:
32+
name: keep-secrets
33+
key: google_client_id
1834
ports:
1935
- containerPort: 8443
36+
readinessProbe:
37+
httpGet:
38+
path: /health
39+
port: 8443
40+
scheme: HTTPS
41+
initialDelaySeconds: 5
42+
periodSeconds: 10
43+
livenessProbe:
44+
httpGet:
45+
path: /health
46+
port: 8443
47+
scheme: HTTPS
48+
initialDelaySeconds: 15
49+
periodSeconds: 20
50+
resources:
51+
requests:
52+
cpu: "150m"
53+
memory: "128Mi"
54+
limits:
55+
cpu: "400m"
56+
memory: "256Mi"

deploy/kubernetes/configmap.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
apiVersion: v1
2+
kind: ConfigMap
3+
metadata:
4+
name: keep-config
5+
data:
6+
OPA_URL: http://opa:8181
7+
INVENTORY_API: http://inventory:8080
8+
APP_ENV: production

deploy/kubernetes/inventory-deployment.yaml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,32 @@ spec:
1515
containers:
1616
- name: inventory
1717
image: ghcr.io/example/inventory:latest
18+
env:
19+
- name: INVENTORY_ADDR
20+
value: ":8080"
21+
- name: APP_ENV
22+
valueFrom:
23+
configMapKeyRef:
24+
name: keep-config
25+
key: APP_ENV
26+
readinessProbe:
27+
httpGet:
28+
path: /health
29+
port: 8080
30+
initialDelaySeconds: 5
31+
periodSeconds: 10
32+
livenessProbe:
33+
httpGet:
34+
path: /health
35+
port: 8080
36+
initialDelaySeconds: 15
37+
periodSeconds: 20
38+
resources:
39+
requests:
40+
cpu: "100m"
41+
memory: "128Mi"
42+
limits:
43+
cpu: "300m"
44+
memory: "256Mi"
1845
ports:
1946
- containerPort: 8080

deploy/kubernetes/kustomization.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ resources:
22
- authz-deployment.yaml
33
- inventory-deployment.yaml
44
- app-deployment.yaml
5+
- configmap.yaml
6+
- secret.yaml

deploy/kubernetes/secret.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
apiVersion: v1
2+
kind: Secret
3+
metadata:
4+
name: keep-secrets
5+
type: Opaque
6+
stringData:
7+
google_client_id: ""

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ require (
1717
github.com/jackc/pgx/v5 v5.7.6
1818
github.com/prometheus/client_golang v1.23.2
1919
github.com/rs/zerolog v1.34.0
20+
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.51.0
21+
go.opentelemetry.io/otel v1.38.0
2022
tailscale.com v1.68.0
2123
)
2224

pkg/telemetry/http.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@ import (
44
"net/http"
55

66
"github.com/go-chi/chi/v5"
7-
ogchi "go.opentelemetry.io/contrib/instrumentation/github.com/go-chi/chi/otelchi"
87
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
8+
"go.opentelemetry.io/otel"
9+
"go.opentelemetry.io/otel/propagation"
910
)
1011

1112
// InstrumentRouter attaches otel middleware to chi routers.
1213
func InstrumentRouter(r chi.Router, service string) {
13-
r.Use(ogchi.Middleware(service, ogchi.WithChiRoutes(true)))
14+
r.Use(func(next http.Handler) http.Handler {
15+
return otelhttp.NewHandler(next, service, otelhttp.WithTracerProvider(otel.GetTracerProvider()), otelhttp.WithPropagators(globalPropagator()))
16+
})
1417
}
1518

1619
// WrapClient ensures outgoing requests are traced.
@@ -22,6 +25,10 @@ func WrapClient(c *http.Client) *http.Client {
2225
if base == nil {
2326
base = http.DefaultTransport
2427
}
25-
c.Transport = otelhttp.NewTransport(base)
28+
c.Transport = otelhttp.NewTransport(base, otelhttp.WithTracerProvider(otel.GetTracerProvider()), otelhttp.WithPropagators(globalPropagator()))
2629
return c
2730
}
31+
32+
func globalPropagator() propagation.TextMapPropagator {
33+
return otel.GetTextMapPropagator()
34+
}

0 commit comments

Comments
 (0)