Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] add metrics to zarf-agent #1853

Closed
wants to merge 48 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
987a4bb
[FEATURE] add metrics to zarf-agent
cmwylie19 Jun 26, 2023
72d51b3
Merge branch 'main' into 1849
cmwylie19 Jun 27, 2023
480b5f6
[DOCS] show how to scrape zarf-agent
cmwylie19 Jun 28, 2023
9c97983
[TASK] add label to agent-hook service for Service Monitor to scrape;
cmwylie19 Jun 28, 2023
1ecbf9b
Merge branch 'main' into 1849
cmwylie19 Jun 28, 2023
5dd63d0
Merge branch 'main' into 1849
cmwylie19 Jun 28, 2023
0039cd5
Update examples/big-bang/README.md
cmwylie19 Jun 28, 2023
c750db9
Merge branch 'main' into 1849
cmwylie19 Jun 29, 2023
77604b7
[TASK] zarf port-forward suggestion
cmwylie19 Jun 29, 2023
9d54451
[TASK] zarf tools k create suggestion
cmwylie19 Jun 29, 2023
402cea3
init
cmwylie19 Jun 29, 2023
4a8098d
[DOCS] scraping example
cmwylie19 Jun 29, 2023
e8de544
[DOCS] prom operator bundle
cmwylie19 Jun 29, 2023
5932a5a
[TEST] e2e test for Prometheus
cmwylie19 Jun 29, 2023
1a57006
Merge branch 'main' into 1849
cmwylie19 Jun 29, 2023
f6d0aa3
[TASK] fix tests
cmwylie19 Jun 29, 2023
94e7f44
Merge branch 'main' into 1849
cmwylie19 Jun 29, 2023
46b0860
[TASK] Make sure prometheus comes up
cmwylie19 Jun 29, 2023
c2039e1
Merge branch 'main' into 1849
cmwylie19 Jun 30, 2023
da58342
[TASK] undo tests
cmwylie19 Jun 30, 2023
c6d84cd
[TASK] revert changes and inadvertent test
cmwylie19 Jun 30, 2023
888be41
[TASK] end to end tests
cmwylie19 Jun 30, 2023
f5c3795
Update examples/scraping-zarf-agent/zarf.yaml
cmwylie19 Jul 5, 2023
65a3b5e
Update examples/big-bang/README.md
cmwylie19 Jul 5, 2023
1614dd9
Update examples/scraping-zarf-agent/README.md
cmwylie19 Jul 5, 2023
087b84f
[TASK] upstream BB README
cmwylie19 Jul 5, 2023
9b75e7a
Merge branch 'main' into 1849
cmwylie19 Jul 5, 2023
63009bb
[TASK] fix newlines and prom-service
cmwylie19 Jul 5, 2023
158c7ac
[TASK] WIP
cmwylie19 Jul 6, 2023
5ef8e6d
[TASK] Updates
cmwylie19 Jul 6, 2023
be7eb94
Merge branch 'main' into 1849
cmwylie19 Jul 10, 2023
31ea09b
Merge branch 'main' into 1849
cmwylie19 Jul 10, 2023
78697ef
Merge branch 'main' into 1849
cmwylie19 Jul 10, 2023
59717d9
[TASK] Update example
cmwylie19 Jul 10, 2023
09eaaff
[TASK] update scraping example
cmwylie19 Jul 10, 2023
7d7998d
[TASK] prepare demo
cmwylie19 Jul 11, 2023
afcfa7e
Merge branch 'main' into 1849
cmwylie19 Jul 11, 2023
6cdd0c7
[TASK] reduce size in values.yaml
cmwylie19 Jul 12, 2023
f592280
[TASK] fix tests
cmwylie19 Jul 12, 2023
e2ed6b0
[TASK] sboms local testing
cmwylie19 Jul 12, 2023
e2e1046
[TASK] zarf init file
cmwylie19 Jul 12, 2023
f3c4e38
[TASK] update prometheus svc name
cmwylie19 Jul 12, 2023
5fcfe39
[TASK] format README.md
cmwylie19 Jul 12, 2023
7eaf174
Merge branch 'defenseunicorns:main' into 1849
cmwylie19 Jul 12, 2023
16a7c5d
Merge branch 'main' into 1849
cmwylie19 Jul 12, 2023
ac0b17c
[TASK] add link to original values file
cmwylie19 Jul 12, 2023
a98ed2c
Merge branch 'main' into 1849
cmwylie19 Jul 20, 2023
0b32193
Merge branch 'main' into 1849
cmwylie19 Jul 20, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 122 additions & 0 deletions examples/big-bang/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,125 @@ To view the example in its entirety, select the `Edit this page` link below the
:::

