From 62e95fd63c3064ee0b8092d01289c5e2e4b7398f Mon Sep 17 00:00:00 2001 From: Jose Diaz-Gonzalez Date: Wed, 20 Sep 2023 12:08:11 -0400 Subject: [PATCH] feat: add telemetry for the cluster status endpoint Without this, internal service errors get swallowed up. --- api/server/handlers/cluster/cluster_status.go | 25 +++++++++++++++---- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/api/server/handlers/cluster/cluster_status.go b/api/server/handlers/cluster/cluster_status.go index cc4b013e012..030454f12a5 100644 --- a/api/server/handlers/cluster/cluster_status.go +++ b/api/server/handlers/cluster/cluster_status.go @@ -13,6 +13,7 @@ import ( "github.com/porter-dev/porter/api/server/shared/config" "github.com/porter-dev/porter/api/types" "github.com/porter-dev/porter/internal/models" + "github.com/porter-dev/porter/internal/telemetry" ) type ClusterStatusHandler struct { @@ -40,8 +41,14 @@ type ClusterStatusResponse struct { } func (c *ClusterStatusHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { - ctx := r.Context() + ctx, span := telemetry.NewSpan(r.Context(), "serve-cluster-status") + defer span.End() + cluster, _ := ctx.Value(types.ClusterScope).(*models.Cluster) + telemetry.WithAttributes(span, + telemetry.AttributeKV{Key: "project-id", Value: cluster.ProjectID}, + telemetry.AttributeKV{Key: "cluster-id", Value: cluster.ID}, + ) req := connect.NewRequest(&porterv1.ClusterStatusRequest{ ProjectId: int64(cluster.ProjectID), @@ -49,13 +56,15 @@ func (c *ClusterStatusHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) }) status, err := c.Config().ClusterControlPlaneClient.ClusterStatus(ctx, req) if err != nil { - e := fmt.Errorf("unable to retrieve status for cluster: %w", err) - c.HandleAPIError(w, r, apierrors.NewErrInternal(e)) + err := fmt.Errorf("unable to retrieve status for cluster: %w", err) + err = telemetry.Error(ctx, span, err, err.Error()) + c.HandleAPIError(w, r, apierrors.NewErrInternal(err)) return } if status.Msg == nil { - e := fmt.Errorf("unable to parse status for cluster: %w", err) - c.HandleAPIError(w, r, apierrors.NewErrInternal(e)) + err := fmt.Errorf("unable to parse status for cluster: %w", err) + err = telemetry.Error(ctx, span, err, err.Error()) + c.HandleAPIError(w, r, apierrors.NewErrInternal(err)) return } statusResp := status.Msg @@ -68,6 +77,12 @@ func (c *ClusterStatusHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) IsControlPlaneReady: statusResp.ControlPlaneStatus, } + telemetry.WithAttributes(span, + telemetry.AttributeKV{Key: "cluster-phase", Value: statusResp.Phase}, + telemetry.AttributeKV{Key: "cluster-infra-status", Value: statusResp.InfrastructureStatus}, + telemetry.AttributeKV{Key: "cluster-control-plane-status", Value: statusResp.ControlPlaneStatus}, + ) + c.WriteResult(w, r, resp) w.WriteHeader(http.StatusOK) }