Skip to content

Commit d9ef742

Browse files
committed
Started Timestamp tests (not working yet)
1 parent 7990a25 commit d9ef742

10 files changed

Lines changed: 93 additions & 55 deletions

File tree

config/config.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ type Aggregation struct {
152152
Params map[string]interface{} `yaml:"params"`
153153
}
154154

155-
func checkError(err error) {
155+
func CheckError(err error) {
156156
if err != nil {
157157
fmt.Println(err)
158158
os.Exit(1)
@@ -170,14 +170,19 @@ func ParseConfigText(content []byte) Config {
170170
config.Prometheus = PrometheusConfig{Port: DefaultPrometheusPort, Timestamps: DefaultTimestamps, WindowSize: DefaultWindowSize}
171171

172172
err := yaml.Unmarshal(content, &config)
173-
checkError(err)
173+
CheckError(err)
174174

175175
return config
176176
}
177177

178178
func ParseConfig(path string) Config {
179179
fileContent, err := os.ReadFile(path)
180-
checkError(err)
180+
CheckError(err)
181181

182182
return ParseConfigText(fileContent)
183183
}
184+
185+
func GetLastFullMin() time.Time {
186+
nowUnix := time.Now().Unix()
187+
return time.Unix(nowUnix-nowUnix%60, 0)
188+
}

exporters/counter_test.go

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,23 +42,23 @@ func TestAddToCounterHistory(t *testing.T) {
4242
})
4343
counterVec, labelValues := prepareCounterVec()
4444

45-
now := time.Now()
46-
counterVec.Add(labelValues, 10, now.Add(-3*time.Minute))
47-
counterVec.Add(labelValues, 10, now.Add(-2*time.Minute))
48-
counterVec.Add(labelValues, 10, now.Add(-1*time.Minute))
49-
counterVec.Add(labelValues, 10, now)
45+
lastFullMin := config.GetLastFullMin()
46+
counterVec.Add(labelValues, 10, lastFullMin.Add(-3*time.Minute))
47+
counterVec.Add(labelValues, 10, lastFullMin.Add(-2*time.Minute))
48+
counterVec.Add(labelValues, 10, lastFullMin.Add(-1*time.Minute))
49+
counterVec.Add(labelValues, 10, lastFullMin)
5050

5151
history := counterVec.withLabelValues(labelValues)
5252
assert.Equal(t,
5353
[]CounterValue{{
5454
Value: 40,
55-
Timestamp: now,
55+
Timestamp: lastFullMin,
5656
}, {
5757
Value: 30,
58-
Timestamp: now.Add(-1 * time.Minute),
58+
Timestamp: lastFullMin.Add(-1 * time.Minute),
5959
}, {
6060
Value: 20,
61-
Timestamp: now.Add(-2 * time.Minute),
61+
Timestamp: lastFullMin.Add(-2 * time.Minute),
6262
}},
6363
history.Values,
6464
)
@@ -72,9 +72,9 @@ func TestCollectCounter(t *testing.T) {
7272
},
7373
})
7474
counterVec, labelValues := prepareCounterVec()
75-
now := time.Now()
76-
counterVec.Add(labelValues, 10, now.Add(-1*time.Minute))
77-
counterVec.Add(labelValues, 10, now)
75+
lastFullMin := config.GetLastFullMin()
76+
counterVec.Add(labelValues, 10, lastFullMin.Add(-1*time.Minute))
77+
counterVec.Add(labelValues, 10, lastFullMin)
7878

7979
ch := make(chan prometheus.Metric)
8080
go func() {
@@ -95,9 +95,8 @@ func TestCollectToOld(t *testing.T) {
9595
})
9696

9797
counterVec, labelValues := prepareCounterVec()
98-
now := time.Now()
99-
counterVec.Add(labelValues, 10, now.Add(-4*time.Minute)) // Metric older than 3 Minutes (configured via WindowSize), so its not collected
100-
counterVec.Add(labelValues, 10, now.Add(-2*time.Minute))
98+
lastFullMin := config.GetLastFullMin()
99+
counterVec.Add(labelValues, 10, lastFullMin.Add(-3*time.Minute)) // Metric older than 3 Minutes (configured via WindowSize), so its not collected
101100

