logs_apply_v2.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. package porter_app
  2. import (
  3. "fmt"
  4. "net/http"
  5. "time"
  6. "github.com/porter-dev/porter/api/server/authz"
  7. "github.com/porter-dev/porter/api/server/handlers"
  8. "github.com/porter-dev/porter/api/server/shared"
  9. "github.com/porter-dev/porter/api/server/shared/apierrors"
  10. "github.com/porter-dev/porter/api/server/shared/config"
  11. "github.com/porter-dev/porter/api/server/shared/requestutils"
  12. "github.com/porter-dev/porter/api/types"
  13. "github.com/porter-dev/porter/internal/deployment_target"
  14. porter_agent "github.com/porter-dev/porter/internal/kubernetes/porter_agent/v2"
  15. "github.com/porter-dev/porter/internal/models"
  16. "github.com/porter-dev/porter/internal/porter_app"
  17. "github.com/porter-dev/porter/internal/telemetry"
  18. )
  19. // AppLogsHandler handles the /apps/logs endpoint
  20. type AppLogsHandler struct {
  21. handlers.PorterHandlerReadWriter
  22. authz.KubernetesAgentGetter
  23. }
  24. // NewAppLogsHandler returns a new AppLogsHandler
  25. func NewAppLogsHandler(
  26. config *config.Config,
  27. decoderValidator shared.RequestDecoderValidator,
  28. writer shared.ResultWriter,
  29. ) *AppLogsHandler {
  30. return &AppLogsHandler{
  31. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  32. KubernetesAgentGetter: authz.NewOutOfClusterAgentGetter(config),
  33. }
  34. }
  35. // AppLogsRequest represents the accepted fields on a request to the /apps/logs endpoint
  36. type AppLogsRequest struct {
  37. DeploymentTargetID string `schema:"deployment_target_id"`
  38. DeploymentTargetName string `schema:"deployment_target_name"`
  39. ServiceName string `schema:"service_name"`
  40. AppID uint `schema:"app_id"`
  41. Limit uint `schema:"limit"`
  42. StartRange time.Time `schema:"start_range,omitempty"`
  43. EndRange time.Time `schema:"end_range,omitempty"`
  44. SearchParam string `schema:"search_param"`
  45. Direction string `schema:"direction"`
  46. AppRevisionID string `schema:"app_revision_id"`
  47. JobRunName string `schema:"job_run_name"`
  48. }
  49. const (
  50. lokiLabel_PorterAppName = "porter_run_app_name"
  51. lokiLabel_PorterAppID = "porter_run_app_id"
  52. lokiLabel_PorterServiceName = "porter_run_service_name"
  53. lokiLabel_PorterAppRevisionID = "porter_run_app_revision_id"
  54. lokiLabel_DeploymentTargetId = "porter_run_deployment_target_id"
  55. lokiLabel_JobRunName = "job_name"
  56. lokiLabel_Namespace = "namespace"
  57. )
  58. const defaultLogLineLimit = 1000
  59. // AppLogsResponse represents the response to the /apps/logs endpoint
  60. type AppLogsResponse struct {
  61. BackwardContinueTime *time.Time `json:"backward_continue_time,omitempty"`
  62. ForwardContinueTime *time.Time `json:"forward_continue_time,omitempty"`
  63. Logs []porter_app.StructuredLog `json:"logs"`
  64. }
  65. // ServeHTTP gets logs for a given app, service, and deployment target
  66. func (c *AppLogsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  67. ctx, span := telemetry.NewSpan(r.Context(), "serve-app-logs")
  68. defer span.End()
  69. r = r.Clone(ctx)
  70. project, _ := ctx.Value(types.ProjectScope).(*models.Project)
  71. cluster, _ := ctx.Value(types.ClusterScope).(*models.Cluster)
  72. request := &AppLogsRequest{}
  73. if ok := c.DecodeAndValidate(w, r, request); !ok {
  74. err := telemetry.Error(ctx, span, nil, "invalid request")
  75. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  76. return
  77. }
  78. appName, reqErr := requestutils.GetURLParamString(r, types.URLParamPorterAppName)
  79. if reqErr != nil {
  80. err := telemetry.Error(ctx, span, reqErr, "porter app name not found in request")
  81. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  82. return
  83. }
  84. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "app-name", Value: appName})
  85. if request.ServiceName == "" {
  86. err := telemetry.Error(ctx, span, nil, "must provide service name")
  87. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest))
  88. return
  89. }
  90. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "service-name", Value: request.ServiceName})
  91. deploymentTargetName := request.DeploymentTargetName
  92. if request.DeploymentTargetName == "" && request.DeploymentTargetID == "" {
  93. defaultDeploymentTarget, err := defaultDeploymentTarget(ctx, defaultDeploymentTargetInput{
  94. ProjectID: project.ID,
  95. ClusterID: cluster.ID,
  96. ClusterControlPlaneClient: c.Config().ClusterControlPlaneClient,
  97. })
  98. if err != nil {
  99. err := telemetry.Error(ctx, span, err, "error getting default deployment target")
  100. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  101. return
  102. }
  103. deploymentTargetName = defaultDeploymentTarget.Name
  104. }
  105. telemetry.WithAttributes(span,
  106. telemetry.AttributeKV{Key: "deployment-target-name", Value: deploymentTargetName},
  107. telemetry.AttributeKV{Key: "deployment-target-id", Value: request.DeploymentTargetID},
  108. )
  109. deploymentTarget, err := deployment_target.DeploymentTargetDetails(ctx, deployment_target.DeploymentTargetDetailsInput{
  110. ProjectID: int64(project.ID),
  111. ClusterID: int64(cluster.ID),
  112. DeploymentTargetID: request.DeploymentTargetID,
  113. DeploymentTargetName: deploymentTargetName,
  114. CCPClient: c.Config().ClusterControlPlaneClient,
  115. })
  116. if err != nil {
  117. err := telemetry.Error(ctx, span, err, "error getting deployment target details")
  118. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  119. return
  120. }
  121. namespace := deploymentTarget.Namespace
  122. telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "namespace", Value: namespace})
  123. startRange := request.StartRange
  124. if request.StartRange.IsZero() {
  125. dayAgo := time.Now().Add(-24 * time.Hour).UTC()
  126. startRange = dayAgo
  127. }
  128. endRange := request.EndRange
  129. if request.EndRange.IsZero() {
  130. endRange = time.Now().UTC()
  131. }
  132. limit := request.Limit
  133. if request.Limit == 0 {
  134. limit = defaultLogLineLimit
  135. }
  136. direction := request.Direction
  137. if request.Direction == "" {
  138. direction = "backward"
  139. }
  140. telemetry.WithAttributes(span,
  141. telemetry.AttributeKV{Key: "start-range", Value: request.StartRange.String()},
  142. telemetry.AttributeKV{Key: "end-range", Value: request.EndRange.String()},
  143. telemetry.AttributeKV{Key: "limit", Value: limit},
  144. telemetry.AttributeKV{Key: "direction", Value: direction},
  145. )
  146. k8sAgent, err := c.GetAgent(r, cluster, "")
  147. if err != nil {
  148. _ = telemetry.Error(ctx, span, err, "unable to get agent")
  149. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(fmt.Errorf("unable to get agent"), http.StatusInternalServerError))
  150. return
  151. }
  152. agentSvc, err := porter_agent.GetAgentService(k8sAgent.Clientset)
  153. if err != nil {
  154. _ = telemetry.Error(ctx, span, err, "unable to get agent service")
  155. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(fmt.Errorf("unable to get agent service"), http.StatusInternalServerError))
  156. return
  157. }
  158. matchLabels := map[string]string{
  159. lokiLabel_Namespace: namespace,
  160. lokiLabel_PorterAppName: appName,
  161. lokiLabel_DeploymentTargetId: request.DeploymentTargetID,
  162. }
  163. if request.ServiceName != "all" {
  164. matchLabels[lokiLabel_PorterServiceName] = request.ServiceName
  165. }
  166. if request.AppRevisionID != "" {
  167. matchLabels[lokiLabel_PorterAppRevisionID] = request.AppRevisionID
  168. }
  169. if request.JobRunName != "" {
  170. matchLabels[lokiLabel_JobRunName] = request.JobRunName
  171. }
  172. logRequest := &types.LogRequest{
  173. Limit: limit,
  174. StartRange: &startRange,
  175. EndRange: &endRange,
  176. MatchLabels: matchLabels,
  177. Direction: direction,
  178. SearchParam: request.SearchParam,
  179. }
  180. logs, err := porter_agent.Logs(ctx, k8sAgent.Clientset, agentSvc, logRequest)
  181. if err != nil {
  182. _ = telemetry.Error(ctx, span, err, "unable to get logs")
  183. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(fmt.Errorf("unable to get logs"), http.StatusInternalServerError))
  184. return
  185. }
  186. if logs == nil {
  187. err := telemetry.Error(ctx, span, nil, "logs response is nil")
  188. c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusInternalServerError))
  189. return
  190. }
  191. res := AppLogsResponse{
  192. Logs: porter_app.AgentLogToStructuredLog(logs.Logs),
  193. ForwardContinueTime: logs.ForwardContinueTime,
  194. BackwardContinueTime: logs.BackwardContinueTime,
  195. }
  196. c.WriteResult(w, r, res)
  197. }