Skip to content

Commit 8cb11f0

Browse files
committed
add Prometheus metrics to Go app
1 parent 3d94c2d commit 8cb11f0

File tree

17 files changed

+294
-91
lines changed

17 files changed

+294
-91
lines changed

.pre-commit-config.yaml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
repos:
2+
- repo: https://github.com/pre-commit/pre-commit-hooks
3+
rev: v5.0.0
4+
hooks:
5+
- id: check-yaml
6+
exclude: ^chart\/.*\/templates\/.*yaml
7+
- id: end-of-file-fixer
8+
- id: trailing-whitespace
9+
- id: detect-aws-credentials
10+
- id: detect-private-key
11+
- id: check-shebang-scripts-are-executable
12+
- id: check-executables-have-shebangs
13+
- repo: https://github.com/gitleaks/gitleaks
14+
rev: v8.24.2
15+
hooks:
16+
- id: gitleaks
17+
- repo: https://github.com/gruntwork-io/pre-commit
18+
rev: v0.1.15
19+
hooks:
20+
- id: helmlint
21+
# - repo: https://github.com/norwoodj/helm-docs
22+
# rev: v1.10.0
23+
# hooks:
24+
# - id: helm-docs
25+
# files: (README\.md\.gotmpl|(Chart|requirements|values)\.yaml)$

Dockerfile

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
1-
FROM golang:1.24.2-bookworm AS builder
1+
FROM golang:1.24.4-bookworm AS builder
22

33
ENV APP_PORT=3080
4+
ENV METRICS_PORT=3088
45

