|
| 1 | +package uiplugin |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/perses/perses/go-sdk/common" |
| 8 | + "github.com/perses/perses/go-sdk/dashboard" |
| 9 | + "github.com/perses/perses/go-sdk/panel" |
| 10 | + panelgroup "github.com/perses/perses/go-sdk/panel-group" |
| 11 | + listvariable "github.com/perses/perses/go-sdk/variable/list-variable" |
| 12 | + "github.com/perses/plugins/prometheus/sdk/go/query" |
| 13 | + labelvalues "github.com/perses/plugins/prometheus/sdk/go/variable/label-values" |
| 14 | + table "github.com/perses/plugins/table/sdk/go" |
| 15 | + timeseries "github.com/perses/plugins/timeserieschart/sdk/go" |
| 16 | + persesv1alpha1 "github.com/rhobs/perses-operator/api/v1alpha1" |
| 17 | + persesv1 "github.com/rhobs/perses/pkg/model/api/v1" |
| 18 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 19 | +) |
| 20 | + |
| 21 | +func withServiceMetrics(variableMatchers string) dashboard.Option { |
| 22 | + return dashboard.AddPanelGroup("Service Metrics", |
| 23 | + panelgroup.PanelsPerLine(3), |
| 24 | + panelgroup.AddPanel("Request rate", |
| 25 | + timeseries.Chart(), |
| 26 | + panel.AddQuery( |
| 27 | + query.PromQL( |
| 28 | + fmt.Sprintf("sum(rate(traces_span_metrics_calls{%s}[$__rate_interval]))", variableMatchers), |
| 29 | + query.SeriesNameFormat("req/s"), |
| 30 | + ), |
| 31 | + ), |
| 32 | + ), |
| 33 | + panelgroup.AddPanel("Error rate", |
| 34 | + timeseries.Chart(), |
| 35 | + panel.AddQuery( |
| 36 | + query.PromQL( |
| 37 | + fmt.Sprintf("sum(rate(traces_span_metrics_calls{%s, status_code=\"STATUS_CODE_ERROR\"}[$__rate_interval])) or vector(0)", variableMatchers), |
| 38 | + query.SeriesNameFormat("error/s"), |
| 39 | + ), |
| 40 | + ), |
| 41 | + ), |
| 42 | + panelgroup.AddPanel("Duration", |
| 43 | + timeseries.Chart( |
| 44 | + timeseries.WithYAxis(timeseries.YAxis{ |
| 45 | + Format: &common.Format{ |
| 46 | + Unit: string(common.MilliSecondsUnit), |
| 47 | + }, |
| 48 | + }), |
| 49 | + timeseries.WithLegend(timeseries.Legend{ |
| 50 | + Position: timeseries.BottomPosition, |
| 51 | + }), |
| 52 | + ), |
| 53 | + panel.AddQuery( |
| 54 | + query.PromQL( |
| 55 | + fmt.Sprintf("histogram_quantile(.95, sum(rate(traces_span_metrics_duration_bucket{%s}[$__rate_interval])) by (le))", variableMatchers), |
| 56 | + query.SeriesNameFormat("95th"), |
| 57 | + ), |
| 58 | + ), |
| 59 | + panel.AddQuery( |
| 60 | + query.PromQL( |
| 61 | + fmt.Sprintf("histogram_quantile(.75, sum(rate(traces_span_metrics_duration_bucket{%s}[$__rate_interval])) by (le))", variableMatchers), |
| 62 | + query.SeriesNameFormat("75th"), |
| 63 | + ), |
| 64 | + ), |
| 65 | + panel.AddQuery( |
| 66 | + query.PromQL( |
| 67 | + fmt.Sprintf("histogram_quantile(.50, sum(rate(traces_span_metrics_duration_bucket{%s}[$__rate_interval])) by (le))", variableMatchers), |
| 68 | + query.SeriesNameFormat("50th"), |
| 69 | + ), |
| 70 | + ), |
| 71 | + ), |
| 72 | + ) |
| 73 | +} |
| 74 | + |
| 75 | +func withOperationMetrics(variableMatchers string) dashboard.Option { |
| 76 | + return dashboard.AddPanelGroup("Operations", |
| 77 | + panelgroup.PanelsPerLine(1), |
| 78 | + panelgroup.AddPanel("Operation metrics", |
| 79 | + table.Table( |
| 80 | + table.Transform([]common.Transform{ |
| 81 | + { |
| 82 | + Kind: common.MergeSeriesKind, |
| 83 | + Spec: common.MergeSeriesSpec{}, |
| 84 | + }, |
| 85 | + }), |
| 86 | + table.WithColumnSettings([]table.ColumnSettings{ |
| 87 | + { |
| 88 | + Name: "span_name", |
| 89 | + Header: "Name", |
| 90 | + EnableSorting: true, |
| 91 | + }, |
| 92 | + { |
| 93 | + Name: "value #1", |
| 94 | + Header: "Request rate", |
| 95 | + Format: &common.Format{ |
| 96 | + Unit: string(common.RequestsPerSecondsUnit), |
| 97 | + DecimalPlaces: 3, |
| 98 | + }, |
| 99 | + }, |
| 100 | + { |
| 101 | + Name: "value #2", |
| 102 | + Header: "Error rate", |
| 103 | + Format: &common.Format{ |
| 104 | + Unit: string(common.DecimalUnit), |
| 105 | + DecimalPlaces: 3, |
| 106 | + }, |
| 107 | + }, |
| 108 | + { |
| 109 | + Name: "value #3", |
| 110 | + Header: "Duration", |
| 111 | + Format: &common.Format{ |
| 112 | + Unit: string(common.MilliSecondsUnit), |
| 113 | + DecimalPlaces: 3, |
| 114 | + }, |
| 115 | + }, |
| 116 | + { |
| 117 | + Name: "timestamp", |
| 118 | + Hide: true, |
| 119 | + }, |
| 120 | + }), |
| 121 | + ), |
| 122 | + panel.AddQuery( |
| 123 | + query.PromQL( |
| 124 | + fmt.Sprintf("sum(rate(traces_span_metrics_calls{%s}[$__rate_interval])) by (span_name) > 0", variableMatchers), |
| 125 | + query.SeriesNameFormat("req/s"), |
| 126 | + ), |
| 127 | + ), |
| 128 | + panel.AddQuery( |
| 129 | + query.PromQL( |
| 130 | + fmt.Sprintf("sum(rate(traces_span_metrics_calls{%s, status_code=\"STATUS_CODE_ERROR\"}[$__rate_interval])) by (span_name) > 0", variableMatchers), |
| 131 | + query.SeriesNameFormat("Error rate"), |
| 132 | + ), |
| 133 | + ), |
| 134 | + panel.AddQuery( |
| 135 | + query.PromQL( |
| 136 | + fmt.Sprintf("sum(rate(traces_span_metrics_duration_sum{%s}[5m]) / rate(traces_span_metrics_duration_count{%s}[5m])) by (span_name) > 0", variableMatchers, variableMatchers), |
| 137 | + query.SeriesNameFormat("95th"), |
| 138 | + ), |
| 139 | + ), |
| 140 | + ), |
| 141 | + ) |
| 142 | +} |
| 143 | + |
| 144 | +func buildAPMDashboard() (dashboard.Builder, error) { |
| 145 | + variableMatchers := "namespace=\"$namespace\", service=\"$collector\", service_name=\"$service\"" |
| 146 | + |
| 147 | + return dashboard.New("apm", |
| 148 | + dashboard.Name("Application Performance Monitoring (APM)"), |
| 149 | + dashboard.AddVariable("namespace", |
| 150 | + listvariable.List( |
| 151 | + listvariable.DisplayName("OTEL Collector Namespace"), |
| 152 | + labelvalues.PrometheusLabelValues("namespace", |
| 153 | + labelvalues.Matchers("traces_span_metrics_calls{}"), |
| 154 | + ), |
| 155 | + ), |
| 156 | + ), |
| 157 | + dashboard.AddVariable("collector", |
| 158 | + listvariable.List( |
| 159 | + listvariable.DisplayName("OTEL Collector"), |
| 160 | + labelvalues.PrometheusLabelValues("service", |
| 161 | + labelvalues.Matchers("traces_span_metrics_calls{namespace=\"$namespace\"}"), |
| 162 | + ), |
| 163 | + ), |
| 164 | + ), |
| 165 | + dashboard.AddVariable("service", |
| 166 | + listvariable.List( |
| 167 | + listvariable.DisplayName("Service"), |
| 168 | + labelvalues.PrometheusLabelValues("service_name", |
| 169 | + labelvalues.Matchers("traces_span_metrics_calls{namespace=\"$namespace\", service=\"$collector\"}"), |
| 170 | + ), |
| 171 | + ), |
| 172 | + ), |
| 173 | + withServiceMetrics(variableMatchers), |
| 174 | + withOperationMetrics(variableMatchers), |
| 175 | + ) |
| 176 | +} |
| 177 | + |
| 178 | +func newAPMDashboard(namespace string) (*persesv1alpha1.PersesDashboard, error) { |
| 179 | + builder, err := buildAPMDashboard() |
| 180 | + if err != nil { |
| 181 | + return nil, err |
| 182 | + } |
| 183 | + |
| 184 | + // Workaround because of type conflict between Perses plugin types and Perses fork in rhobs org |
| 185 | + rhobsDashboard := persesv1.Dashboard{} |
| 186 | + bytes, err := json.Marshal(builder.Dashboard) |
| 187 | + if err != nil { |
| 188 | + return nil, err |
| 189 | + } |
| 190 | + err = rhobsDashboard.UnmarshalJSON(bytes) |
| 191 | + if err != nil { |
| 192 | + return nil, err |
| 193 | + } |
| 194 | + |
| 195 | + return &persesv1alpha1.PersesDashboard{ |
| 196 | + TypeMeta: metav1.TypeMeta{ |
| 197 | + APIVersion: persesv1alpha1.GroupVersion.String(), |
| 198 | + Kind: "PersesDashboard", |
| 199 | + }, |
| 200 | + ObjectMeta: metav1.ObjectMeta{ |
| 201 | + Name: "apm-dashboard", |
| 202 | + Namespace: namespace, |
| 203 | + Labels: map[string]string{ |
| 204 | + "app.kubernetes.io/managed-by": "observability-operator", |
| 205 | + }, |
| 206 | + }, |
| 207 | + Spec: persesv1alpha1.Dashboard{ |
| 208 | + DashboardSpec: rhobsDashboard.Spec, |
| 209 | + }, |
| 210 | + }, nil |
| 211 | +} |
0 commit comments