-
Notifications
You must be signed in to change notification settings - Fork 134
Instrument http add on operator #1328
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
base: main
Are you sure you want to change the base?
Changes from all commits
c7c7717
ff77d80
b8767b8
e46bce8
015b5c3
a1c5a02
0072119
1e610a3
f56c1bd
cbcb3f9
bb94b8f
1e8e24a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: operator | ||
spec: | ||
replicas: 1 | ||
template: | ||
spec: | ||
containers: | ||
- name: operator | ||
env: | ||
- name: OTEL_PROM_EXPORTER_ENABLED | ||
value: "true" | ||
- name: OTEL_PROM_EXPORTER_PORT | ||
value: "8080" | ||
- name: OTEL_EXPORTER_OTLP_METRICS_ENABLED | ||
value: "true" | ||
- name: OTEL_EXPORTER_OTLP_ENDPOINT | ||
value: "http://opentelemetry-collector.open-telemetry-system:4318" | ||
- name: OTEL_METRIC_EXPORT_INTERVAL | ||
value: "1" | ||
- name: OTEL_EXPORTER_OTLP_PROTOCOL | ||
value: "http" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
apiVersion: kustomize.config.k8s.io/v1beta1 | ||
kind: Kustomization | ||
resources: | ||
- deployment.yaml |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
apiVersion: v1 | ||
kind: Service | ||
metadata: | ||
name: operator-metrics | ||
spec: | ||
type: ClusterIP | ||
ports: | ||
- name: metrics | ||
protocol: TCP | ||
port: 8080 | ||
targetPort: metrics |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -19,6 +19,7 @@ package http | |||||||
import ( | ||||||||
"context" | ||||||||
"fmt" | ||||||||
"sync" | ||||||||
"time" | ||||||||
|
||||||||
kedav1alpha1 "github.com/kedacore/keda/v2/apis/keda/v1alpha1" | ||||||||
|
@@ -34,6 +35,16 @@ import ( | |||||||
httpv1alpha1 "github.com/kedacore/http-add-on/operator/apis/http/v1alpha1" | ||||||||
"github.com/kedacore/http-add-on/operator/controllers/http/config" | ||||||||
"github.com/kedacore/http-add-on/operator/controllers/util" | ||||||||
"github.com/kedacore/http-add-on/operator/metrics" | ||||||||
) | ||||||||
|
||||||||
type httpScaledObjectMetricsData struct { | ||||||||
namespace string | ||||||||
} | ||||||||
|
||||||||
var ( | ||||||||
httpScaledObjectPromMetricsMap map[string]httpScaledObjectMetricsData | ||||||||
httpScaledObjectPromMetricsLock *sync.Mutex | ||||||||
) | ||||||||
|
||||||||
// HTTPScaledObjectReconciler reconciles a HTTPScaledObject object | ||||||||
|
@@ -48,6 +59,11 @@ type HTTPScaledObjectReconciler struct { | |||||||
BaseConfig config.Base | ||||||||
} | ||||||||
|
||||||||
func init() { | ||||||||
httpScaledObjectPromMetricsMap = make(map[string]httpScaledObjectMetricsData) | ||||||||
httpScaledObjectPromMetricsLock = &sync.Mutex{} | ||||||||
} | ||||||||
|
||||||||
// +kubebuilder:rbac:groups=http.keda.sh,resources=httpscaledobjects,verbs=get;list;watch;create;update;patch;delete | ||||||||
// +kubebuilder:rbac:groups=http.keda.sh,resources=httpscaledobjects/status,verbs=get;update;patch | ||||||||
// +kubebuilder:rbac:groups=http.keda.sh,resources=httpscaledobjects/finalizers,verbs=update | ||||||||
|
@@ -78,6 +94,7 @@ func (r *HTTPScaledObjectReconciler) Reconcile(ctx context.Context, req ctrl.Req | |||||||
} | ||||||||
|
||||||||
if httpso.GetDeletionTimestamp() != nil { | ||||||||
r.updatePromMetricsOnDelete(ctx, httpso) | ||||||||
return ctrl.Result{}, finalizeScaledObject(ctx, logger, r.Client, httpso) | ||||||||
} | ||||||||
|
||||||||
|
@@ -139,6 +156,7 @@ func (r *HTTPScaledObjectReconciler) Reconcile(ctx context.Context, req ctrl.Req | |||||||
) | ||||||||
|
||||||||
// success reconciling | ||||||||
r.updatePromMetrics(ctx, httpso) | ||||||||
logger.Info("Reconcile success") | ||||||||
return ctrl.Result{}, nil | ||||||||
} | ||||||||
|
@@ -162,3 +180,35 @@ func (r *HTTPScaledObjectReconciler) SetupWithManager(mgr ctrl.Manager) error { | |||||||
))). | ||||||||
Complete(r) | ||||||||
} | ||||||||
|
||||||||
func (r *HTTPScaledObjectReconciler) updatePromMetrics(ctx context.Context, scaledObject *httpv1alpha1.HTTPScaledObject) { | ||||||||
httpScaledObjectPromMetricsLock.Lock() | ||||||||
defer httpScaledObjectPromMetricsLock.Unlock() | ||||||||
|
||||||||
namespacedName := client.ObjectKeyFromObject(scaledObject).String() | ||||||||
metricsData, ok := httpScaledObjectPromMetricsMap[namespacedName] | ||||||||
if ok { | ||||||||
metrics.RecordDeleteHTTPScaledObjectCount(scaledObject.Namespace) | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When the map entry already exists, the code calls RecordDeleteHTTPScaledObjectCount before RecordHTTPScaledObjectCount, which could lead to incorrect metrics if the namespace changed. This logic appears to be attempting to handle namespace changes, but it's unclear and potentially buggy.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||
} | ||||||||
metricsData.namespace = scaledObject.Namespace | ||||||||
|
||||||||
logger := log.FromContext(ctx, "updatePromMetrics", namespacedName) | ||||||||
logger.Info("updatePromMetrics") | ||||||||
metrics.RecordHTTPScaledObjectCount(scaledObject.Namespace) | ||||||||
|
||||||||
httpScaledObjectPromMetricsMap[namespacedName] = metricsData | ||||||||
} | ||||||||
|
||||||||
func (r *HTTPScaledObjectReconciler) updatePromMetricsOnDelete(ctx context.Context, scaledObject *httpv1alpha1.HTTPScaledObject) { | ||||||||
httpScaledObjectPromMetricsLock.Lock() | ||||||||
defer httpScaledObjectPromMetricsLock.Unlock() | ||||||||
|
||||||||
namespacedName := scaledObject.Name + scaledObject.Namespace | ||||||||
wozniakjan marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
logger := log.FromContext(ctx, "updatePromMetricsOnDelete", namespacedName) | ||||||||
logger.Info("updatePromMetricsOnDelete") | ||||||||
|
||||||||
if _, ok := httpScaledObjectPromMetricsMap[namespacedName]; ok { | ||||||||
metrics.RecordDeleteHTTPScaledObjectCount(scaledObject.Namespace) | ||||||||
} | ||||||||
delete(httpScaledObjectPromMetricsMap, namespacedName) | ||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package metrics | ||
|
||
import ( | ||
"go.opentelemetry.io/otel/exporters/prometheus" | ||
ctrlmetrics "sigs.k8s.io/controller-runtime/pkg/metrics" | ||
|
||
"github.com/kedacore/http-add-on/operator/controllers/http/config" | ||
) | ||
|
||
var ( | ||
collectors []Collector | ||
) | ||
|
||
const meterName = "keda-http-add-on-operator" | ||
|
||
type Collector interface { | ||
RecordHTTPScaledObjectCount(namespace string) | ||
RecordDeleteHTTPScaledObjectCount(namespace string) | ||
} | ||
|
||
func NewMetricsCollectors(metricsConfig *config.Metrics) { | ||
if metricsConfig.OtelPrometheusExporterEnabled { | ||
options := prometheus.WithRegisterer(ctrlmetrics.Registry) | ||
promometrics := NewPrometheusMetrics(options) | ||
collectors = append(collectors, promometrics) | ||
} | ||
|
||
if metricsConfig.OtelHTTPExporterEnabled { | ||
otelhttpmetrics := NewOtelMetrics() | ||
collectors = append(collectors, otelhttpmetrics) | ||
} | ||
} | ||
|
||
func RecordHTTPScaledObjectCount(namespace string) { | ||
for _, collector := range collectors { | ||
collector.RecordHTTPScaledObjectCount(namespace) | ||
} | ||
} | ||
|
||
func RecordDeleteHTTPScaledObjectCount(namespace string) { | ||
for _, collector := range collectors { | ||
collector.RecordDeleteHTTPScaledObjectCount(namespace) | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.