Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
19 changes: 16 additions & 3 deletions internal/controller/nodedeployment/monitoring.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,22 @@ func endpointSpec(group *seiv1alpha1.SeiNodeDeployment, interval string) map[str
"targetLabel": "chain_id",
})
}
if len(relabelings) > 0 {
ep["metricRelabelings"] = relabelings
}
// EC2-compat shim: stamp legacy labels from `pod` so dashboards that
// filter on instance_name / public_dns work for both EC2 and k8s scrapes.
// TODO(sei-protocol/sei-k8s-controller#122): remove after EC2 scrape decommission.
relabelings = append(relabelings,
map[string]any{
"action": "replace",
"sourceLabels": []any{"pod"},
"targetLabel": "instance_name",
},
map[string]any{
"action": "replace",
"sourceLabels": []any{"pod"},
"targetLabel": "public_dns",
},
)
ep["metricRelabelings"] = relabelings
return ep
}

Expand Down
50 changes: 45 additions & 5 deletions internal/controller/nodedeployment/monitoring_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func TestGenerateServiceMonitor_ChainIDRelabeling(t *testing.T) {
g.Expect(findRelabeling(relabelings, "chain_id")).To(Equal("pacific-1"))
}

func TestGenerateServiceMonitor_NoRelabelingsWhenNothingDerivable(t *testing.T) {
func TestGenerateServiceMonitor_OnlyEC2CompatWhenNothingDerivable(t *testing.T) {
g := NewWithT(t)
group := newTestGroup("role-test", "sei")
group.Spec.Monitoring = &seiv1alpha1.MonitoringConfig{
Expand All @@ -134,18 +134,58 @@ func TestGenerateServiceMonitor_NoRelabelingsWhenNothingDerivable(t *testing.T)
sm := generateServiceMonitor(group)
spec := sm.Object["spec"].(map[string]any)
ep := spec["endpoints"].([]any)[0].(map[string]any)
_, has := ep["metricRelabelings"]
g.Expect(has).To(BeFalse())
relabelings := ep["metricRelabelings"].([]any)
g.Expect(findRelabeling(relabelings, "component")).To(Equal(""))
g.Expect(findRelabeling(relabelings, "chain_id")).To(Equal(""))
g.Expect(findSourceRelabeling(relabelings, "instance_name")).To(Equal("pod"))
g.Expect(findSourceRelabeling(relabelings, "public_dns")).To(Equal("pod"))
}

func TestGenerateServiceMonitor_EC2CompatLabels(t *testing.T) {
g := NewWithT(t)
group := newTestGroup("role-test", "sei")
group.Spec.Monitoring = &seiv1alpha1.MonitoringConfig{
ServiceMonitor: &seiv1alpha1.ServiceMonitorConfig{},
}

sm := generateServiceMonitor(group)
spec := sm.Object["spec"].(map[string]any)
ep := spec["endpoints"].([]any)[0].(map[string]any)
relabelings := ep["metricRelabelings"].([]any)
g.Expect(findSourceRelabeling(relabelings, "instance_name")).To(Equal("pod"))
g.Expect(findSourceRelabeling(relabelings, "public_dns")).To(Equal("pod"))
}

// findRelabeling returns the `replacement` string for a metricRelabeling
// targeting the given label, or "" if no such rule exists.
func findRelabeling(relabelings []any, targetLabel string) string {
for _, r := range relabelings {
rule := r.(map[string]any)
if rule["targetLabel"] == targetLabel {
return rule["replacement"].(string)
if rule["targetLabel"] != targetLabel {
continue
}
repl, ok := rule["replacement"].(string)
if !ok {
continue
}
return repl
}
return ""
}

// findSourceRelabeling returns the first sourceLabels entry for a metricRelabeling
// targeting the given label, or "" if no such rule exists.
func findSourceRelabeling(relabelings []any, targetLabel string) string {
for _, r := range relabelings {
rule := r.(map[string]any)
if rule["targetLabel"] != targetLabel {
continue
}
src, ok := rule["sourceLabels"].([]any)
if !ok || len(src) == 0 {
continue
}
return src[0].(string)
}
return ""
}
Loading