<ExampleYAML example="big-bang/yolo" showLink={false} />

## Big Bang Scrape Zarf Agent Metrics
cmwylie19 marked this conversation as resolved.
Show resolved Hide resolved

The Zarf Agent emits Prometheus metrics that can be scraped by Big Bang's Prometheus Operator. To enable this, set `monitoring.enabled` to true in the `config/disable-all.yaml`, and uncomment the `disable-all.yaml` under the `components.extentions.bigbang.valuesFiles` section in `zarf.yaml`.

Finally, create a `ServiceMonitor` for the Zarf Agent. Since this the Zarf Agent exposes an `https` port, we need to provide the `bearerTokenFile` and `tlsConfig` to specify the TLS settings for scraping agaist the service.

```yaml
kubectl create -f -<<EOF
cmwylie19 marked this conversation as resolved.
Show resolved Hide resolved
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
labels:
artifact: monitoring-agent-hook
name: monitoring-agent-hook
namespace: monitoring
spec:
endpoints:
- bearerTokenFile: /var/run/secrets/kubernetes.io/serviceaccount/token
targetPort: 443
path: /metrics
scheme: https
tlsConfig:
caFile: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
insecureSkipVerify: false
# host name for the TLS handshake
serverName: zarf-agent.zarf.svc.cluster.local
jobLabel: zarf-agent
namespaceSelector:
matchNames:
- zarf
selector:
matchLabels:
app: agent-hook
EOF
```

At this point, we can curl against the Prometheus API to ensure the Zarf Agent target has been picked up by the Prometheus Operator.

