11//
22// DISCLAIMER
33//
4- // Copyright 2016-2023 ArangoDB GmbH, Cologne, Germany
4+ // Copyright 2016-2024 ArangoDB GmbH, Cologne, Germany
55//
66// Licensed under the Apache License, Version 2.0 (the "License");
77// you may not use this file except in compliance with the License.
@@ -38,6 +38,9 @@ var metricsGoTemplate []byte
3838//go:embed metrics.item.go.tmpl
3939var metricsItemGoTemplate []byte
4040
41+ //go:embed metrics.item.go_test.tmpl
42+ var metricsItemGoTestTemplate []byte
43+
4144//go:embed metrics.item.tmpl
4245var metricItemTemplate []byte
4346
@@ -98,6 +101,8 @@ type Metric struct {
98101 Type string `json:"type" yaml:"type"`
99102 ShortDescription string `json:"shortDescription" yaml:"shortDescription"`
100103
104+ Global bool `json:"global" yaml:"global"`
105+
101106 Labels []Label `json:"labels" yaml:"labels"`
102107 AlertingRules []Alerting `json:"alertingRules" yaml:"alertingRules"`
103108}
@@ -277,22 +282,22 @@ func generateLabels(labels []Label) string {
277282
278283func generateMetricsGO (root string , in MetricsDoc ) error {
279284 i , err := template .New ("metrics" ).Parse (string (metricsItemGoTemplate ))
285+ if err != nil {
286+ return err
287+ }
280288
289+ t , err := template .New ("metrics" ).Parse (string (metricsItemGoTestTemplate ))
281290 if err != nil {
282291 return err
283292 }
293+
284294 for _ , namespace := range in .Namespaces .Keys () {
285295 for _ , g := range in .Namespaces [namespace ].Keys () {
286296 for _ , metric := range in.Namespaces [namespace ][g ].Keys () {
287297 details := in.Namespaces [namespace ][g ][metric ]
288298
289299 mname := fmt .Sprintf ("%s_%s_%s" , namespace , g , metric )
290300
291- out , err := os .OpenFile (path .Join (root , fmt .Sprintf ("%s.go" , mname )), os .O_CREATE | os .O_TRUNC | os .O_WRONLY , 0644 )
292- if err != nil {
293- return err
294- }
295-
296301 parts := strings .Split (mname , "_" )
297302 tparts := strings .Split (strings .Title (strings .Join (parts , " " )), " " )
298303
@@ -311,6 +316,10 @@ func generateMetricsGO(root string, in MetricsDoc) error {
311316 params = append (params , "value float64" )
312317 keys = append (keys , "value" )
313318
319+ var mapTypes = map [string ]string {}
320+ var mapKeys []string
321+ var mapIKeys = map [string ]string {}
322+
314323 for _ , label := range details .Labels {
315324 v := strings .Split (strings .ToLower (label .Key ), "_" )
316325 for id := range v {
@@ -323,29 +332,60 @@ func generateMetricsGO(root string, in MetricsDoc) error {
323332
324333 k := strings .Join (v , "" )
325334
335+ v [0 ] = strings .Title (v [0 ])
336+
337+ kPublic := strings .Join (v , "" )
338+
326339 keys = append (keys , k )
340+ mapKeys = append (mapKeys , kPublic )
341+
342+ mapIKeys [kPublic ] = k
327343
328344 if t := label .Type ; t != nil {
329345 params = append (params , fmt .Sprintf ("%s %s" , k , * t ))
346+ mapTypes [kPublic ] = * t
330347 } else {
331348 params = append (params , fmt .Sprintf ("%s string" , k ))
349+ mapTypes [kPublic ] = "string"
332350 }
333351 }
334352
335- if err := i .Execute (out , map [string ]interface {}{
336- "name" : mname ,
337- "fname" : strings .Join (fnameParts , "" ),
338- "ename" : strings .Join (tparts , "" ),
339- "shortDescription" : details .ShortDescription ,
340- "labels" : generateLabels (details .Labels ),
341- "type" : details .Type ,
342- "fparams" : strings .Join (params , ", " ),
343- "fkeys" : strings .Join (keys , ", " ),
344- }); err != nil {
353+ save := func (template * template.Template , path string ) error {
354+ out , err := os .OpenFile (path , os .O_CREATE | os .O_TRUNC | os .O_WRONLY , 0644 )
355+ if err != nil {
356+ return err
357+ }
358+
359+ if err := template .Execute (out , map [string ]interface {}{
360+ "name" : mname ,
361+ "fname" : strings .Join (fnameParts , "" ),
362+ "ename" : strings .Join (tparts , "" ),
363+ "shortDescription" : details .ShortDescription ,
364+ "global" : details .Global ,
365+ "labels" : generateLabels (details .Labels ),
366+ "type" : details .Type ,
367+ "mapTypes" : mapTypes ,
368+ "mapKeys" : mapKeys ,
369+ "mapIKeys" : mapIKeys ,
370+ "args" : strings .Join (params [1 :], ", " ),
371+ "fparams" : strings .Join (params , ", " ),
372+ "fkeys" : strings .Join (keys , ", " ),
373+ }); err != nil {
374+ return err
375+ }
376+
377+ if err := out .Close (); err != nil {
378+ return err
379+ }
380+
381+ return nil
382+ }
383+
384+ if err := save (i , path .Join (root , fmt .Sprintf ("%s.go" , mname ))); err != nil {
345385 return err
346386 }
347387
348- if err := out . Close ( ); err != nil {
388+ if err := save ( t , path . Join ( root , fmt . Sprintf ( "%s_test.go" , mname )) ); err != nil {
349389 return err
350390 }
351391 }
0 commit comments