102101
ch := make(chan prometheus.Metric)
103102
go func() {
@@ -106,5 +105,5 @@ func TestCollectToOld(t *testing.T) {
106105
}()
107106

108107
metrics := collectValues(ch)
109-
assert.Equal(t, 1, len(metrics))
108+
assert.Equal(t, 0, len(metrics))
110109
}

exporters/histogram_test.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,29 +35,29 @@ func TestAddToHistogramHistory(t *testing.T) {
3535
})
3636
histogramVec, labelValues := prepareHistgramVec()
3737

38-
now := time.Now()
39-
histogramVec.Add(labelValues, map[float64]uint64{10: 10, 20: 20}, 30, 600, now.Add(-3*time.Minute))
40-
histogramVec.Add(labelValues, map[float64]uint64{10: 10, 20: 20}, 30, 600, now.Add(-2*time.Minute))
41-
histogramVec.Add(labelValues, map[float64]uint64{10: 0, 20: 1}, 1, 20, now.Add(-1*time.Minute))
42-
histogramVec.Add(labelValues, map[float64]uint64{10: 1, 20: 1}, 1, 10, now)
38+
lastFullMin := config.GetLastFullMin()
39+
histogramVec.Add(labelValues, map[float64]uint64{10: 10, 20: 20}, 30, 600, lastFullMin.Add(-3*time.Minute))
40+
histogramVec.Add(labelValues, map[float64]uint64{10: 10, 20: 20}, 30, 600, lastFullMin.Add(-2*time.Minute))
41+
histogramVec.Add(labelValues, map[float64]uint64{10: 0, 20: 1}, 1, 20, lastFullMin.Add(-1*time.Minute))
42+
histogramVec.Add(labelValues, map[float64]uint64{10: 1, 20: 1}, 1, 10, lastFullMin)
4343

4444
history := histogramVec.withLabelValues(labelValues)
4545
assert.Equal(t,
4646
[]HistogramValue{{
4747
Buckets: map[float64]uint64{10: 21, 20: 42},
4848
Count: 62,
4949
Sum: 1230,
50-
Timestamp: now,
50+
Timestamp: lastFullMin,
5151
}, {
5252
Buckets: map[float64]uint64{10: 20, 20: 41},
5353
Count: 61,
5454
Sum: 1220,
55-
Timestamp: now.Add(-1 * time.Minute),
55+
Timestamp: lastFullMin.Add(-1 * time.Minute),
5656
}, {
5757
Buckets: map[float64]uint64{10: 20, 20: 40},
5858
Count: 60,
5959
Sum: 1200,
60-
Timestamp: now.Add(-2 * time.Minute),
60+
Timestamp: lastFullMin.Add(-2 * time.Minute),
6161
}},
6262
history.Values,
6363
)
@@ -78,9 +78,9 @@ func TestAddBadBucketsToHistogramHistory(t *testing.T) {
7878
}
7979
}()
8080

81-
now := time.Now()
82-
histogramVec.Add(labelValues, map[float64]uint64{10: 10, 20: 20}, 30, 600, now.Add(-1*time.Minute))
83-
histogramVec.Add(labelValues, map[float64]uint64{20: 10}, 10, 200, now)
81+
lastFullMin := config.GetLastFullMin()
82+
histogramVec.Add(labelValues, map[float64]uint64{10: 10, 20: 20}, 30, 600, lastFullMin.Add(-1*time.Minute))
83+
histogramVec.Add(labelValues, map[float64]uint64{20: 10}, 10, 200, lastFullMin)
8484
}
8585

