Skip to content

Commit 0e12fa6

Browse files
authored
Expose apiserver library metrics on otel port (default 9464) (#1084)
* Expose apiserver library metrics on otel port (9464) Signed-off-by: Kushal Harish Naidu <kushal.harish.naidu@ericsson.com> * Add unit test for verify there are no errors during metrics gathering Signed-off-by: Kushal Harish Naidu <kushal.harish.naidu@ericsson.com> --------- Signed-off-by: Kushal Harish Naidu <kushal.harish.naidu@ericsson.com>
1 parent ff5d0d4 commit 0e12fa6

2 files changed

Lines changed: 24 additions & 1 deletion

File tree

internal/telemetry/otel.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333

3434
"github.com/prometheus/client_golang/prometheus"
3535
"github.com/prometheus/client_golang/prometheus/promhttp"
36+
"k8s.io/component-base/metrics/legacyregistry"
3637
"k8s.io/klog/v2"
3738
controllerruntimemetrics "sigs.k8s.io/controller-runtime/pkg/metrics"
3839
)
@@ -208,6 +209,7 @@ func startMetricsServerIfConfigured(res *OTelResources) error {
208209
gatherers := prometheus.Gatherers{
209210
prometheus.DefaultGatherer,
210211
controllerruntimemetrics.Registry,
212+
legacyregistry.DefaultGatherer,
211213
}
212214
handler := promhttp.HandlerFor(gatherers, promhttp.HandlerOpts{
213215
ErrorHandling: promhttp.ContinueOnError,

internal/telemetry/otel_test.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ import (
2929
"go.opentelemetry.io/otel"
3030
"google.golang.org/grpc"
3131
"google.golang.org/protobuf/proto"
32+
compbasemetrics "k8s.io/component-base/metrics"
33+
"k8s.io/component-base/metrics/legacyregistry"
3234

3335
otlpmetrics "go.opentelemetry.io/proto/otlp/collector/metrics/v1"
3436
otlptraces "go.opentelemetry.io/proto/otlp/collector/trace/v1"
@@ -60,6 +62,16 @@ func TestPrometheusHTTPServer(t *testing.T) {
6062
t.Setenv(ENV_OTEL_EXPORTER_PROMETHEUS_HOST, "0.0.0.0")
6163
t.Setenv(ENV_OTEL_EXPORTER_PROMETHEUS_PORT, fmt.Sprintf("%d", port))
6264

65+
// Register a trivial metric into the k8s legacy registry to verify it
66+
// appears in the unified /metrics response.
67+
legacyCounter := compbasemetrics.NewCounter(&compbasemetrics.CounterOpts{
68+
Name: "test_legacy_registry_total",
69+
Help: "A test counter registered in the k8s legacy registry",
70+
})
71+
legacyregistry.MustRegister(legacyCounter)
72+
legacyCounter.Inc()
73+
defer legacyregistry.Reset()
74+
6375
ctx, cancel := context.WithCancel(context.Background())
6476
defer cancel()
6577

@@ -74,7 +86,16 @@ func TestPrometheusHTTPServer(t *testing.T) {
7486

7587
body, err := io.ReadAll(resp.Body)
7688
require.NoError(t, err)
77-
assert.Contains(t, string(body), "target_info")
89+
bodyStr := string(body)
90+
91+
// Verify OTel metrics are present
92+
assert.Contains(t, bodyStr, "target_info")
93+
94+
// Verify the legacy registry metric appears (apiserver library metrics path)
95+
assert.Contains(t, bodyStr, "test_legacy_registry_total")
96+
97+
// Verify no gather errors are reported in the response
98+
assert.NotContains(t, bodyStr, "error gathering metrics")
7899
}
79100

80101
func TestPrometheusHTTPServerInvalidPort(t *testing.T) {

0 commit comments

Comments
 (0)