summaryrefslogtreecommitdiff
path: root/internal/ingester/remotewrite_test.go
blob: d1fdee66b8587dd5d1a9e0199c1d2b1685c4aeef (plain)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package ingester

import (
	"testing"
	"time"

	"epimetheus/internal/metrics"

	"github.com/prometheus/prometheus/prompb"
)

func TestNewRemoteWriteIngester(t *testing.T) {
	ingester := NewRemoteWriteIngester()
	if ingester.client == nil {
		t.Error("RemoteWriteIngester.client should not be nil")
	}
}

func TestConvertSamplesToTimeSeries(t *testing.T) {
	now := time.Now()
	samples := []metrics.Sample{
		{
			MetricName: "test_metric1",
			Labels:     map[string]string{"env": "prod", "host": "server1"},
			Value:      42.5,
			Timestamp:  now,
		},
		{
			MetricName: "test_metric2",
			Labels:     map[string]string{"env": "test"},
			Value:      100.0,
			Timestamp:  now.Add(-1 * time.Hour),
		},
	}

	timeSeries := convertSamplesToTimeSeries(samples)

	if len(timeSeries) != 2 {
		t.Errorf("Expected 2 time series, got %d", len(timeSeries))
	}

	// Check first time series
	ts1 := timeSeries[0]
	if len(ts1.Labels) != 3 { // __name__ + 2 custom labels
		t.Errorf("Expected 3 labels, got %d", len(ts1.Labels))
	}

	hasName := false
	for _, label := range ts1.Labels {
		if label.Name == "__name__" && label.Value == "test_metric1" {
			hasName = true
		}
	}
	if !hasName {
		t.Error("Missing or incorrect __name__ label")
	}

	if len(ts1.Samples) != 1 {
		t.Errorf("Expected 1 sample, got %d", len(ts1.Samples))
	}
	if ts1.Samples[0].Value != 42.5 {
		t.Errorf("Expected value 42.5, got %f", ts1.Samples[0].Value)
	}
}

func TestGenerateHistoricTimeSeries(t *testing.T) {
	timestamp := time.Now().Add(-24 * time.Hour)

	timeSeries := generateHistoricTimeSeries(timestamp)

	if len(timeSeries) == 0 {
		t.Error("Expected time series to be generated")
	}

	// Should contain various metric types
	metricNames := make(map[string]bool)
	for _, ts := range timeSeries {
		for _, label := range ts.Labels {
			if label.Name == "__name__" {
				metricNames[label.Value] = true
			}
		}
	}

	expectedMetrics := []string{
		"epimetheus_test_requests_total",
		"epimetheus_test_active_connections",
		"epimetheus_test_temperature_celsius",
		"epimetheus_test_jobs_processed_total",
	}

	for _, expected := range expectedMetrics {
		if !metricNames[expected] {
			t.Errorf("Expected metric %s not found", expected)
		}
	}
}

func TestCreateCounterSeries(t *testing.T) {
	baseLabels := []prompb.Label{
		{Name: "instance", Value: "test-instance"},
		{Name: "job", Value: "test-job"},
	}

	ts := createCounterSeries("test_counter", baseLabels, 123.45, 1234567890000)

	if len(ts.Labels) != 3 { // __name__ + 2 base labels
		t.Errorf("Expected 3 labels, got %d", len(ts.Labels))
	}

	if len(ts.Samples) != 1 {
		t.Errorf("Expected 1 sample, got %d", len(ts.Samples))
	}

	if ts.Samples[0].Value != 123.45 {
		t.Errorf("Expected value 123.45, got %f", ts.Samples[0].Value)
	}

	if ts.Samples[0].Timestamp != 1234567890000 {
		t.Errorf("Expected timestamp 1234567890000, got %d", ts.Samples[0].Timestamp)
	}
}

func TestCreateGaugeSeries(t *testing.T) {
	baseLabels := []prompb.Label{
		{Name: "instance", Value: "test-instance"},
	}

	ts := createGaugeSeries("test_gauge", baseLabels, 67.89, 9876543210000)

	if len(ts.Samples) != 1 {
		t.Errorf("Expected 1 sample, got %d", len(ts.Samples))
	}

	if ts.Samples[0].Value != 67.89 {
		t.Errorf("Expected value 67.89, got %f", ts.Samples[0].Value)
	}
}

func TestGenerateHistogramSeries(t *testing.T) {
	baseLabels := []prompb.Label{
		{Name: "instance", Value: "test-instance"},
	}
	timestamp := int64(1234567890000)

	series := generateHistogramSeries(baseLabels, timestamp)

	if len(series) == 0 {
		t.Error("Expected histogram series to be generated")
	}

	// Should contain buckets, +Inf, sum, and count
	metricTypes := make(map[string]int)
	for _, ts := range series {
		for _, label := range ts.Labels {
			if label.Name == "__name__" {
				metricTypes[label.Value]++
			}
		}
	}

	if metricTypes["epimetheus_test_request_duration_seconds_bucket"] == 0 {
		t.Error("Expected histogram buckets")
	}
	if metricTypes["epimetheus_test_request_duration_seconds_sum"] != 1 {
		t.Error("Expected histogram sum")
	}
	if metricTypes["epimetheus_test_request_duration_seconds_count"] != 1 {
		t.Error("Expected histogram count")
	}
}

func TestGenerateLabeledCounterSeries(t *testing.T) {
	baseLabels := []prompb.Label{
		{Name: "instance", Value: "test-instance"},
	}
	timestamp := int64(1234567890000)

	series := generateLabeledCounterSeries(baseLabels, timestamp)

	if len(series) == 0 {
		t.Error("Expected labeled counter series to be generated")
	}

	// Should have combinations of job types and statuses
	// 3 job types * 2 statuses = 6 series
	if len(series) != 6 {
		t.Errorf("Expected 6 labeled counter series, got %d", len(series))
	}

	// Verify label structure
	for _, ts := range series {
		hasJobType := false
		hasStatus := false
		for _, label := range ts.Labels {
			if label.Name == "job_type" {
				hasJobType = true
			}
			if label.Name == "status" {
				hasStatus = true
			}
		}
		if !hasJobType {
			t.Error("Expected job_type label")
		}
		if !hasStatus {
			t.Error("Expected status label")
		}
	}
}