|
| 1 | +package clusterconfig |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + |
| 8 | + "github.com/openshift/insights-operator/pkg/record" |
| 9 | + "github.com/openshift/insights-operator/pkg/utils" |
| 10 | + "github.com/openshift/insights-operator/pkg/utils/marshal" |
| 11 | + "k8s.io/client-go/rest" |
| 12 | + "k8s.io/klog/v2" |
| 13 | +) |
| 14 | + |
| 15 | +const ( |
| 16 | + // metricsAlertsLinesLimit is the maximal number of lines read from monitoring Prometheus |
| 17 | + // 500 KiB of alerts is limit, one alert line has typically 450 bytes => 1137 lines. |
| 18 | + // This number has been rounded to 1000 for simplicity. |
| 19 | + // Formerly, the `500 * 1024 / 450` expression was used instead. |
| 20 | + helmMetricsAlertsLinesLimit = 1000 |
| 21 | +) |
| 22 | + |
| 23 | +func (g *Gatherer) GatherMetricsForHelmInstalls(ctx context.Context) ([]record.Record, []error) { |
| 24 | + metricsRESTClient, err := rest.RESTClientFor(g.metricsGatherKubeConfig) |
| 25 | + if err != nil { |
| 26 | + klog.Warningf("Unable to load metrics client, no metrics will be collected: %v", err) |
| 27 | + return nil, nil |
| 28 | + } |
| 29 | + |
| 30 | + return gatherMetricsForHelmInstalls(ctx, metricsRESTClient) |
| 31 | +} |
| 32 | + |
| 33 | +func gatherMetricsForHelmInstalls(ctx context.Context, metricsClient rest.Interface) ([]record.Record, []error) { |
| 34 | + data, err := metricsClient.Get().AbsPath("federate"). |
| 35 | + Param("match[]", "console_helm_installs_total"). |
| 36 | + DoRaw(ctx) |
| 37 | + if err != nil { |
| 38 | + klog.Errorf("Unable to retrieve metrics for helm installs: %v", err) |
| 39 | + return nil, []error{err} |
| 40 | + } |
| 41 | + |
| 42 | + rsp, err := metricsClient.Get().AbsPath("federate"). |
| 43 | + Param("match[]", "ALERTS"). |
| 44 | + Stream(ctx) |
| 45 | + if err != nil { |
| 46 | + klog.Errorf("Unable to retrieve most recent alerts from metrics: %v", err) |
| 47 | + return nil, []error{err} |
| 48 | + } |
| 49 | + r := utils.NewLineLimitReader(rsp, metricsAlertsLinesLimit) |
| 50 | + alerts, err := io.ReadAll(r) |
| 51 | + if err != nil && err != io.EOF { |
| 52 | + klog.Errorf("Unable to read most recent alerts from metrics: %v", err) |
| 53 | + return nil, []error{err} |
| 54 | + } |
| 55 | + |
| 56 | + remainingAlertLines, err := utils.CountLines(rsp) |
| 57 | + if err != nil && err != io.EOF { |
| 58 | + klog.Errorf("Unable to count truncated lines of alerts metric: %v", err) |
| 59 | + return nil, []error{err} |
| 60 | + } |
| 61 | + totalAlertCount := r.GetTotalLinesRead() + remainingAlertLines |
| 62 | + |
| 63 | + // # ALERTS <Total Alerts Lines>/<Alerts Line Limit> |
| 64 | + // The total number of alerts will typically be greater than the true number of alerts by 2 |
| 65 | + // because the `# TYPE ALERTS untyped` header and the final empty line are counter in. |
| 66 | + data = append(data, []byte(fmt.Sprintf("# ALERTS %d/%d\n", totalAlertCount, metricsAlertsLinesLimit))...) |
| 67 | + data = append(data, alerts...) |
| 68 | + records := []record.Record{ |
| 69 | + {Name: "config/metrics", Item: marshal.RawByte(data)}, |
| 70 | + } |
| 71 | + |
| 72 | + return records, nil |
| 73 | +} |
0 commit comments