app_metrics.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package porter_app
  2. import (
  3. "net/http"
  4. "github.com/porter-dev/porter/internal/kubernetes/prometheus"
  5. "connectrpc.com/connect"
  6. porterv1 "github.com/porter-dev/api-contracts/generated/go/porter/v1"
  7. "github.com/porter-dev/porter/internal/telemetry"
  8. "github.com/porter-dev/porter/api/server/authz"
  9. "github.com/porter-dev/porter/api/server/handlers"
  10. "github.com/porter-dev/porter/api/server/shared"
  11. "github.com/porter-dev/porter/api/server/shared/apierrors"
  12. "github.com/porter-dev/porter/api/server/shared/config"
  13. "github.com/porter-dev/porter/api/types"
  14. "github.com/porter-dev/porter/internal/models"
  15. )
  16. // AppMetricsHandler handles the /apps/metrics endpoint
  17. type AppMetricsHandler struct {
  18. handlers.PorterHandlerReadWriter
  19. authz.KubernetesAgentGetter
  20. }
  21. // NewAppMetricsHandler returns a new AppMetricsHandler
  22. func NewAppMetricsHandler(
  23. config *config.Config,
  24. decoderValidator shared.RequestDecoderValidator,
  25. writer shared.ResultWriter,
  26. ) *AppMetricsHandler {
  27. return &AppMetricsHandler{
  28. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  29. KubernetesAgentGetter: authz.NewOutOfClusterAgentGetter(config),
  30. }
  31. }
  32. // MetricsRequest is the expected request body for the /apps/metrics endpoint
  33. type MetricsRequest struct {
  34. // Deployment target of the app to query for metrics
  35. DeploymentTargetID string `schema:"deployment_target_id"`
  36. // Below is just a copy of prometheus.QueryOpts, other than namespace
  37. // the name of the metric being queried for
  38. Metric string `schema:"metric"`
  39. ShouldSum bool `schema:"shouldsum"`
  40. Kind string `schema:"kind"`
  41. PodList []string `schema:"pods"`
  42. Name string `schema:"name"`
  43. // start time (in unix timestamp) for prometheus results
  44. StartRange uint `schema:"startrange"`
  45. // end time time (in unix timestamp) for prometheus results
  46. EndRange uint `schema:"endrange"`
  47. Resolution string `schema:"resolution"`
  48. Percentile float64 `schema:"percentile"`
  49. }
  50. // ServeHTTP returns metrics for a given app in the provided deployment target
  51. func (c *AppMetricsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  52. ctx, span := telemetry.NewSpan(r.Context(), "serve-app-metrics")
  53. defer span.End()
  54. r = r.Clone(ctx)
  55. project, _ := ctx.Value(types.ProjectScope).(*models.Project)
  56. cluster, _ := ctx.Value(types.ClusterScope).(*models.Cluster)
  57. request := &MetricsRequest{}
  58. if ok := c.DecodeAndValidate(w, r, request); !ok {
  59. err := telemetry.Error(ctx, span, nil, "error decoding request")
  60. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  61. return
  62. }
  63. if request.DeploymentTargetID == "" {
  64. err := telemetry.Error(ctx, span, nil, "must provide deployment target id")
  65. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  66. return
  67. }
  68. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "deployment-target-id", Value: request.DeploymentTargetID})
  69. deploymentTargetDetailsReq := connect.NewRequest(&porterv1.DeploymentTargetDetailsRequest{
  70. ProjectId: int64(project.ID),
  71. DeploymentTargetId: request.DeploymentTargetID,
  72. })
  73. deploymentTargetDetailsResp, err := c.Config().ClusterControlPlaneClient.DeploymentTargetDetails(ctx, deploymentTargetDetailsReq)
  74. if err != nil {
  75. err := telemetry.Error(ctx, span, err, "error getting deployment target details from cluster control plane client")
  76. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  77. return
  78. }
  79. if deploymentTargetDetailsResp == nil || deploymentTargetDetailsResp.Msg == nil {
  80. err := telemetry.Error(ctx, span, err, "deployment target details resp is nil")
  81. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  82. return
  83. }
  84. if deploymentTargetDetailsResp.Msg.ClusterId != int64(cluster.ID) {
  85. err := telemetry.Error(ctx, span, err, "deployment target details resp cluster id does not match cluster id")
  86. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  87. return
  88. }
  89. namespace := deploymentTargetDetailsResp.Msg.Namespace
  90. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "namespace", Value: namespace})
  91. agent, err := c.GetAgent(r, cluster, "")
  92. if err != nil {
  93. err = telemetry.Error(ctx, span, err, "error getting k8s agent")
  94. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  95. return
  96. }
  97. // get prometheus service
  98. promSvc, found, err := prometheus.GetPrometheusService(agent.Clientset)
  99. if err != nil || !found {
  100. err = telemetry.Error(ctx, span, err, "error getting prometheus service")
  101. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  102. return
  103. }
  104. telemetry.WithAttributes(span,
  105. telemetry.AttributeKV{Key: "metric", Value: request.Metric},
  106. telemetry.AttributeKV{Key: "shouldsum", Value: request.ShouldSum},
  107. telemetry.AttributeKV{Key: "kind", Value: request.Kind},
  108. telemetry.AttributeKV{Key: "name", Value: request.Name},
  109. telemetry.AttributeKV{Key: "start-range", Value: request.StartRange},
  110. telemetry.AttributeKV{Key: "end-range", Value: request.EndRange},
  111. telemetry.AttributeKV{Key: "resolution", Value: request.Resolution},
  112. telemetry.AttributeKV{Key: "percentile", Value: request.Percentile},
  113. )
  114. queryOpts := &prometheus.QueryOpts{
  115. Metric: request.Metric,
  116. ShouldSum: request.ShouldSum,
  117. Kind: request.Kind,
  118. Name: request.Name,
  119. Namespace: namespace,
  120. StartRange: request.StartRange,
  121. EndRange: request.EndRange,
  122. Resolution: request.Resolution,
  123. Percentile: request.Percentile,
  124. }
  125. rawQuery, err := prometheus.QueryPrometheus(ctx, agent.Clientset, promSvc, queryOpts)
  126. if err != nil {
  127. err = telemetry.Error(ctx, span, err, "error querying prometheus")
  128. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  129. return
  130. }
  131. c.WriteResult(w, r, rawQuery)
  132. }