@@ -38,6 +38,26 @@ var gtePG16 json.RawMessage
3838//go:embed "generated/lt_pg16_metrics.json"
3939var ltPG16 json.RawMessage
4040
41+ type queryMetrics struct {
42+ Metrics []* metric `json:"metrics"`
43+ Query string `json:"sql"`
44+ }
45+
46+ type metric struct {
47+ Aggregation string `json:"aggregation,omitempty"`
48+ AttributeColumns []string `json:"attribute_columns,omitempty"`
49+ DataType string `json:"data_type,omitempty"`
50+ Description string `json:"description,omitempty"`
51+ MetricName string `json:"metric_name"`
52+ Monotonic bool `json:"monotonic,omitempty"`
53+ StartTsColumn string `json:"start_ts_column,omitempty"`
54+ StaticAttributes map [string ]string `json:"static_attributes,omitempty"`
55+ TsColumn string `json:"ts_column,omitempty"`
56+ Unit string `json:"unit,omitempty"`
57+ ValueColumn string `json:"value_column"`
58+ ValueType string `json:"value_type,omitempty"`
59+ }
60+
4161func EnablePostgresMetrics (ctx context.Context , inCluster * v1beta1.PostgresCluster , config * Config ) {
4262 if feature .Enabled (ctx , feature .OpenTelemetryMetrics ) {
4363 log := logging .FromContext (ctx )
@@ -72,8 +92,8 @@ func EnablePostgresMetrics(ctx context.Context, inCluster *v1beta1.PostgresClust
7292 inCluster .Spec .Instrumentation .Metrics .CustomQueries != nil &&
7393 inCluster .Spec .Instrumentation .Metrics .CustomQueries .Remove != nil {
7494
75- // Convert json to array of maps
76- var fiveSecondMetricsArr []map [ string ] any
95+ // Convert json to array of queryMetrics objects
96+ var fiveSecondMetricsArr []queryMetrics
7797 err := json .Unmarshal (fiveSecondMetricsClone , & fiveSecondMetricsArr )
7898 if err != nil {
7999 log .Error (err , "error compiling postgres metrics" )
@@ -83,8 +103,8 @@ func EnablePostgresMetrics(ctx context.Context, inCluster *v1beta1.PostgresClust
83103 fiveSecondMetricsArr = removeMetricsFromQueries (
84104 inCluster .Spec .Instrumentation .Metrics .CustomQueries .Remove , fiveSecondMetricsArr )
85105
86- // Convert json to array of maps
87- var fiveMinuteMetricsArr []map [ string ] any
106+ // Convert json to array of queryMetrics objects
107+ var fiveMinuteMetricsArr []queryMetrics
88108 err = json .Unmarshal (fiveMinuteMetricsClone , & fiveMinuteMetricsArr )
89109 if err != nil {
90110 log .Error (err , "error compiling postgres metrics" )
@@ -198,48 +218,37 @@ func appendToJSONArray(a1, a2 json.RawMessage) (json.RawMessage, error) {
198218}
199219
200220func removeMetricsFromQueries (metricsToRemove []string ,
201- queryMetricsArr []map [ string ] any ,
202- ) []map [ string ] any {
221+ queryMetricsArr []queryMetrics ,
222+ ) []queryMetrics {
203223 // Iterate over the metrics that should be removed
204224Outer:
205225 for _ , metricToRemove := range metricsToRemove {
206- // Iterate over array of query/metrics maps
226+ // Iterate over array of query/metrics objects
207227 for j , queryAndMetrics := range queryMetricsArr {
208- // Assert that the metrics key in the query/metrics map holds an array
209- if metricsArr , ok := queryAndMetrics ["metrics" ].([]any ); ok {
210- // Iterate over the metrics array
211- for k , metric := range metricsArr {
212- // Assert that element in the metrics array is a map[string]any
213- if metricMap , ok := metric .(map [string ]any ); ok {
214- // Check to see if the metric_name matches the metricToRemove
215- if metricMap ["metric_name" ] == metricToRemove {
216- // Remove the metric. Since there won't ever be any
217- // duplicates, we will be exiting this loop early and
218- // therefore don't care about the order of the metrics
219- // array.
220- metricsArr [k ] = metricsArr [len (metricsArr )- 1 ]
221- metricsArr = metricsArr [:len (metricsArr )- 1 ]
222- queryAndMetrics ["metrics" ] = metricsArr
223-
224- // If the metrics array is empty, remove the query/metrics
225- // map entirely. Again, we don't care about order.
226- if len (metricsArr ) == 0 {
227- queryMetricsArr [j ] = queryMetricsArr [len (queryMetricsArr )- 1 ]
228- queryMetricsArr = queryMetricsArr [:len (queryMetricsArr )- 1 ]
229- }
230-
231- // We found and deleted the metric, so we can continue
232- // to the next iteration of the Outer loop.
233- continue Outer
234- }
235- } else {
236- // TODO: What should we do if the assertion fails?
237- fmt .Printf ("%s is not a map[string]any" , metric )
228+ // Iterate over the metrics array
229+ metricsArr := queryAndMetrics .Metrics
230+ for k , metric := range metricsArr {
231+ // Check to see if the metric_name matches the metricToRemove
232+ if metric .MetricName == metricToRemove {
233+ // Remove the metric. Since there won't ever be any
234+ // duplicates, we will be exiting this loop early and
235+ // therefore don't care about the order of the metrics
236+ // array.
237+ metricsArr [len (metricsArr )- 1 ], metricsArr [k ] = nil , metricsArr [len (metricsArr )- 1 ]
238+ metricsArr = metricsArr [:len (metricsArr )- 1 ]
239+ queryMetricsArr [j ].Metrics = metricsArr
240+
241+ // If the metrics array is empty, remove the query/metrics
242+ // map entirely. Again, we don't care about order.
243+ if len (metricsArr ) == 0 {
244+ queryMetricsArr [j ] = queryMetricsArr [len (queryMetricsArr )- 1 ]
245+ queryMetricsArr = queryMetricsArr [:len (queryMetricsArr )- 1 ]
238246 }
247+
248+ // We found and deleted the metric, so we can continue
249+ // to the next iteration of the Outer loop.
250+ continue Outer
239251 }
240- } else {
241- // TODO: What should we do if the assertion fails?
242- fmt .Printf ("%s is not a []any" , queryAndMetrics ["metrics" ])
243252 }
244253 }
245254 }
0 commit comments