Skip to content

Commit

Permalink
fix: crane uses port/targetPort in serviceMonitors for metrics (#964)
Browse files Browse the repository at this point in the history
  • Loading branch information
nandor-magyar authored Apr 19, 2024
1 parent 22614cd commit 995230c
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 28 deletions.
4 changes: 3 additions & 1 deletion golang/api/v1/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,10 @@ type ContainerConfig struct {
}

type Metrics struct {
// Path the path to be scraped, if not defined /metrics is used
Path string `json:"path"`
Port string `json:"port"`
// Port exposed port of the service where metrics are available
Port int `json:"port"`
}

func (c *ContainerConfig) Strings(appConfig *config.CommonConfiguration) []string {
Expand Down
2 changes: 1 addition & 1 deletion golang/internal/mapper/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ func mapCraneConfig(crane *agent.CraneContainerConfig, containerConfig *v1.Conta
if crane.Metrics != nil {
containerConfig.Metrics = &v1.Metrics{
Path: crane.Metrics.Path,
Port: crane.Metrics.Path,
Port: int(crane.Metrics.Port),
}
}
}
Expand Down
29 changes: 15 additions & 14 deletions golang/pkg/crane/k8s/service_monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

smv1 "github.com/prometheus-operator/prometheus-operator/pkg/client/applyconfiguration/monitoring/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
)

type ServiceMonitor struct {
Expand Down Expand Up @@ -58,28 +59,28 @@ func (sm *ServiceMonitor) Deploy(namespace, serviceName string, metricParams v1.
return err
}

portName := metricParams.Port

if portName == "" {
portName = firstPort
}

metricsPath := metricParams.Path
if metricsPath == "" {
metricsPath = "/metrics"
}

endpoint := smv1.Endpoint().WithPath(metricsPath)
if metricParams.Port != 0 {
endpoint = endpoint.WithTargetPort(intstr.FromInt(metricParams.Port))
} else {
endpoint = endpoint.WithPort(firstPort)
}

smApplyConfig := smv1.ServiceMonitor(serviceName, namespace).
WithSpec(smv1.ServiceMonitorSpec().
WithEndpoints(
smv1.Endpoint().WithPort(portName).WithPath(metricsPath),
).WithSelector(
metav1.LabelSelector{
MatchLabels: map[string]string{
"app": serviceName,
WithEndpoints(endpoint).
WithSelector(
metav1.LabelSelector{
MatchLabels: map[string]string{
"app": serviceName,
},
},
},
))
))

_, err = sm.ClientSet.MonitoringV1().ServiceMonitors(namespace).Apply(sm.Ctx, smApplyConfig, metav1.ApplyOptions{
FieldManager: sm.appConfig.FieldManagerName,
Expand Down
2 changes: 1 addition & 1 deletion golang/pkg/crane/k8s/service_monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func TestMain(m *testing.M) {
func TestServiceMonitorSpawning(t *testing.T) {
m, err := k8s.NewServiceMonitor(context.Background(), k8s.NewClient(getTestConfig()))
assert.Nil(t, err, "client is spawned without errors")
err = m.Deploy("crane-sm-test", "test-sm", v1.Metrics{Path: "/metrics", Port: "tcp-metrics"}, "")
err = m.Deploy("crane-sm-test", "test-sm", v1.Metrics{Path: "/metrics", Port: 8080}, "")
assert.Nil(t, err, "no errors expected with default parameters")
}

Expand Down
8 changes: 4 additions & 4 deletions protobuf/go/agent/agent.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion protobuf/proto/agent.proto
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ message Marker {
}

message Metrics {
string port = 1;
int32 port = 1;
string path = 2;
}

Expand Down
2 changes: 1 addition & 1 deletion web/crux/proto/agent.proto
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ message Marker {
}

message Metrics {
string port = 1;
int32 port = 1;
string path = 2;
}

Expand Down
2 changes: 1 addition & 1 deletion web/crux/src/app/deploy/deploy.mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ export default class DeployMapper {
metrics: config.metrics?.enabled
? {
path: config.metrics.path ?? null,
port: config.metrics.port?.toString() ?? null,
port: config.metrics.port ?? null,
}
: null,
}
Expand Down
8 changes: 4 additions & 4 deletions web/crux/src/grpc/protobuf/proto/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ export interface Marker_IngressEntry {
}

export interface Metrics {
port: string
port: number
path: string
}

Expand Down Expand Up @@ -1049,17 +1049,17 @@ export const Marker_IngressEntry = {
}

function createBaseMetrics(): Metrics {
return { port: '', path: '' }
return { port: 0, path: '' }
}

export const Metrics = {
fromJSON(object: any): Metrics {
return { port: isSet(object.port) ? String(object.port) : '', path: isSet(object.path) ? String(object.path) : '' }
return { port: isSet(object.port) ? Number(object.port) : 0, path: isSet(object.path) ? String(object.path) : '' }
},

toJSON(message: Metrics): unknown {
const obj: any = {}
message.port !== undefined && (obj.port = message.port)
message.port !== undefined && (obj.port = Math.round(message.port))
message.path !== undefined && (obj.path = message.path)
return obj
},
Expand Down

0 comments on commit 995230c

Please sign in to comment.