From 5a665087b4fc49090ba0abede4cf1b8e5f8a75be Mon Sep 17 00:00:00 2001 From: Anna Kapuscinska Date: Wed, 26 Jun 2024 23:53:41 +0100 Subject: [PATCH 1/2] cmd: Move metrics-docs out of tetra The command generating metrics reference starts a metrics server and initializes all the metrics. This means it needs to import a large chunk of Tetragon code (every package that defines metrics). Before, the command was included in tetra CLI. However, the large import graph is problematic for tetra, so let's move metrics docs generation into a separate command. Signed-off-by: Anna Kapuscinska --- .gitignore | 1 + Makefile | 12 ++++++++---- cmd/tetra/commands_linux.go | 2 -- .../print.go => tetragon-metrics-docs/main.go} | 9 ++++++++- 4 files changed, 17 insertions(+), 7 deletions(-) rename cmd/{tetra/metrics/print.go => tetragon-metrics-docs/main.go} (95%) diff --git a/.gitignore b/.gitignore index 6288bc65574..ab36077a7dd 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ lib/* /tetra /tetragon-oci-hook /tetragon-oci-hook-setup +/tetragon-metrics-docs /parsertest /checkerpc observer.test diff --git a/Makefile b/Makefile index ccfdd4b377f..48b3b8166aa 100644 --- a/Makefile +++ b/Makefile @@ -437,8 +437,12 @@ kind-setup: images kind kind-install-tetragon METRICS_DOCS_PATH := docs/content/en/docs/reference/metrics.md +.PHONY: tetragon-metrics-docs +tetragon-metrics-docs: + $(GO_BUILD) ./cmd/tetragon-metrics-docs/ + .PHONY: metrics-docs -metrics-docs: tetra +metrics-docs: tetragon-metrics-docs echo '---' > $(METRICS_DOCS_PATH) echo 'title: "Metrics"' >> $(METRICS_DOCS_PATH) echo 'description: >' >> $(METRICS_DOCS_PATH) @@ -448,9 +452,9 @@ metrics-docs: tetra echo '{{< comment >}}' >> $(METRICS_DOCS_PATH) echo 'This page is autogenerated via `make metrics-doc` please do not edit directly.' >> $(METRICS_DOCS_PATH) echo '{{< /comment >}}' >> $(METRICS_DOCS_PATH) - $(CONTAINER_ENGINE) run --rm -v $(PWD):$(PWD) -w $(PWD) $(GO_IMAGE) ./tetra metrics-docs health >> $(METRICS_DOCS_PATH) - $(CONTAINER_ENGINE) run --rm -v $(PWD):$(PWD) -w $(PWD) $(GO_IMAGE) ./tetra metrics-docs resources >> $(METRICS_DOCS_PATH) - $(CONTAINER_ENGINE) run --rm -v $(PWD):$(PWD) -w $(PWD) $(GO_IMAGE) ./tetra metrics-docs events >> $(METRICS_DOCS_PATH) + $(CONTAINER_ENGINE) run --rm -v $(PWD):$(PWD) -w $(PWD) $(GO_IMAGE) ./tetragon-metrics-docs health >> $(METRICS_DOCS_PATH) + $(CONTAINER_ENGINE) run --rm -v $(PWD):$(PWD) -w $(PWD) $(GO_IMAGE) ./tetragon-metrics-docs resources >> $(METRICS_DOCS_PATH) + $(CONTAINER_ENGINE) run --rm -v $(PWD):$(PWD) -w $(PWD) $(GO_IMAGE) ./tetragon-metrics-docs events >> $(METRICS_DOCS_PATH) .PHONY: lint-metrics-md lint-metrics-md: metrics-docs diff --git a/cmd/tetra/commands_linux.go b/cmd/tetra/commands_linux.go index c7f242cc1a1..c50f70d1f54 100644 --- a/cmd/tetra/commands_linux.go +++ b/cmd/tetra/commands_linux.go @@ -6,7 +6,6 @@ package main import ( "github.com/cilium/tetragon/cmd/tetra/bugtool" "github.com/cilium/tetragon/cmd/tetra/dump" - "github.com/cilium/tetragon/cmd/tetra/metrics" "github.com/cilium/tetragon/cmd/tetra/policyfilter" "github.com/cilium/tetragon/cmd/tetra/probe" "github.com/cilium/tetragon/cmd/tetra/tracingpolicy" @@ -20,5 +19,4 @@ func addCommands(rootCmd *cobra.Command) { rootCmd.AddCommand(dump.New()) rootCmd.AddCommand(policyfilter.New()) rootCmd.AddCommand(probe.New()) - rootCmd.AddCommand(metrics.New()) } diff --git a/cmd/tetra/metrics/print.go b/cmd/tetragon-metrics-docs/main.go similarity index 95% rename from cmd/tetra/metrics/print.go rename to cmd/tetragon-metrics-docs/main.go index 4ab68732f36..288b0c99f7f 100644 --- a/cmd/tetra/metrics/print.go +++ b/cmd/tetragon-metrics-docs/main.go @@ -1,10 +1,11 @@ // SPDX-License-Identifier: Apache-2.0 // Copyright Authors of Tetragon -package metrics +package main import ( "log/slog" + "os" "github.com/isovalent/metricstool/pkg/metricsmd" "github.com/prometheus/client_golang/prometheus" @@ -13,6 +14,12 @@ import ( "github.com/cilium/tetragon/pkg/metricsconfig" ) +func main() { + if err := New().Execute(); err != nil { + os.Exit(1) + } +} + func New() *cobra.Command { targets := map[string]string{ "health": "Tetragon Health", From 17fe423ed466d6254fc3e9f6c85f16de9ce0a309 Mon Sep 17 00:00:00 2001 From: Anna Kapuscinska Date: Thu, 27 Jun 2024 13:24:58 +0100 Subject: [PATCH 2/2] cmd: Refactor tetragon-metrics-docs command Targets and InitMetrics function are now passed as arguments to the New function. This is done so that the New function can be reused to generate different references (e.g. for different metrics groups). Signed-off-by: Anna Kapuscinska --- cmd/tetragon-metrics-docs/main.go | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/cmd/tetragon-metrics-docs/main.go b/cmd/tetragon-metrics-docs/main.go index 288b0c99f7f..1af98c31035 100644 --- a/cmd/tetragon-metrics-docs/main.go +++ b/cmd/tetragon-metrics-docs/main.go @@ -14,19 +14,20 @@ import ( "github.com/cilium/tetragon/pkg/metricsconfig" ) -func main() { - if err := New().Execute(); err != nil { - os.Exit(1) - } -} +type initMetricsFunc func(string, *prometheus.Registry, *slog.Logger) error -func New() *cobra.Command { +func main() { targets := map[string]string{ "health": "Tetragon Health", "resources": "Tetragon Resources", "events": "Tetragon Events", } + if err := New(targets, initMetrics).Execute(); err != nil { + os.Exit(1) + } +} +func New(targets map[string]string, init initMetricsFunc) *cobra.Command { overrides := []metricsmd.LabelOverrides{ // Theses metrics takes VCS info into account supplied at build // time, which changes every build, so override those. @@ -65,7 +66,7 @@ func New() *cobra.Command { config := &metricsmd.Config{ Targets: targets, LabelOverrides: overrides, - InitMetrics: initMetrics, + InitMetrics: init, HeadingLevel: 1, }