1+ /*
2+ * === This file is part of ALICE O² ===
3+ *
4+ * Copyright 2025 CERN and copyright holders of ALICE O².
5+ * Author: Teo Mrnjavac <teo.mrnjavac@cern.ch>
6+ *
7+ * This program is free software: you can redistribute it and/or modify
8+ * it under the terms of the GNU General Public License as published by
9+ * the Free Software Foundation, either version 3 of the License, or
10+ * (at your option) any later version.
11+ *
12+ * This program is distributed in the hope that it will be useful,
13+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+ * GNU General Public License for more details.
16+ *
17+ * You should have received a copy of the GNU General Public License
18+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
19+ *
20+ * In applying this license CERN does not waive the privileges and
21+ * immunities granted to it by virtue of its status as an
22+ * Intergovernmental Organization or submit itself to any jurisdiction.
23+ */
24+
125package monitoring
226
327import (
@@ -10,58 +34,70 @@ import (
1034
1135type (
1236 Tag struct {
13- Name string
14- Value string
37+ name string
38+ value string
1539 }
1640
1741 TagsType []Tag
18- ValuesType map [string ]interface {}
42+ FieldsType map [string ]any
1943)
2044
2145type Metric struct {
22- Name string
23- Values ValuesType
24- Tags TagsType
25- Timestamp time.Time
46+ name string
47+ fields FieldsType
48+ tags TagsType
49+ timestamp time.Time
50+ }
51+
52+ func NewMetric (name string , timestamp time.Time ) Metric {
53+ return Metric {
54+ name : name , timestamp : timestamp ,
55+ }
56+ }
57+
58+ func (metric * Metric ) GetFields () (fields FieldsType ) {
59+ for fieldName , field := range metric .fields {
60+ fields [fieldName ] = field
61+ }
62+ return
2663}
2764
2865func (metric * Metric ) AddTag (tagName string , value string ) {
29- metric .Tags = append (metric .Tags , Tag {Name : tagName , Value : value })
66+ metric .tags = append (metric .tags , Tag {name : tagName , value : value })
3067}
3168
32- func (metric * Metric ) addValue ( valueName string , value interface {} ) {
33- if metric .Values == nil {
34- metric .Values = make (ValuesType )
69+ func (metric * Metric ) setField ( fieldName string , field any ) {
70+ if metric .fields == nil {
71+ metric .fields = make (FieldsType )
3572 }
36- metric .Values [ valueName ] = value
73+ metric .fields [ fieldName ] = field
3774}
3875
39- func (metric * Metric ) AddValueInt64 ( valueName string , value int64 ) {
40- metric .addValue ( valueName , value )
76+ func (metric * Metric ) SetFieldInt64 ( fieldName string , field int64 ) {
77+ metric .setField ( fieldName , field )
4178}
4279
43- func (metric * Metric ) AddValueUInt64 ( valueName string , value uint64 ) {
44- metric .addValue ( valueName , value )
80+ func (metric * Metric ) SetFieldUInt64 ( fieldName string , field uint64 ) {
81+ metric .setField ( fieldName , field )
4582}
4683
47- func (metric * Metric ) AddValueFloat64 ( valueName string , value float64 ) {
48- metric .addValue ( valueName , value )
84+ func (metric * Metric ) SetFieldFloat64 ( fieldName string , field float64 ) {
85+ metric .setField ( fieldName , field )
4986}
5087
51- func (metric * Metric ) MergeValues (other * Metric ) {
52- for valueName , value := range other .Values {
53- if storedValue , ok := metric .Values [ valueName ]; ok {
54- switch v := value .(type ) {
88+ func (metric * Metric ) MergeFields (other * Metric ) {
89+ for fieldName , field := range other .fields {
90+ if storedField , ok := metric .fields [ fieldName ]; ok {
91+ switch v := field .(type ) {
5592 case int64 :
56- metric .Values [ valueName ] = v + storedValue .(int64 )
93+ metric .fields [ fieldName ] = v + storedField .(int64 )
5794 case uint64 :
58- metric .Values [ valueName ] = v + storedValue .(uint64 )
95+ metric .fields [ fieldName ] = v + storedField .(uint64 )
5996 case float64 :
60- metric .Values [valueName ] = v + storedValue .(float64 )
61- case string :
97+ metric .fields [fieldName ] = v + storedField .(float64 )
6298 }
6399 } else {
64- metric .Values [ valueName ] = value
100+ metric .fields [ fieldName ] = field
65101 }
66102 }
67103}
@@ -70,16 +106,16 @@ func Format(writer io.Writer, metrics []Metric) error {
70106 var enc lp.Encoder
71107
72108 for _ , metric := range metrics {
73- enc .StartLine (metric .Name )
74- for _ , tag := range metric .Tags {
75- enc .AddTag (tag .Name , tag .Value )
109+ enc .StartLine (metric .name )
110+ for _ , tag := range metric .tags {
111+ enc .AddTag (tag .name , tag .value )
76112 }
77113
78- for valueName , value := range metric .Values {
79- // we cannot panic as we provide accessors only for allowed type with AddValue *
80- enc .AddField (valueName , lp .MustNewValue (value ))
114+ for fieldName , field := range metric .fields {
115+ // we cannot panic as we provide accessors only for allowed type with AddField *
116+ enc .AddField (fieldName , lp .MustNewValue (field ))
81117 }
82- enc .EndLine (metric .Timestamp )
118+ enc .EndLine (metric .timestamp )
83119 }
84120
85121 if err := enc .Err (); err != nil {
0 commit comments