|
| 1 | +// |
| 2 | +// Copyright 2024 The Chainloop Authors. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | + |
| 16 | +package service |
| 17 | + |
| 18 | +import ( |
| 19 | + "fmt" |
| 20 | + "net/http" |
| 21 | + |
| 22 | + "github.com/chainloop-dev/chainloop/app/controlplane/pkg/biz" |
| 23 | + "github.com/chainloop-dev/chainloop/app/controlplane/pkg/jwt/apitoken" |
| 24 | + |
| 25 | + jwtmiddleware "github.com/go-kratos/kratos/v2/middleware/auth/jwt" |
| 26 | + "github.com/gorilla/mux" |
| 27 | + "github.com/prometheus/common/expfmt" |
| 28 | +) |
| 29 | + |
| 30 | +const ( |
| 31 | + // PrometheusMetricsPath is the path for the Prometheus metrics |
| 32 | + PrometheusMetricsPath = "/prom/{org_name}/metrics" |
| 33 | +) |
| 34 | + |
| 35 | +// PrometheusService is the prometheus service |
| 36 | +type PrometheusService struct { |
| 37 | + *service |
| 38 | + // Use Cases |
| 39 | + prometheusUseCase *biz.PrometheusUseCase |
| 40 | + organizationUseCase *biz.OrganizationUseCase |
| 41 | +} |
| 42 | + |
| 43 | +// NewPrometheusService creates a new prometheus service |
| 44 | +func NewPrometheusService(orgUseCase *biz.OrganizationUseCase, prometheusUseCase *biz.PrometheusUseCase, opts ...NewOpt) *PrometheusService { |
| 45 | + return &PrometheusService{ |
| 46 | + organizationUseCase: orgUseCase, |
| 47 | + prometheusUseCase: prometheusUseCase, |
| 48 | + service: newService(opts...), |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +// MetricsRequestHandler is the handler for the metrics request. It fetches the Prometheus registry |
| 53 | +// and if found, retrieves all metrics in Prometheus format. |
| 54 | +func (p *PrometheusService) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 55 | + // Extract org_name from the URL path |
| 56 | + orgName, ok := mux.Vars(r)["org_name"] |
| 57 | + if !ok { |
| 58 | + http.Error(w, "Error extracting organization name from URL path", http.StatusBadRequest) |
| 59 | + return |
| 60 | + } |
| 61 | + |
| 62 | + // Extracts the organization name from the request |
| 63 | + rawClaims, ok := jwtmiddleware.FromContext(r.Context()) |
| 64 | + if !ok { |
| 65 | + http.Error(w, "Error extracting claims from context", http.StatusInternalServerError) |
| 66 | + return |
| 67 | + } |
| 68 | + |
| 69 | + apiTokenClaims, ok := rawClaims.(*apitoken.CustomClaims) |
| 70 | + if !ok { |
| 71 | + http.Error(w, "Error extracting API Token claims", http.StatusInternalServerError) |
| 72 | + return |
| 73 | + } |
| 74 | + |
| 75 | + // Check if the organization in the API Token matches the one in the request |
| 76 | + if apiTokenClaims.OrgName != orgName { |
| 77 | + http.Error(w, fmt.Sprintf("Organization [%v] on API Token does not match the organization in the request", apiTokenClaims.OrgName), http.StatusBadRequest) |
| 78 | + return |
| 79 | + } |
| 80 | + |
| 81 | + // Checks if the organization has a Prometheus integration activated |
| 82 | + if !p.prometheusUseCase.OrganizationHasRegistry(orgName) { |
| 83 | + http.Error(w, "Organization does not have a Prometheus integration activated", http.StatusNotFound) |
| 84 | + return |
| 85 | + } |
| 86 | + |
| 87 | + // Fetches the Prometheus registry for the organization |
| 88 | + reg := p.prometheusUseCase.GetRegistryByOrganizationName(orgName) |
| 89 | + if reg == nil { |
| 90 | + http.Error(w, "Error fetching Prometheus registry", http.StatusInternalServerError) |
| 91 | + return |
| 92 | + } |
| 93 | + |
| 94 | + // Gathers the metrics |
| 95 | + gather, err := reg.Gather() |
| 96 | + if err != nil { |
| 97 | + http.Error(w, "Error gathering metrics", http.StatusInternalServerError) |
| 98 | + return |
| 99 | + } |
| 100 | + |
| 101 | + // Encode and write the metrics to the response |
| 102 | + contentType := expfmt.Negotiate(r.Header) |
| 103 | + w.Header().Set("Content-Type", string(contentType)) |
| 104 | + |
| 105 | + enc := expfmt.NewEncoder(w, contentType) |
| 106 | + for _, mf := range gather { |
| 107 | + if err := enc.Encode(mf); err != nil { |
| 108 | + http.Error(w, "Error encoding metrics", http.StatusInternalServerError) |
| 109 | + return |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments