diff --git a/pkg/tsdb/testdata/scenarios.go b/pkg/tsdb/testdata/scenarios.go index 0a521894f43..3e7aa0541a8 100644 --- a/pkg/tsdb/testdata/scenarios.go +++ b/pkg/tsdb/testdata/scenarios.go @@ -2,6 +2,7 @@ package testdata import ( "encoding/json" + "fmt" "math" "math/rand" "strconv" @@ -261,6 +262,84 @@ func init() { return queryRes }, }) + + registerScenario(&Scenario{ + Id: "logs", + Name: "Logs", + + Handler: func(query *tsdb.Query, context *tsdb.TsdbQuery) *tsdb.QueryResult { + from := context.TimeRange.GetFromAsMsEpoch() + to := context.TimeRange.GetToAsMsEpoch() + lines := query.Model.Get("lines").MustInt64(10) + includeLevelColumn := query.Model.Get("levelColumn").MustBool(false) + + logLevelGenerator := newRandomStringProvider([]string{ + "emerg", + "alert", + "crit", + "critical", + "warn", + "warning", + "err", + "eror", + "error", + "info", + "notice", + "dbug", + "debug", + "trace", + "", + }) + containerIDGenerator := newRandomStringProvider([]string{ + "f36a9eaa6d34310686f2b851655212023a216de955cbcc764210cefa71179b1a", + "5a354a630364f3742c602f315132e16def594fe68b1e4a195b2fce628e24c97a", + }) + hostnameGenerator := newRandomStringProvider([]string{ + "srv-001", + "srv-002", + }) + + table := tsdb.Table{ + Columns: []tsdb.TableColumn{ + {Text: "time"}, + {Text: "message"}, + {Text: "container_id"}, + {Text: "hostname"}, + }, + Rows: []tsdb.RowValues{}, + } + + if includeLevelColumn { + table.Columns = append(table.Columns, tsdb.TableColumn{Text: "level"}) + } + + for i := int64(0); i < lines && to > from; i++ { + row := tsdb.RowValues{float64(to)} + + logLevel := logLevelGenerator.Next() + timeFormatted := time.Unix(to/1000, 0).Format(time.RFC3339) + lvlString := "" + if !includeLevelColumn { + lvlString = fmt.Sprintf("lvl=%s ", logLevel) + } + + row = append(row, fmt.Sprintf("t=%s %smsg=\"Request Completed\" logger=context userId=1 orgId=1 uname=admin method=GET path=/api/datasources/proxy/152/api/prom/label status=502 remote_addr=[::1] time_ms=1 size=0 referer=\"http://localhost:3000/explore?left=%%5B%%22now-6h%%22,%%22now%%22,%%22Prometheus%%202.x%%22,%%7B%%7D,%%7B%%22ui%%22:%%5Btrue,true,true,%%22none%%22%%5D%%7D%%5D\"", timeFormatted, lvlString)) + row = append(row, containerIDGenerator.Next()) + row = append(row, hostnameGenerator.Next()) + + if includeLevelColumn { + row = append(row, logLevel) + } + + table.Rows = append(table.Rows, row) + to -= query.IntervalMs + } + + queryRes := tsdb.NewQueryResult() + queryRes.Tables = append(queryRes.Tables, &table) + return queryRes + }, + }) } func getRandomWalk(query *tsdb.Query, tsdbQuery *tsdb.TsdbQuery) *tsdb.QueryResult { diff --git a/pkg/tsdb/testdata/utils.go b/pkg/tsdb/testdata/utils.go new file mode 100644 index 00000000000..85c02e8a296 --- /dev/null +++ b/pkg/tsdb/testdata/utils.go @@ -0,0 +1,22 @@ +package testdata + +import ( + "math/rand" + "time" +) + +type randomStringProvider struct { + r *rand.Rand + data []string +} + +func newRandomStringProvider(data []string) *randomStringProvider { + return &randomStringProvider{ + r: rand.New(rand.NewSource(time.Now().UnixNano())), + data: data, + } +} + +func (p *randomStringProvider) Next() string { + return p.data[p.r.Int31n(int32(len(p.data)))] +} diff --git a/public/app/plugins/datasource/testdata/datasource.ts b/public/app/plugins/datasource/testdata/datasource.ts index 0f69028f784..c3b706153b3 100644 --- a/public/app/plugins/datasource/testdata/datasource.ts +++ b/public/app/plugins/datasource/testdata/datasource.ts @@ -32,10 +32,11 @@ export class TestDataDatasource extends DataSourceApi { scenarioId: item.scenarioId, intervalMs: options.intervalMs, maxDataPoints: options.maxDataPoints, + datasourceId: this.id, stringInput: item.stringInput, points: item.points, alias: item.alias, - datasourceId: this.id, + ...item, }; }); diff --git a/public/app/plugins/datasource/testdata/partials/query.editor.html b/public/app/plugins/datasource/testdata/partials/query.editor.html index e982ce9fb61..e70abb4d96d 100644 --- a/public/app/plugins/datasource/testdata/partials/query.editor.html +++ b/public/app/plugins/datasource/testdata/partials/query.editor.html @@ -40,8 +40,8 @@
-
-
-
-
+ +
+
+ + +
+
+ +
+