You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ac...@apache.org on 2022/04/20 16:31:06 UTC

[camel-k] branch main updated: chore: Log build duration metrics

This is an automated email from the ASF dual-hosted git repository.

acosentino pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-k.git


The following commit(s) were added to refs/heads/main by this push:
     new 8ce7ae1db chore: Log build duration metrics
8ce7ae1db is described below

commit 8ce7ae1db4a4d0287daccf3f68b2198a0d97ceac
Author: Christoph Deppisch <cd...@redhat.com>
AuthorDate: Tue Apr 19 16:40:48 2022 +0200

    chore: Log build duration metrics
    
    In addition to exposing the build duration metrics via Prometheus also add a log statement that prints the build duration and build queue duration to the operator logs (similar to how time to first readiness is printed already). Users can easily correlate such logs via integration name when reviewing the operator logs.
    
    Need to copy the creator labels of the IntegrationKit to the Build CR in order to get the correlated integration name and namespace as log value.
---
 pkg/controller/build/metrics.go         | 27 ++++++++++++++++++++++++---
 pkg/controller/build/monitor_pod.go     |  7 +++++--
 pkg/controller/build/monitor_routine.go |  5 ++++-
 pkg/controller/build/schedule.go        |  4 +++-
 4 files changed, 36 insertions(+), 7 deletions(-)

diff --git a/pkg/controller/build/metrics.go b/pkg/controller/build/metrics.go
index a43c29d5a..9bdfaf9ea 100644
--- a/pkg/controller/build/metrics.go
+++ b/pkg/controller/build/metrics.go
@@ -26,6 +26,7 @@ import (
 	"github.com/prometheus/client_golang/prometheus"
 
 	v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
+	corev1 "k8s.io/api/core/v1"
 )
 
 const (
@@ -88,12 +89,23 @@ func init() {
 	metrics.Registry.MustRegister(buildDuration, buildRecovery, queueDuration)
 }
 
-func observeBuildQueueDuration(build *v1.Build) {
+func observeBuildQueueDuration(build *v1.Build, creator *corev1.ObjectReference) {
+	duration := time.Since(getBuildQueuingTime(build))
+
+	requestName := build.Name
+	requestNamespace := build.Namespace
+	if creator != nil {
+		requestName = creator.Name
+		requestNamespace = creator.Namespace
+	}
+
+	Log.WithValues("request-namespace", requestNamespace, "request-name", requestName, "build-queue-duration", duration.Seconds()).
+		ForBuild(build).Infof("Build queue duration %s", duration)
 	queueDuration.WithLabelValues(build.Labels[v1.IntegrationKitLayoutLabel]).
-		Observe(time.Since(getBuildQueuingTime(build)).Seconds())
+		Observe(duration.Seconds())
 }
 
-func observeBuildResult(build *v1.Build, phase v1.BuildPhase, duration time.Duration) {
+func observeBuildResult(build *v1.Build, phase v1.BuildPhase, creator *corev1.ObjectReference, duration time.Duration) {
 	attempt, attemptMax := getBuildAttemptFor(build)
 
 	if phase == v1.BuildPhaseFailed && attempt >= attemptMax {
@@ -105,6 +117,15 @@ func observeBuildResult(build *v1.Build, phase v1.BuildPhase, duration time.Dura
 	resultLabel := phase.String()
 	typeLabel := build.Labels[v1.IntegrationKitLayoutLabel]
 
+	requestName := build.Name
+	requestNamespace := build.Namespace
+	if creator != nil {
+		requestName = creator.Name
+		requestNamespace = creator.Namespace
+	}
+
+	Log.WithValues("request-namespace", requestNamespace, "request-name", requestName, "build-attempt", float64(attempt), "build-result", resultLabel, "build-duration", duration.Seconds()).
+		ForBuild(build).Infof("Build duration %s", duration)
 	buildRecovery.WithLabelValues(resultLabel, typeLabel).Observe(float64(attempt))
 	buildDuration.WithLabelValues(resultLabel, typeLabel).Observe(duration.Seconds())
 }
diff --git a/pkg/controller/build/monitor_pod.go b/pkg/controller/build/monitor_pod.go
index 960fd6f04..baaa5800c 100644
--- a/pkg/controller/build/monitor_pod.go
+++ b/pkg/controller/build/monitor_pod.go
@@ -35,6 +35,7 @@ import (
 	"github.com/pkg/errors"
 
 	v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
+	"github.com/apache/camel-k/pkg/util/kubernetes"
 )
 
 const timeoutAnnotation = "camel.apache.org/timeout"
@@ -121,8 +122,9 @@ func (action *monitorPodAction) Handle(ctx context.Context, build *v1.Build) (*v
 		duration := finishedAt.Sub(build.Status.StartedAt.Time)
 		build.Status.Duration = duration.String()
 
+		buildCreator := kubernetes.GetCamelCreator(build)
 		// Account for the Build metrics
-		observeBuildResult(build, build.Status.Phase, duration)
+		observeBuildResult(build, build.Status.Phase, buildCreator, duration)
 
 		for _, task := range build.Spec.Tasks {
 			if t := task.Buildah; t != nil {
@@ -166,8 +168,9 @@ func (action *monitorPodAction) Handle(ctx context.Context, build *v1.Build) (*v
 		duration := finishedAt.Sub(build.Status.StartedAt.Time)
 		build.Status.Duration = duration.String()
 
+		buildCreator := kubernetes.GetCamelCreator(build)
 		// Account for the Build metrics
-		observeBuildResult(build, build.Status.Phase, duration)
+		observeBuildResult(build, build.Status.Phase, buildCreator, duration)
 	}
 
 	return build, nil
diff --git a/pkg/controller/build/monitor_routine.go b/pkg/controller/build/monitor_routine.go
index 92e55af2c..055d960bd 100644
--- a/pkg/controller/build/monitor_routine.go
+++ b/pkg/controller/build/monitor_routine.go
@@ -34,6 +34,7 @@ import (
 	v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
 	"github.com/apache/camel-k/pkg/builder"
 	"github.com/apache/camel-k/pkg/event"
+	"github.com/apache/camel-k/pkg/util/kubernetes"
 	"github.com/apache/camel-k/pkg/util/patch"
 )
 
@@ -174,8 +175,10 @@ tasks:
 
 	duration := metav1.Now().Sub(build.Status.StartedAt.Time)
 	status.Duration = duration.String()
+
+	buildCreator := kubernetes.GetCamelCreator(build)
 	// Account for the Build metrics
-	observeBuildResult(build, status.Phase, duration)
+	observeBuildResult(build, status.Phase, buildCreator, duration)
 
 	_ = action.updateBuildStatus(ctx, build, status)
 }
diff --git a/pkg/controller/build/schedule.go b/pkg/controller/build/schedule.go
index 6f9e4bd5a..12e879d61 100644
--- a/pkg/controller/build/schedule.go
+++ b/pkg/controller/build/schedule.go
@@ -29,6 +29,7 @@ import (
 
 	v1 "github.com/apache/camel-k/pkg/apis/camel/v1"
 	"github.com/apache/camel-k/pkg/event"
+	"github.com/apache/camel-k/pkg/util/kubernetes"
 )
 
 func newScheduleAction(reader ctrl.Reader) Action {
@@ -116,8 +117,9 @@ func (action *scheduleAction) toPendingPhase(ctx context.Context, build *v1.Buil
 		return err
 	}
 
+	buildCreator := kubernetes.GetCamelCreator(build)
 	// Report the duration the Build has been waiting in the build queue
-	observeBuildQueueDuration(build)
+	observeBuildQueueDuration(build, buildCreator)
 
 	return nil
 }