1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
package parser
import (
"context"
"encoding/json"
"fmt"
"io"
"time"
"epimetheus/internal/metrics"
)
// JSONParser parses metrics from JSON format
type JSONParser struct{}
// NewJSONParser creates a new JSON parser
func NewJSONParser() *JSONParser {
return &JSONParser{}
}
type jsonSample struct {
Metric string `json:"metric"`
Labels map[string]string `json:"labels"`
Value float64 `json:"value"`
TimestampMs int64 `json:"timestamp_ms,omitempty"`
}
// Parse reads metrics from JSON format
func (p *JSONParser) Parse(ctx context.Context, reader io.Reader) ([]metrics.Sample, error) {
var rawSamples []jsonSample
decoder := json.NewDecoder(reader)
if err := decoder.Decode(&rawSamples); err != nil {
return nil, fmt.Errorf("failed to parse JSON: %w", err)
}
samples := make([]metrics.Sample, 0, len(rawSamples))
for _, raw := range rawSamples {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
}
if raw.Metric == "" {
continue
}
timestamp := time.Now()
if raw.TimestampMs > 0 {
timestamp = time.UnixMilli(raw.TimestampMs)
}
if raw.Labels == nil {
raw.Labels = make(map[string]string)
}
samples = append(samples, metrics.NewSample(raw.Metric, raw.Labels, raw.Value, timestamp))
}
return samples, nil
}
|