Skip to content

Commit af078c4

Browse files
committed
document how to use prometheus and otel metrics. change matric type to fit meaning
Signed-off-by: Elieser Pereira <[email protected]>
1 parent ebf0358 commit af078c4

File tree

7 files changed

+62
-9
lines changed

7 files changed

+62
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ This changelog keeps track of work items that have been completed and are ready
3333
### Improvements
3434

3535
- **Interceptor**: Support HTTPScaledObject scoped timeout ([#813](https://github.com/kedacore/http-add-on/issues/813))
36+
- **General**: Add prometehus and otel instrumentation for the operator ([#965](https://github.com/kedacore/http-add-on/issues/965))
3637

3738
### Fixes
3839

docs/operate.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,22 @@ Optional variables
6060
`OTEL_EXPORTER_OTLP_TRACES_TIMEOUT` - The batcher timeout in seconds to send batch of data points (`5` by default)
6161

6262
### Configuring Service Failover
63+
64+
# Configuring metrics for the KEDA HTTP Add-on Operator
65+
66+
### Exportable metrics:
67+
* **operator_http_scaled_object_count** - the number of http_scaled_object
68+
69+
There are currently 2 supported methods for exposing metrics from the operator - via a Prometheus compatible metrics endpoint or by pushing metrics to a OTEL HTTP collector.
70+
71+
### Configuring the Prometheus compatible metrics endpoint
72+
When configured, the operator can expose metrics on a Prometheus compatible endpoint.
73+
74+
This endpoint can be enabled by setting the `OTEL_PROM_EXPORTER_ENABLED` environment variable to `true` on the operator deployment (`true` by default) and by setting `OTEL_PROM_EXPORTER_PORT` to an unused port for the endpoint to be made avaialble on (`2223` by default).
75+
76+
### Configuring the OTEL HTTP exporter
77+
When configured, the ioperator can export metrics to a OTEL HTTP collector.
78+
79+
The OTEL exporter can be enabled by setting the `OTEL_EXPORTER_OTLP_METRICS_ENABLED` environment variable to `true` on the operator deployment (`false` by default). When enabled the `OTEL_EXPORTER_OTLP_ENDPOINT` environment variable must also be configured so the exporter knows what collector to send the metrics to (e.g. http://opentelemetry-collector.open-telemetry-system:4318).
80+
81+
If you need to provide any headers such as authentication details in order to utilise your OTEL collector you can add them into the `OTEL_EXPORTER_OTLP_HEADERS` environment variable. The frequency at which the metrics are exported can be configured by setting `OTEL_METRIC_EXPORT_INTERVAL` to the number of seconds you require between each export interval (`30` by default).

operator/controllers/http/httpscaledobject_controller.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,9 @@ func (r *HTTPScaledObjectReconciler) updatePromMetrics(ctx context.Context, scal
170170
logger.Info("updatePromMetrics")
171171
metrics.RecordHTTPScaledObjectCount(namespacedName)
172172
}
173+
174+
func (r *HTTPScaledObjectReconciler) updatePromMetricsOnDelete(ctx context.Context, scaledObject *httpv1alpha1.HTTPScaledObject, namespacedName string) {
175+
logger := log.FromContext(ctx, "updatePromMetricsOnDelete", namespacedName)
176+
logger.Info("updatePromMetricsOnDelete")
177+
metrics.RecordDeleteHTTPScaledObjectCount(namespacedName)
178+
}

operator/metrics/metricscollector.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const meterName = "keda-http-add-on-operator"
1212

1313
type Collector interface {
1414
RecordHTTPScaledObjectCount(namespace string)
15+
RecordDeleteHTTPScaledObjectCount(namespace string)
1516
}
1617

1718
func NewMetricsCollectors(metricsConfig *config.Metrics) {
@@ -31,3 +32,9 @@ func RecordHTTPScaledObjectCount(namespace string) {
3132
collector.RecordHTTPScaledObjectCount(namespace)
3233
}
3334
}
35+
36+
func RecordDeleteHTTPScaledObjectCount(namespace string) {
37+
for _, collector := range collectors {
38+
collector.RecordDeleteHTTPScaledObjectCount(namespace)
39+
}
40+
}

operator/metrics/otelmetrics.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616

1717
type OtelMetrics struct {
1818
meter api.Meter
19-
httpScaledObjectCounter api.Int64Counter
19+
httpScaledObjectCounter api.Int64UpDownCounter
2020
}
2121

2222
func NewOtelMetrics(options ...metric.Option) *OtelMetrics {
@@ -43,7 +43,7 @@ func NewOtelMetrics(options ...metric.Option) *OtelMetrics {
4343
provider := metric.NewMeterProvider(options...)
4444
meter := provider.Meter(meterName)
4545

46-
httpScaledObjectCounter, err := meter.Int64Counter("operator_http_scaled_object_count", api.WithDescription("a counter of http_scaled_objects processed by the operator"))
46+
httpScaledObjectCounter, err := meter.Int64UpDownCounter("operator_http_scaled_object_count", api.WithDescription("a counter of http_scaled_objects processed by the operator"))
4747
if err != nil {
4848
log.Fatalf("could not create new otelhttpmetric request counter: %v", err)
4949
}
@@ -63,3 +63,13 @@ func (om *OtelMetrics) RecordHTTPScaledObjectCount(namespace string) {
6363
)
6464
om.httpScaledObjectCounter.Add(ctx, 1, opt)
6565
}
66+
67+
func (om *OtelMetrics) RecordDeleteHTTPScaledObjectCount(namespace string) {
68+
ctx := context.Background()
69+
opt := api.WithAttributeSet(
70+
attribute.NewSet(
71+
attribute.Key("namespace").String(namespace),
72+
),
73+
)
74+
om.httpScaledObjectCounter.Add(ctx, -1, opt)
75+
}

operator/metrics/prommetrics.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616

1717
type PrometheusMetrics struct {
1818
meter api.Meter
19-
httpScaledObjectCounter api.Int64Counter
19+
httpScaledObjectCounter api.Int64UpDownCounter
2020
}
2121

2222
func NewPrometheusMetrics(options ...prometheus.Option) *PrometheusMetrics {
@@ -43,7 +43,7 @@ func NewPrometheusMetrics(options ...prometheus.Option) *PrometheusMetrics {
4343
)
4444
meter := provider.Meter(meterName)
4545

46-
httpScaledObjectCounter, err := meter.Int64Counter("operator_http_scaled_object_count", api.WithDescription("a counter of http_scaled_objects processed by the operator"))
46+
httpScaledObjectCounter, err := meter.Int64UpDownCounter("operator_http_scaled_object_count", api.WithDescription("a counter of http_scaled_objects processed by the operator"))
4747
if err != nil {
4848
log.Fatalf("could not create new Prometheus request counter: %v", err)
4949
}
@@ -63,3 +63,13 @@ func (p *PrometheusMetrics) RecordHTTPScaledObjectCount(namespace string) {
6363
)
6464
p.httpScaledObjectCounter.Add(ctx, 1, opt)
6565
}
66+
67+
func (p *PrometheusMetrics) RecordDeleteHTTPScaledObjectCount(namespace string) {
68+
ctx := context.Background()
69+
opt := api.WithAttributeSet(
70+
attribute.NewSet(
71+
attribute.Key("namespace").String(namespace),
72+
),
73+
)
74+
p.httpScaledObjectCounter.Add(ctx, -1, opt)
75+
}

operator/metrics/prommetrics_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ func TestPromRequestCountMetric(t *testing.T) {
1515
options := []promexporter.Option{promexporter.WithRegisterer(testRegistry)}
1616
testPrometheus := NewPrometheusMetrics(options...)
1717
expectedOutput := `
18-
# HELP operator_http_scaled_object_count_total a counter of http_scaled_objects processed by the operator
19-
# TYPE operator_http_scaled_object_count_total counter
20-
operator_http_scaled_object_count_total{namespace="test-namespace",otel_scope_name="keda-http-add-on-operator",otel_scope_version=""} 2
21-
operator_http_scaled_object_count_total{namespace="other-test-namespace",otel_scope_name="keda-http-add-on-operator",otel_scope_version=""} 1
18+
# HELP operator_http_scaled_object_count a counter of http_scaled_objects processed by the operator
19+
# TYPE operator_http_scaled_object_count gauge
20+
operator_http_scaled_object_count{namespace="test-namespace",otel_scope_name="keda-http-add-on-operator",otel_scope_version=""} 0
21+
operator_http_scaled_object_count{namespace="other-test-namespace",otel_scope_name="keda-http-add-on-operator",otel_scope_version=""} 1
2222
# HELP otel_scope_info Instrumentation Scope metadata
2323
# TYPE otel_scope_info gauge
2424
otel_scope_info{otel_scope_name="keda-http-add-on-operator",otel_scope_version=""} 1
@@ -28,7 +28,7 @@ func TestPromRequestCountMetric(t *testing.T) {
2828
`
2929
expectedOutputReader := strings.NewReader(expectedOutput)
3030
testPrometheus.RecordHTTPScaledObjectCount("test-namespace")
31-
testPrometheus.RecordHTTPScaledObjectCount("test-namespace")
31+
testPrometheus.RecordDeleteHTTPScaledObjectCount("test-namespace")
3232
testPrometheus.RecordHTTPScaledObjectCount("other-test-namespace")
3333
err := testutil.CollectAndCompare(testRegistry, expectedOutputReader)
3434
assert.Nil(t, err)

0 commit comments

Comments
 (0)