8686
func TestCollectHistogramVec(t *testing.T) {
@@ -91,9 +91,10 @@ func TestCollectHistogramVec(t *testing.T) {
9191
},
9292
})
9393
histogramVec, labelValues := prepareHistgramVec()
94-
now := time.Now()
95-
histogramVec.Add(labelValues, map[float64]uint64{10: 10, 20: 20}, 30, 600, now.Add(-1*time.Minute))
96-
histogramVec.Add(labelValues, map[float64]uint64{10: 10, 20: 20}, 30, 600, now)
94+
95+
lastFullMin := config.GetLastFullMin()
96+
histogramVec.Add(labelValues, map[float64]uint64{10: 10, 20: 20}, 30, 600, lastFullMin.Add(-1*time.Minute))
97+
histogramVec.Add(labelValues, map[float64]uint64{10: 10, 20: 20}, 30, 600, lastFullMin)
9798

9899
ch := make(chan prometheus.Metric)
99100
go func() {

exporters/prometheus_exporter.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,5 +287,6 @@ func (pe *PrometheusExporter) StartPrometheusExporter() {
287287
handler := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
288288

289289
http.Handle("/metrics", handler)
290-
http.ListenAndServe(fmt.Sprintf(":%d", Config.Prometheus.Port), nil)
290+
err := http.ListenAndServe(fmt.Sprintf(":%d", Config.Prometheus.Port), nil)
291+
config.CheckError(err)
291292
}

exporters/prometheus_exporter_test.go

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -78,38 +78,69 @@ func TestCalculateBuckets(t *testing.T) {
7878
// AddCounter(metricIdentifier, desc, 5.0, 1755506438)
7979
// }
8080

81-
func TestPrometheusExporter(t *testing.T) {
81+
func TestPrometheusExporterWithoutTimestamps(t *testing.T) {
8282

83-
config := config.ParseConfig("./testdata/config.yaml")
84-
SetConf(&config)
83+
conf := config.ParseConfig("./testdata/config.yaml")
84+
SetConf(&conf)
8585

86-
//TODO: Fix tests
8786
prometheusExporter := NewPrometheusExporter()
88-
8987
go prometheusExporter.StartPrometheusExporter()
9088

91-
now := time.Now()
89+
time.Sleep(time.Second)
90+
91+
lastFullMin := config.GetLastFullMin()
9292

9393
//Export dsc file and check if its correctly exported
94-
dscData := dscparser.ReadFileWithCustomTimestamp("./testdata/test_dsc_file.xml", "loc", "ns", time.Now().Add(-time.Minute))
95-
prometheusExporter.ExportDSCData(dscData, now.Add(-1*time.Minute))
94+
dscData := dscparser.ReadFileWithCustomTimestamp("./testdata/test_dsc_file.xml", "loc", "ns", lastFullMin.Add(-time.Minute))
95+
prometheusExporter.ExportDSCData(dscData, lastFullMin.Add(-1*time.Minute))
9696

97-
metrics := getMetrics(t, config)
98-
expected_metrics, err := os.ReadFile("./testdata/expected_metrics.txt")
97+
metrics := getMetrics(t, conf)
98+
expected_metrics, err := os.ReadFile("./testdata/NoTimestamp/expected_metrics.txt")
9999
assert.NoError(t, err)
100-
assert.Equal(t, sortMetrics(string(expected_metrics)), sortMetrics(metrics))
100+
assert.Equal(t, string(expected_metrics), metrics)
101101

102102
//Export another dsc file and check if its correctly exported too
103-
dscData2 := dscparser.ReadFileWithCustomTimestamp("./testdata/test_dsc_file2.xml", "loc", "ns", time.Now())
104-
prometheusExporter.ExportDSCData(dscData2, now)
103+
dscData2 := dscparser.ReadFileWithCustomTimestamp("./testdata/test_dsc_file2.xml", "loc", "ns", lastFullMin)
104+
prometheusExporter.ExportDSCData(dscData2, lastFullMin)
105+
106+
metrics = getMetrics(t, conf)
107+
expected_metrics, err = os.ReadFile("./testdata/NoTimestamp/expected_metrics2.txt")
108+
assert.NoError(t, err)
109+
assert.Equal(t, string(expected_metrics), metrics)
110+
}
111+
112+
func TestPrometheusExporterWithTimestamps(t *testing.T) {
113+
114+
conf := config.ParseConfig("./testdata/config.yaml")
115+
conf.Prometheus.Timestamps = true
116+
SetConf(&conf)
117+
118+
prometheusExporter := NewPrometheusExporter()
119+
go prometheusExporter.StartPrometheusExporter()
120+
121+
lastFullMin := config.GetLastFullMin()
122+
123+
//DSC File to old, so its not collected
124+
dscData := dscparser.ReadFileWithCustomTimestamp("./testdata/test_dsc_file.xml", "loc", "ns", lastFullMin.Add(-3*time.Minute-time.Second))
125+
prometheusExporter.ExportDSCData(dscData, lastFullMin.Add(-3*time.Minute))
126+
127+
dscData = dscparser.ReadFileWithCustomTimestamp("./testdata/test_dsc_file.xml", "loc", "ns", lastFullMin.Add(-2*time.Minute))
128+
prometheusExporter.ExportDSCData(dscData, lastFullMin.Add(-2*time.Minute))
129+
130+
dscData = dscparser.ReadFileWithCustomTimestamp("./testdata/test_dsc_file.xml", "loc", "ns", lastFullMin.Add(-1*time.Minute))
131+
prometheusExporter.ExportDSCData(dscData, lastFullMin.Add(-1*time.Minute))
132+
133+
dscData = dscparser.ReadFileWithCustomTimestamp("./testdata/test_dsc_file2.xml", "loc", "ns", lastFullMin)
134+
prometheusExporter.ExportDSCData(dscData, lastFullMin)
105135

106-
metrics = getMetrics(t, config)
107-
expected_metrics, err = os.ReadFile("./testdata/expected_metrics2.txt")
136+
metrics := getMetrics(t, conf)
137+
expected_metrics, err := os.ReadFile("./testdata/Timestamp/expected_metrics.txt")
108138
assert.NoError(t, err)
109-
assert.Equal(t, sortMetrics(string(expected_metrics)), sortMetrics(metrics))
139+
assert.Equal(t, string(expected_metrics), metrics)
110140
}
111141

112-
//TODO Test with timestamp
142+
//TODO: Test with timestamp
143+
//TODO: Check order of metrics
113144

114145
func TestNewPrometheusExporter(t *testing.T) {
115146
config := config.ParseConfig("./testdata/config.yaml")
File renamed without changes.
File renamed without changes.

exporters/testdata/Timestamp/expected_metrics.txt

Whitespace-only changes.

exporters/testdata/config.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ prometheus:
2525
start: -1
2626
width: 512
2727
count: 6
28-
timestamps: false
28+
timestamps: false
29+
windowsize: 3

scheduler/scheduler_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@ func getMetrics(t *testing.T, config config.Config) string {
6969
func copyDSCFiles(t *testing.T) {
7070

7171
os.RemoveAll("./testdata/tmp/")
72-
config := config.ParseConfigText([]byte("data: ./testdata/dsc-data"))
73-
dscFiles := ListDSCFiles(config)
72+
conf := config.ParseConfigText([]byte("data: ./testdata/dsc-data"))
73+
dscFiles := ListDSCFiles(conf)
7474

75-
now := time.Now()
75+
lastFullMin := config.GetLastFullMin()
7676
newestTestData := time.Unix(1741170600, 0)
7777
for _, dscFile := range dscFiles {
7878

@@ -81,7 +81,7 @@ func copyDSCFiles(t *testing.T) {
8181
content := string(fileContent)
8282

8383
diff := newestTestData.Sub(dscFile.StopTime)
84-
newStopTime := now.Add(-diff)
84+
newStopTime := lastFullMin.Add(-diff)
8585

8686
content = strings.ReplaceAll(
8787
content,

0 commit comments

Comments
 (0)