Skip to content

Commit bdcef77

Browse files
committed
register metrics collector to use existing metrics server
Signed-off-by: Elieser Pereira [email protected]
1 parent af078c4 commit bdcef77

File tree

9 files changed

+26
-55
lines changed

9 files changed

+26
-55
lines changed

config/operator/deployment.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ spec:
3838
value: ""
3939
ports:
4040
- name: metrics
41-
containerPort: 2223
41+
containerPort: 8080
4242
- name: probes
4343
containerPort: 8081
4444
livenessProbe:

config/operator/e2e-test/otel/deployment.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ spec:
1212
- name: OTEL_PROM_EXPORTER_ENABLED
1313
value: "true"
1414
- name: OTEL_PROM_EXPORTER_PORT
15-
value: "2223"
15+
value: "8080"
1616
- name: OTEL_EXPORTER_OTLP_METRICS_ENABLED
1717
value: "true"
1818
- name: OTEL_EXPORTER_OTLP_ENDPOINT
1919
value: "http://opentelemetry-collector.open-telemetry-system:4318"
2020
- name: OTEL_METRIC_EXPORT_INTERVAL
2121
value: "1"
22+

config/operator/metrics.service.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ spec:
77
ports:
88
- name: metrics
99
protocol: TCP
10-
port: 2223
10+
port: 8080
1111
targetPort: metrics

operator/controllers/http/config/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ type Metrics struct {
9696
// Sets whether or not to enable the Prometheus metrics exporter
9797
OtelPrometheusExporterEnabled bool `envconfig:"OTEL_PROM_EXPORTER_ENABLED" default:"true"`
9898
// Sets the port which the Prometheus compatible metrics endpoint should be served on
99-
OtelPrometheusExporterPort int `envconfig:"OTEL_PROM_EXPORTER_PORT" default:"2223"`
99+
OtelPrometheusExporterPort int `envconfig:"OTEL_PROM_EXPORTER_PORT" default:"8080"`
100100
// Sets whether or not to enable the OTEL metrics exporter
101101
OtelHTTPExporterEnabled bool `envconfig:"OTEL_EXPORTER_OTLP_METRICS_ENABLED" default:"false"`
102102
}

operator/main.go

Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@ limitations under the License.
1717
package main
1818