56
WORKDIR /build
67
COPY src/* ./
78
COPY .git ./
89
RUN go mod tidy
910
RUN APP_VERSION=$(git describe --tags || echo '0.0.0') && APP_BUILD_DATE=$(date +%Y%m%d.%H%M) &&\
10-
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 \
11-
go build -o ledlighter -ldflags "-X main.appVersion=$APP_VERSION -X main.buildDate=$APP_BUILD_DATE" .
11+
CGO_ENABLED=0 GOOS=linux \
12+
go build -o ledlighter -ldflags "-X main.appVersion=$APP_VERSION -X main.buildDate=$APP_BUILD_DATE" .
1213

1314
FROM alpine:3.21.3
14-
RUN apk --no-cache add ca-certificates curl htop
1515
RUN addgroup -S web && adduser -S web -G web
1616
USER web
1717
WORKDIR /
1818
COPY --from=builder /build/ledlighter /bin
1919
HEALTHCHECK --interval=30s --timeout=3s \
20-
CMD curl -f http://localhost:${APP_PORT}/healthz || exit 1
20+
CMD curl -f http://localhost:${APP_PORT}/healthz || exit 1
2121
EXPOSE $APP_PORT
22+
EXPOSE $METRICS_PORT
2223
USER root
2324
ENTRYPOINT ["/bin/ledlighter"]
25+
USER web

Dockerfile.goreleaser

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
FROM scratch
22

33
ENV APP_PORT=3080
4+
ENV METRICS_PORT=3088
45

56
COPY ledlighter /ledlighter
67

78
HEALTHCHECK --interval=30s --timeout=3s \
8-
CMD curl -f http://localhost:${APP_PORT}/healthz || exit 1
9+
CMD curl -f http://localhost:${APP_PORT}/healthz || exit 1
10+
911
EXPOSE $APP_PORT
12+
EXPOSE $METRICS_PORT
1013

1114
ENTRYPOINT ["/ledlighter"]

chart/ledlighter/Chart.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ apiVersion: v2
22
name: ledlighter
33
description: ledlighter, a proxy for LED APIs
44
type: application
5-
version: 0.1.0
6-
appVersion: "0.0.1"
5+
version: 0.1.1
6+
appVersion: "0.0.2"

chart/ledlighter/templates/_helpers.tpl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ helm.sh/chart: {{ include "ledlighter.chart" . }}
4040
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
4141
{{- end }}
4242
app.kubernetes.io/managed-by: {{ .Release.Service }}
43+
app: {{ include "ledlighter.name" . }}
4344
{{- end }}
4445

4546
{{/*

chart/ledlighter/templates/deployment.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ spec:
4444
- name: http
4545
containerPort: {{ .Values.service.port }}
4646
protocol: TCP
47+
- name: metrics
48+
containerPort: {{ .Values.service.metricsPort }}
49+
protocol: TCP
4750
{{- with .Values.livenessProbe }}
4851
livenessProbe:
4952
{{- toYaml . | nindent 12 }}

chart/ledlighter/templates/service.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,9 @@ spec:
1111
targetPort: http
1212
protocol: TCP
1313
name: http
14+
- port: {{ .Values.service.metricsPort }}
15+
targetPort: metrics
16+
protocol: TCP
17+
name: metrics
1418
selector:
1519
{{- include "ledlighter.selectorLabels" . | nindent 4 }}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
{{ if .Values.prometheus.serviceMonitor.enabled }}
3+
apiVersion: monitoring.coreos.com/v1
4+
kind: ServiceMonitor
5+
metadata:
6+
labels:
7+
{{- include "ledlighter.labels" . | nindent 4 }}
8+
{{- with .Values.prometheus.serviceMonitor.labels }}
9+
{{- toYaml . | nindent 4 }}
10+
{{- end }}
11+
name: {{ include "ledlighter.fullname" . }}
12+
namespace: {{ .Values.prometheus.serviceMonitor.namespace | default .Release.Namespace }}
13+
spec:
14+
endpoints:
15+
- interval: {{ .Values.prometheus.serviceMonitor.scrapingInterval | default "30s" }}
16+
targetPort: {{ .Values.service.metricsPort }}
17+
path: {{ .Values.prometheus.serviceMonitor.metricsPath | default "/metrics" }}
18+
namespaceSelector:
19+
{{- with .Values.prometheus.serviceMonitor.namespaceSelector }}
20+
{{- toYaml . | nindent 4 }}
21+
{{- end }}
22+
selector:
23+
matchLabels:
24+
{{- with .Values.additionalLabels }}
25+
{{- toYaml . | nindent 6 }}
26+
{{- end }}
27+
{{ end }}
28+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
image:
3+
repository: registry.0.os76.xyz/xeno/ledlighter
4+
pullPolicy: IfNotPresent
5+
tag: 0.0.12

chart/ledlighter/values.yaml

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
1+
---
12
nameOverride: ""
23
fullnameOverride: ""
4+
35
replicaCount: 1
6+
47
image:
58
repository: ghcr.io/xenos76/ledlighter
69
pullPolicy: IfNotPresent
710
tag: ""
11+
812
imagePullSecrets: []
13+
14+
additionalLabels:
15+
app: ledlighter
16+
917
#
1018
# Application Configuration via configMap
1119
# and volume mounts
@@ -20,29 +28,31 @@ appConfig:
2028
description: "RGB bulb, back of desk's monitor"
2129
address: 192.168.1.85
2230
kind: shelly
23-
#
31+
2432
volumes:
2533
- name: config-volume
2634
configMap:
2735
name: ledlighter-config
28-
#
36+
2937
volumeMounts:
3038
- name: config-volume
3139
mountPath: /config.yaml
3240
subPath: config.yaml
33-
#
41+
3442
podAnnotations: {}
3543
podLabels: {}
36-
#
44+
3745
serviceAccount:
3846
create: true
3947
automount: true
4048
annotations: {}
4149
name: ""
50+
4251
service:
4352
type: ClusterIP
4453
port: 3080
45-
#
54+
metricsPort: 3088
55+
4656
resources: {}
4757
# limits:
4858
# cpu: 100m
@@ -55,10 +65,12 @@ livenessProbe:
5565
httpGet:
5666
path: /livez
5767
port: http
68+
5869
readinessProbe:
5970
httpGet:
6071
path: /readyz
6172
port: http
73+
6274
#
6375
# Istio config
6476
#
@@ -69,3 +81,18 @@ istio:
6981
- ledlighter.k3s.os76.xyz
7082
gateways:
7183
- istio-system/gateway-priv-os76
84+
85+
#
86+
# Prometheus
87+
#
88+
prometheus:
89+
serviceMonitor:
90+
enabled: true
91+
namespace: monitoring
92+
namespaceSelector:
93+
matchNames:
94+
- default
95+
labels:
96+
release: kube-prometheus-stack
97+
scrapingInterval: 30s
98+
metricsPath: "/metrics"

0 commit comments

Comments
 (0)