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

Generate metrics reference docs #2164

Merged
merged 16 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
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
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
# docs
/docs/content/en/docs/reference/helm-chart.md linguist-generated
/docs/content/en/docs/reference/grpc-api.md linguist-generated
/docs/content/en/docs/reference/metrics.md linguist-generated
/docs/data/tetragon_flags.yaml linguist-generated
3 changes: 3 additions & 0 deletions .github/workflows/generated-files.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,6 @@ jobs:
git status --porcelain
echo "Please run 'make generate-flags' and submit your changes'"; exit 1
fi
- name: Check metrics docs
run: |
make lint-metrics-md
27 changes: 27 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,10 @@ help:
@echo ' clang-format - run code formatter on BPF code'
@echo ' go-format - run code formatter on Go code'
@echo ' format - convenience alias for clang-format and go-format'
@echo ' lint-metrics-md - check if metrics documentation is up to date'
@echo ' Documentation:'
@echo ' docs - preview documentation website'
@echo ' metrics-docs - generate metrics reference documentation page'
@echo 'Options:'
@echo ' TARGET_ARCH - target architecture to build for (e.g. amd64 or arm64)'
@echo ' BPF_TARGET_ARCH - target architecture for BPF progs, set by TARGET_ARCH'
Expand Down Expand Up @@ -419,3 +421,28 @@ kind-install-tetragon:

.PHONY: kind-setup
kind-setup: images kind kind-install-tetragon

METRICS_DOCS_PATH := docs/content/en/docs/reference/metrics.md

.PHONY: metrics-docs
metrics-docs: tetra
lambdanis marked this conversation as resolved.
Show resolved Hide resolved
echo '---' > $(METRICS_DOCS_PATH)
echo 'title: "Metrics"' >> $(METRICS_DOCS_PATH)
echo 'description: >' >> $(METRICS_DOCS_PATH)
echo ' This reference is autogenerated from the Tetragon Prometheus metrics registry' >> $(METRICS_DOCS_PATH)
echo ' using github.com/isovalent/metricstool' >> $(METRICS_DOCS_PATH)
echo 'weight: 4' >> $(METRICS_DOCS_PATH)
echo '---' >> $(METRICS_DOCS_PATH)
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)

.PHONY: lint-metrics-md
lint-metrics-md: metrics-docs
@if [ -n "$$(git status --porcelain $(METRICS_DOCS_PATH))" ]; then \
echo "metrics doc out of sync; please run 'make metrics-docs'" > /dev/stderr; \
false; \
fi
2 changes: 2 additions & 0 deletions cmd/tetra/commands_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ 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"
Expand All @@ -19,4 +20,5 @@ func addCommands(rootCmd *cobra.Command) {
rootCmd.AddCommand(dump.New())
rootCmd.AddCommand(policyfilter.New())
rootCmd.AddCommand(probe.New())
rootCmd.AddCommand(metrics.New())
}
82 changes: 82 additions & 0 deletions cmd/tetra/metrics/print.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Tetragon

package metrics

import (
"log/slog"

"github.com/isovalent/metricstool/pkg/metricsmd"
"github.com/prometheus/client_golang/prometheus"
"github.com/spf13/cobra"

"github.com/cilium/tetragon/pkg/metrics/metricsconfig"
)

func New() *cobra.Command {
targets := map[string]string{
"health": "Tetragon Health",
"resources": "Tetragon Resources",
"events": "Tetragon Events",
}

overrides := []metricsmd.LabelOverrides{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you need to provide "false" values here or can you just pass an empty object? Can be a bit misleading

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could pass empty strings as label values, then the docs would still list the label names but not values.

I'm thinking having example values is helpful (users can see the format), but currently the reference doesn't distinguish between example and real values, so I see how it can be misleading. Maybe the next iteration of metricstool could support rendering example and real label values differently or something.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the build info labels specifically, we can make them less confusing by updating them on a release too.

// Theses metrics takes VCS info into account supplied at build
// time, which changes every build, so override those.
{
Metric: "go_info",
Overrides: []metricsmd.LabelValues{
{
Label: "version",
Values: []string{"go1.22.0"},
},
},
},
{
Metric: "tetragon_build_info",
Overrides: []metricsmd.LabelValues{
{
Label: "commit",
Values: []string{"931b70f2c9878ba985ba6b589827bea17da6ec33"},
},
{
Label: "go_version",
Values: []string{"go1.22.0"},
},
{
Label: "modified",
Values: []string{"false"},
},
{
Label: "time",
Values: []string{"2022-05-13T15:54:45Z"},
},
},
},
}

config := &metricsmd.Config{
Targets: targets,
LabelOverrides: overrides,
InitMetrics: initMetrics,
HeadingLevel: 1,
}

cmd, err := metricsmd.NewCmd(nil, nil, config)
if err != nil {
slog.Error("failed to create metrics-docs command", "error", err)
}
return cmd
}

func initMetrics(target string, reg *prometheus.Registry, _ *slog.Logger) error {
switch target {
case "health":
metricsconfig.InitHealthMetricsForDocs(reg)
case "resources":
metricsconfig.InitResourcesMetricsForDocs(reg)
case "events":
metricsconfig.InitEventsMetricsForDocs(reg)
}
return nil
}
Loading
Loading