1919
import (
20-
"context"
2120
"flag"
22-
"fmt"
2321
"os"
2422

25-
"github.com/go-logr/logr"
23+
httpv1alpha1 "github.com/kedacore/http-add-on/operator/apis/http/v1alpha1"
24+
httpcontrollers "github.com/kedacore/http-add-on/operator/controllers/http"
25+
"github.com/kedacore/http-add-on/operator/controllers/http/config"
26+
"github.com/kedacore/http-add-on/operator/metrics"
27+
"github.com/kedacore/http-add-on/pkg/util"
2628
kedav1alpha1 "github.com/kedacore/keda/v2/apis/keda/v1alpha1"
27-
"github.com/prometheus/client_golang/prometheus/promhttp"
2829
"k8s.io/apimachinery/pkg/runtime"
2930
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
3031
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
@@ -34,14 +35,6 @@ import (
3435
"sigs.k8s.io/controller-runtime/pkg/healthz"
3536
"sigs.k8s.io/controller-runtime/pkg/log/zap"
3637
"sigs.k8s.io/controller-runtime/pkg/metrics/server"
37-
38-
httpv1alpha1 "github.com/kedacore/http-add-on/operator/apis/http/v1alpha1"
39-
httpcontrollers "github.com/kedacore/http-add-on/operator/controllers/http"
40-
"github.com/kedacore/http-add-on/operator/controllers/http/config"
41-
"github.com/kedacore/http-add-on/operator/metrics"
42-
kedahttp "github.com/kedacore/http-add-on/pkg/http"
43-
"github.com/kedacore/http-add-on/pkg/util"
44-
"golang.org/x/sync/errgroup"
4538
// +kubebuilder:scaffold:imports
4639
)
4740

@@ -124,20 +117,6 @@ func main() {
124117

125118
ctx := ctrl.SetupSignalHandler()
126119
ctx = util.ContextWithLogger(ctx, ctrl.Log)
127-
eg, ctx := errgroup.WithContext(ctx)
128-
129-
if metricsCfg.OtelPrometheusExporterEnabled {
130-
// start the prometheus compatible metrics server
131-
// serves a prometheus compatible metrics endpoint on the configured port
132-
eg.Go(func() error {
133-
if err := runMetricsServer(ctx, ctrl.Log, metricsCfg); !util.IsIgnoredErr(err) {
134-
setupLog.Error(err, "could not start the Prometheus metrics server")
135-
return err
136-
}
137-
138-
return nil
139-
})
140-
}
141120
if err = (&httpcontrollers.HTTPScaledObjectReconciler{
142121
Client: mgr.GetClient(),
143122
Scheme: mgr.GetScheme(),
@@ -165,13 +144,3 @@ func main() {
165144
os.Exit(1)
166145
}
167146
}
168-
169-
func runMetricsServer(
170-
ctx context.Context,
171-
lggr logr.Logger,
172-
metricsCfg *config.Metrics,
173-
) error {
174-
lggr.Info("starting the prometheus metrics server", "port", metricsCfg.OtelPrometheusExporterPort, "path", "/metrics")
175-
addr := fmt.Sprintf("0.0.0.0:%d", metricsCfg.OtelPrometheusExporterPort)
176-
return kedahttp.ServeContext(ctx, addr, promhttp.Handler(), nil)
177-
}

operator/metrics/metricscollector.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package metrics
22

33
import (
44
"github.com/kedacore/http-add-on/operator/controllers/http/config"
5+
"go.opentelemetry.io/otel/exporters/prometheus"
6+
ctrlmetrics "sigs.k8s.io/controller-runtime/pkg/metrics"
57
)
68

79
var (
@@ -17,7 +19,8 @@ type Collector interface {
1719

1820
func NewMetricsCollectors(metricsConfig *config.Metrics) {
1921
if metricsConfig.OtelPrometheusExporterEnabled {
20-
promometrics := NewPrometheusMetrics()
22+
options := prometheus.WithRegisterer(ctrlmetrics.Registry)
23+
promometrics := NewPrometheusMetrics(options)
2124
collectors = append(collectors, promometrics)
2225
}
2326

operator/metrics/prommetrics.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@ import (
44
"context"
55
"log"
66

7+
"github.com/kedacore/http-add-on/pkg/build"
78
"go.opentelemetry.io/otel/attribute"
89
"go.opentelemetry.io/otel/exporters/prometheus"
910
api "go.opentelemetry.io/otel/metric"
1011
"go.opentelemetry.io/otel/sdk/metric"
1112
"go.opentelemetry.io/otel/sdk/resource"
1213
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
13-
14-
"github.com/kedacore/http-add-on/pkg/build"
1514
)
1615

1716
type PrometheusMetrics struct {

tests/checks/operator_otel_metrics/operator_otel_metrics_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,16 +167,16 @@ func TestMetricGeneration(t *testing.T) {
167167

168168
// Fetch metrics and validate them
169169
family := fetchAndParsePrometheusMetrics(t, fmt.Sprintf("curl --insecure %s", otelCollectorPromURL))
170-
val, ok := family["operator_http_scaled_object_count_total"]
170+
val, ok := family["operator_http_scaled_object_count"]
171171
// If the metric is not found first time around then retry with a delay.
172172
if !ok {
173173
// Add a small sleep to allow metrics to be pushed from the exporter to the collector
174174
time.Sleep(10 * time.Second)
175175
// Fetch metrics and validate them
176176
family := fetchAndParsePrometheusMetrics(t, fmt.Sprintf("curl --insecure %s", otelCollectorPromURL))
177-
val, ok = family["operator_http_scaled_object_count_total"]
177+
val, ok = family["operator_http_scaled_object_count"]
178178
}
179-
assert.True(t, ok, "operator_http_scaled_object_count_total is available")
179+
assert.True(t, ok, "operator_http_scaled_object_count is available")
180180

181181
requestCount := getMetricsValue(val)
182182
assert.GreaterOrEqual(t, requestCount, float64(1))
@@ -208,13 +208,13 @@ func fetchAndParsePrometheusMetrics(t *testing.T, cmd string) map[string]*prommo
208208
}
209209

210210
func getMetricsValue(val *prommodel.MetricFamily) float64 {
211-
if val.GetName() == "operator_http_scaled_object_count_total" {
211+
if val.GetName() == "operator_http_scaled_object_count" {
212212
metrics := val.GetMetric()
213213
for _, metric := range metrics {
214214
labels := metric.GetLabel()
215215
for _, label := range labels {
216216
if *label.Name == "namespace" && *label.Value == testNamespace {
217-
return metric.GetCounter().GetValue()
217+
return metric.GetGauge().GetValue()
218218
}
219219
}
220220
}

tests/checks/operator_prometheus_metrics/operator_prometheus_metrics_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ var (
2929
host = testName
3030
minReplicaCount = 0
3131
maxReplicaCount = 1
32-
kedaOperatorPrometheusURL = "http://keda-add-ons-http-operator-metrics.keda:2223/metrics"
32+
kedaOperatorPrometheusURL = "http://keda-add-ons-http-operator-metrics.keda:8080/metrics"
3333
)
3434

3535
type templateData struct {
@@ -167,13 +167,11 @@ func TestMetricGeneration(t *testing.T) {
167167

168168
// Fetch metrics and validate them
169169
family := fetchAndParsePrometheusMetrics(t, fmt.Sprintf("curl --insecure %s", kedaOperatorPrometheusURL))
170-
val, ok := family["operator_http_scaled_object_count_total"]
171-
assert.True(t, ok, "operator_http_scaled_object_count_total is available")
172-
t.Log("--- ASSERT operator_http_scaled_object_count_total is available ---")
170+
val, ok := family["operator_http_scaled_object_count"]
171+
assert.True(t, ok, "operator_http_scaled_object_count is available")
173172

174173
requestCount := getMetricsValue(val)
175174
assert.GreaterOrEqual(t, requestCount, float64(1))
176-
t.Log("--- ASSERT metrics greater than1 ---")
177175

178176
// cleanup
179177
DeleteKubernetesResources(t, testNamespace, data, templates)
@@ -203,13 +201,14 @@ func fetchAndParsePrometheusMetrics(t *testing.T, cmd string) map[string]*prommo
203201
}
204202

205203
func getMetricsValue(val *prommodel.MetricFamily) float64 {
206-
if val.GetName() == "operator_http_scaled_object_count_total" {
204+
if val.GetName() == "operator_http_scaled_object_count" {
205+
207206
metrics := val.GetMetric()
208207
for _, metric := range metrics {
209208
labels := metric.GetLabel()
210209
for _, label := range labels {
211210
if *label.Name == "namespace" && *label.Value == testNamespace {
212-
return metric.GetCounter().GetValue()
211+
return metric.GetGauge().GetValue()
213212
}
214213
}
215214
}

0 commit comments

Comments
 (0)