Skip to content

Commit 761e919

Browse files
authored
show error on invalid metric name (#430)
* show error on invalid metric name * fmt * fix tests * add test
1 parent 2ce27ed commit 761e919

File tree

4 files changed

+46
-73
lines changed

4 files changed

+46
-73
lines changed

Diff for: pkg/appender/appender.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ type MetricsCache struct {
131131
lastError error
132132
performanceReporter *performance.MetricReporter
133133

134-
stopChan chan int
134+
stopChan chan int
135135
}
136136

137137
func NewMetricsCache(container v3io.Container, logger logger.Logger, cfg *config.V3ioConfig,
@@ -219,6 +219,10 @@ func (mc *MetricsCache) Add(lset utils.LabelsIfc, t int64, v interface{}) (uint6
219219
}
220220

221221
name, key, hash := lset.GetKey()
222+
err = utils.IsValidMetricName(name)
223+
if err != nil {
224+
return 0, err
225+
}
222226
metric, ok := mc.getMetric(name, hash)
223227

224228
var aggrMetrics []*MetricState

Diff for: pkg/pquerier/collector.go

+9-6
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,11 @@ func mainCollector(ctx *selectQueryContext, responseChannel chan *qryResults) {
6666

6767
for res := range responseChannel {
6868
if res.IsRawQuery() {
69-
rawCollector(ctx, res)
69+
err := rawCollector(ctx, res)
70+
if err != nil {
71+
ctx.errorChannel <- err
72+
return
73+
}
7074
} else {
7175
err := res.frame.addMetricIfNotExist(res.name, ctx.getResultBucketsSize(), res.IsServerAggregates())
7276
if err != nil {
@@ -104,7 +108,7 @@ func mainCollector(ctx *selectQueryContext, responseChannel chan *qryResults) {
104108
}
105109
}
106110

107-
func rawCollector(ctx *selectQueryContext, res *qryResults) {
111+
func rawCollector(ctx *selectQueryContext, res *qryResults) error {
108112
ctx.logger.Debug("using Raw Collector for metric %v", res.name)
109113

110114
if res.frame.isWildcardSelect {
@@ -114,8 +118,7 @@ func rawCollector(ctx *selectQueryContext, res *qryResults) {
114118
} else {
115119
series, err := NewRawSeries(res, ctx.logger.GetChild("v3ioRawSeries"))
116120
if err != nil {
117-
ctx.errorChannel <- err
118-
return
121+
return err
119122
}
120123
res.frame.rawColumns = append(res.frame.rawColumns, series)
121124
res.frame.columnByName[res.name] = len(res.frame.rawColumns) - 1
@@ -128,12 +131,12 @@ func rawCollector(ctx *selectQueryContext, res *qryResults) {
128131
} else {
129132
series, err := NewRawSeries(res, ctx.logger.GetChild("v3ioRawSeries"))
130133
if err != nil {
131-
ctx.errorChannel <- err
132-
return
134+
return err
133135
}
134136
res.frame.rawColumns[columnIndex] = series
135137
}
136138
}
139+
return nil
137140
}
138141

139142
func aggregateClientAggregates(ctx *selectQueryContext, res *qryResults) {

Diff for: pkg/pquerier/pqueriertest/raw_query_integration_test.go

-36
Original file line numberDiff line numberDiff line change
@@ -233,42 +233,6 @@ func (suite *testRawQuerySuite) TestQueryWithBadTimeParameters() {
233233
}
234234
}
235235

236-
func (suite *testRawQuerySuite) TestQueryMetricWithDashInTheName() { // IG-8585
237-
adapter, err := tsdb.NewV3ioAdapter(suite.v3ioConfig, nil, nil)
238-
if err != nil {
239-
suite.T().Fatalf("failed to create v3io adapter. reason: %s", err)
240-
}
241-
242-
labels1 := utils.LabelsFromStringList("os", "linux")
243-
numberOfEvents := 10
244-
eventsInterval := 60 * 1000
245-
246-
expectedData := []tsdbtest.DataPoint{{suite.basicQueryTime, 10},
247-
{int64(suite.basicQueryTime + tsdbtest.MinuteInMillis), 20},
248-
{suite.basicQueryTime + 2*tsdbtest.MinuteInMillis, 30},
249-
{suite.basicQueryTime + 3*tsdbtest.MinuteInMillis, 40}}
250-
testParams := tsdbtest.NewTestParams(suite.T(),
251-
tsdbtest.TestOption{
252-
Key: tsdbtest.OptTimeSeries,
253-
Value: tsdbtest.TimeSeries{tsdbtest.Metric{
254-
Name: "cool-cpu",
255-
Labels: labels1,
256-
Data: expectedData},
257-
}})
258-
tsdbtest.InsertData(suite.T(), testParams)
259-
260-
querierV2, err := adapter.QuerierV2()
261-
if err != nil {
262-
suite.T().Fatalf("Failed to create querier v2, err: %v", err)
263-
}
264-
265-
params := &pquerier.SelectParams{From: suite.basicQueryTime, To: suite.basicQueryTime + int64(numberOfEvents*eventsInterval)}
266-
_, err = querierV2.Select(params)
267-
if err == nil {
268-
suite.T().Fatalf("expected an error but finish succesfully")
269-
}
270-
}
271-
272236
func (suite *testRawQuerySuite) TestSelectRawDataByRequestedColumns() {
273237
adapter, err := tsdb.NewV3ioAdapter(suite.v3ioConfig, nil, nil)
274238
if err != nil {

Diff for: pkg/tsdb/v3iotsdb_integration_test.go

+32-30
Original file line numberDiff line numberDiff line change
@@ -89,26 +89,12 @@ func TestIngestData(t *testing.T) {
8989
}}},
9090
),
9191
},
92-
{desc: "Should ingest record with a dash in the metric name (IG-8585)",
93-
params: tsdbtest.NewTestParams(t,
94-
tsdbtest.TestOption{
95-
Key: tsdbtest.OptTimeSeries,
96-
Value: tsdbtest.TimeSeries{tsdbtest.Metric{
97-
Name: "cool-cpu",
98-
Labels: utils.LabelsFromStringList("os", "linux", "iguaz", "yesplease"),
99-
Data: []tsdbtest.DataPoint{
100-
{Time: 1532940510, Value: 314.3},
101-
{Time: 1532940510 + 5, Value: 300.3},
102-
{Time: 1532940510 - 10, Value: 3234.6}},
103-
}}},
104-
),
105-
},
10692
{desc: "Should ingest into first partition in epoch without corruption (TSDB-67)",
10793
params: tsdbtest.NewTestParams(t,
10894
tsdbtest.TestOption{
10995
Key: tsdbtest.OptTimeSeries,
11096
Value: tsdbtest.TimeSeries{tsdbtest.Metric{
111-
Name: "cool-cpu",
97+
Name: "coolcpu",
11298
Labels: utils.LabelsFromStringList("os", "linux", "iguaz", "yesplease"),
11399
Data: []tsdbtest.DataPoint{
114100
{Time: 10, Value: 314.3},
@@ -216,6 +202,37 @@ func TestIngestDataWithSameTimestamp(t *testing.T) {
216202
tsdbtest.ValidateCountOfSamples(t, adapter, "", 2, baseTime-1*tsdbtest.HoursInMillis, baseTime+1*tsdbtest.HoursInMillis, -1)
217203
}
218204

205+
func TestWriteMetricWithDashInName(t *testing.T) {
206+
testParams := tsdbtest.NewTestParams(t,
207+
tsdbtest.TestOption{
208+
Key: tsdbtest.OptTimeSeries,
209+
Value: tsdbtest.TimeSeries{tsdbtest.Metric{
210+
Name: "cpu-1",
211+
Labels: utils.LabelsFromStringList("testLabel", "balbala"),
212+
Data: []tsdbtest.DataPoint{{Time: 1532940510, Value: 314.3}},
213+
}}})
214+
defer tsdbtest.SetUp(t, testParams)()
215+
216+
adapter, err := NewV3ioAdapter(testParams.V3ioConfig(), nil, nil)
217+
if err != nil {
218+
t.Fatalf("Failed to create v3io adapter. reason: %s", err)
219+
}
220+
221+
appender, err := adapter.Appender()
222+
if err != nil {
223+
t.Fatalf("Failed to get appender. reason: %s", err)
224+
}
225+
for _, dp := range testParams.TimeSeries() {
226+
labels := utils.Labels{utils.Label{Name: "__name__", Value: dp.Name}}
227+
labels = append(labels, dp.Labels...)
228+
229+
_, err := appender.Add(labels, dp.Data[0].Time, dp.Data[0].Value)
230+
if err == nil {
231+
t.Fatalf("Test should have failed")
232+
}
233+
}
234+
}
235+
219236
func TestQueryData(t *testing.T) {
220237
testCases := []struct {
221238
desc string
@@ -296,21 +313,6 @@ func TestQueryData(t *testing.T) {
296313
step: defaultStepMs,
297314
expected: map[string][]tsdbtest.DataPoint{"": {{Time: 1532940510, Value: 31.3}}}},
298315

299-
{desc: "Should ingest and query data with '-' in the metric name (IG-8585)",
300-
testParams: tsdbtest.NewTestParams(t,
301-
tsdbtest.TestOption{
302-
Key: tsdbtest.OptTimeSeries,
303-
Value: tsdbtest.TimeSeries{tsdbtest.Metric{
304-
Name: "cool-cpu",
305-
Labels: utils.LabelsFromStringList("os", "linux", "iguaz", "yesplease"),
306-
Data: []tsdbtest.DataPoint{{Time: 1532940510, Value: 314.3}},
307-
}}},
308-
),
309-
from: 0,
310-
to: 1532940510 + 1,
311-
step: defaultStepMs,
312-
expected: map[string][]tsdbtest.DataPoint{"": {{Time: 1532940510, Value: 314.3}}}},
313-
314316
{desc: "Should ingest and query by time",
315317
testParams: tsdbtest.NewTestParams(t,
316318
tsdbtest.TestOption{

0 commit comments

Comments
 (0)