|
| 1 | +/* |
| 2 | + * === This file is part of ALICE O² === |
| 3 | + * |
| 4 | + * Copyright 2025 CERN and copyright holders of ALICE O². |
| 5 | + * Author: Michal Tichak <michal.tichak@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 | + |
1 | 25 | package monitoring |
2 | 26 |
|
| 27 | +import ( |
| 28 | + "fmt" |
| 29 | + "io" |
| 30 | + "time" |
| 31 | + |
| 32 | + lp "github.com/influxdata/line-protocol/v2/lineprotocol" |
| 33 | +) |
| 34 | + |
3 | 35 | type ( |
4 | | - TagsType map[string]any |
5 | | - ValuesType map[string]any |
| 36 | + Tag struct { |
| 37 | + name string |
| 38 | + value string |
| 39 | + } |
| 40 | + |
| 41 | + TagsType []Tag |
| 42 | + FieldsType map[string]any |
6 | 43 | ) |
7 | 44 |
|
8 | 45 | type Metric struct { |
9 | | - Name string `json:"name"` |
10 | | - Values ValuesType `json:"values"` |
11 | | - Tags TagsType `json:"tags,omitempty"` |
12 | | - Timestamp int64 `json:"timestamp"` |
| 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 |
| 63 | +} |
| 64 | + |
| 65 | +func (metric *Metric) AddTag(tagName string, value string) { |
| 66 | + metric.tags = append(metric.tags, Tag{name: tagName, value: value}) |
13 | 67 | } |
14 | 68 |
|
15 | | -func (metric *Metric) AddTag(tagName string, value any) { |
16 | | - if metric.Tags == nil { |
17 | | - metric.Tags = make(TagsType) |
| 69 | +func (metric *Metric) setField(fieldName string, field any) { |
| 70 | + if metric.fields == nil { |
| 71 | + metric.fields = make(FieldsType) |
18 | 72 | } |
19 | | - metric.Tags[tagName] = value |
| 73 | + metric.fields[fieldName] = field |
20 | 74 | } |
21 | 75 |
|
22 | | -func (metric *Metric) AddValue(valueName string, value any) { |
23 | | - if metric.Values == nil { |
24 | | - metric.Values = make(ValuesType) |
| 76 | +func (metric *Metric) SetFieldInt64(fieldName string, field int64) { |
| 77 | + metric.setField(fieldName, field) |
| 78 | +} |
| 79 | + |
| 80 | +func (metric *Metric) SetFieldUInt64(fieldName string, field uint64) { |
| 81 | + metric.setField(fieldName, field) |
| 82 | +} |
| 83 | + |
| 84 | +func (metric *Metric) SetFieldFloat64(fieldName string, field float64) { |
| 85 | + metric.setField(fieldName, field) |
| 86 | +} |
| 87 | + |
| 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) { |
| 92 | + case int64: |
| 93 | + metric.fields[fieldName] = v + storedField.(int64) |
| 94 | + case uint64: |
| 95 | + metric.fields[fieldName] = v + storedField.(uint64) |
| 96 | + case float64: |
| 97 | + metric.fields[fieldName] = v + storedField.(float64) |
| 98 | + } |
| 99 | + } else { |
| 100 | + metric.fields[fieldName] = field |
| 101 | + } |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +func Format(writer io.Writer, metrics []Metric) error { |
| 106 | + var enc lp.Encoder |
| 107 | + |
| 108 | + for _, metric := range metrics { |
| 109 | + enc.StartLine(metric.name) |
| 110 | + for _, tag := range metric.tags { |
| 111 | + enc.AddTag(tag.name, tag.value) |
| 112 | + } |
| 113 | + |
| 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)) |
| 117 | + } |
| 118 | + enc.EndLine(metric.timestamp) |
25 | 119 | } |
26 | | - metric.Values[valueName] = value |
| 120 | + |
| 121 | + if err := enc.Err(); err != nil { |
| 122 | + return err |
| 123 | + } |
| 124 | + |
| 125 | + _, err := fmt.Fprintf(writer, "%s", enc.Bytes()) |
| 126 | + return err |
27 | 127 | } |
0 commit comments