```bash
# terminal 1
$ kubectl port-forward svc/prometheus-operated 9090 -n monitoring
cmwylie19 marked this conversation as resolved.
Show resolved Hide resolved

# terminal 2
$ curl http://localhost:9090/api/v1/targets | jq | grep -A 28 -B 10 '__meta_kubernetes_pod_name": "agent-hook'

"__meta_kubernetes_pod_controller_kind": "ReplicaSet",
"__meta_kubernetes_pod_controller_name": "agent-hook-566b5959d4",
"__meta_kubernetes_pod_host_ip": "172.18.0.2",
"__meta_kubernetes_pod_ip": "10.42.0.13",
"__meta_kubernetes_pod_label_app": "agent-hook",
"__meta_kubernetes_pod_label_pod_template_hash": "566b5959d4",
"__meta_kubernetes_pod_label_zarf_dev_agent": "ignore",
"__meta_kubernetes_pod_labelpresent_app": "true",
"__meta_kubernetes_pod_labelpresent_pod_template_hash": "true",
"__meta_kubernetes_pod_labelpresent_zarf_dev_agent": "true",
"__meta_kubernetes_pod_name": "agent-hook-566b5959d4-gs875",
"__meta_kubernetes_pod_node_name": "k3d-k3s-default-server-0",
"__meta_kubernetes_pod_phase": "Running",
"__meta_kubernetes_pod_ready": "true",
"__meta_kubernetes_pod_uid": "a66fbd5c-dfe5-4fef-9645-0e0c6cbfed8d",
"__meta_kubernetes_service_annotation_meta_helm_sh_release_name": "zarf-d2db14ef40305397791454e883b26fc94ad9615d",
"__meta_kubernetes_service_annotation_meta_helm_sh_release_namespace": "zarf",
"__meta_kubernetes_service_annotationpresent_meta_helm_sh_release_name": "true",
"__meta_kubernetes_service_annotationpresent_meta_helm_sh_release_namespace": "true",
"__meta_kubernetes_service_label_app_kubernetes_io_managed_by": "Helm",
"__meta_kubernetes_service_label_zarf_dev": "agent",
"__meta_kubernetes_service_labelpresent_app_kubernetes_io_managed_by": "true",
"__meta_kubernetes_service_labelpresent_zarf_dev": "true",
"__meta_kubernetes_service_name": "agent-hook",
"__metrics_path__": "/metrics",
"__scheme__": "https",
"__scrape_interval__": "30s",
"__scrape_timeout__": "10s",
"__tmp_prometheus_job_name": "serviceMonitor/monitoring/monitoring-agent-hook/0"
}
},
{
"discoveredLabels": {
"__address__": "10.42.0.14:8443",
"__meta_kubernetes_endpoint_address_target_kind": "Pod",
"__meta_kubernetes_endpoint_address_target_name": "agent-hook-566b5959d4-kxx2m",
"__meta_kubernetes_endpoint_node_name": "k3d-k3s-default-server-0",
"__meta_kubernetes_endpoint_port_protocol": "TCP",
"__meta_kubernetes_endpoint_ready": "true",
--
"__meta_kubernetes_pod_controller_kind": "ReplicaSet",
"__meta_kubernetes_pod_controller_name": "agent-hook-566b5959d4",
"__meta_kubernetes_pod_host_ip": "172.18.0.2",
"__meta_kubernetes_pod_ip": "10.42.0.14",
"__meta_kubernetes_pod_label_app": "agent-hook",
"__meta_kubernetes_pod_label_pod_template_hash": "566b5959d4",
"__meta_kubernetes_pod_label_zarf_dev_agent": "ignore",
"__meta_kubernetes_pod_labelpresent_app": "true",
"__meta_kubernetes_pod_labelpresent_pod_template_hash": "true",
"__meta_kubernetes_pod_labelpresent_zarf_dev_agent": "true",
"__meta_kubernetes_pod_name": "agent-hook-566b5959d4-kxx2m",
"__meta_kubernetes_pod_node_name": "k3d-k3s-default-server-0",
"__meta_kubernetes_pod_phase": "Running",
"__meta_kubernetes_pod_ready": "true",
"__meta_kubernetes_pod_uid": "252d5cd0-b9be-4a23-97cf-d94d349e50a5",
"__meta_kubernetes_service_annotation_meta_helm_sh_release_name": "zarf-d2db14ef40305397791454e883b26fc94ad9615d",
"__meta_kubernetes_service_annotation_meta_helm_sh_release_namespace": "zarf",
"__meta_kubernetes_service_annotationpresent_meta_helm_sh_release_name": "true",
"__meta_kubernetes_service_annotationpresent_meta_helm_sh_release_namespace": "true",
"__meta_kubernetes_service_label_app_kubernetes_io_managed_by": "Helm",
"__meta_kubernetes_service_label_zarf_dev": "agent",
"__meta_kubernetes_service_labelpresent_app_kubernetes_io_managed_by": "true",
"__meta_kubernetes_service_labelpresent_zarf_dev": "true",
"__meta_kubernetes_service_name": "agent-hook",
"__metrics_path__": "/metrics",
"__scheme__": "https",
"__scrape_interval__": "30s",
"__scrape_timeout__": "10s",
"__tmp_prometheus_job_name": "serviceMonitor/monitoring/monitoring-agent-hook/0"
}
},
```

To see your metrics head to the Prometheus UI at http://localhost:9090/graph and select the `agent_hook` target.

:::
2 changes: 2 additions & 0 deletions packages/zarf-agent/manifests/service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ kind: Service
metadata:
name: agent-hook
namespace: zarf
labels:
app: agent-hook
spec:
selector:
app: agent-hook
Expand Down
3 changes: 3 additions & 0 deletions src/internal/agent/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/defenseunicorns/zarf/src/internal/agent/hooks"
"github.com/defenseunicorns/zarf/src/pkg/message"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

// NewAdmissionServer creates an http.Server for the mutating webhook admission handler.
Expand All @@ -26,6 +27,7 @@ func NewAdmissionServer(port string) *http.Server {
mux.Handle("/healthz", healthz())
mux.Handle("/mutate/pod", ah.Serve(podsMutation))
mux.Handle("/mutate/flux-gitrepository", ah.Serve(gitRepositoryMutation))
mux.Handle("/metrics", promhttp.Handler())

return &http.Server{
Addr: fmt.Sprintf(":%s", port),
Expand All @@ -40,6 +42,7 @@ func NewProxyServer(port string) *http.Server {
mux := http.NewServeMux()
mux.Handle("/healthz", healthz())
mux.Handle("/", ProxyHandler())
mux.Handle("/metrics", promhttp.Handler())

return &http.Server{
Addr: fmt.Sprintf(":%s", port),
Expand Down