-
-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
3,367 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
const Sql = require('@cloki/clickhouse-sql') | ||
const prometheus = require('../wasm_parts/main') | ||
const { rawRequest } = require('../lib/db/clickhouse') | ||
const { DATABASE_NAME } = require('../lib/utils') | ||
const { clusterName } = require('../common') | ||
const _dist = clusterName ? '_dist' : '' | ||
/** | ||
* | ||
* @param query {string} | ||
* @param startMs {number} | ||
* @param endMs {number} | ||
* @param stepMs {number} | ||
*/ | ||
module.exports.rangeQuery = async (query, startMs, endMs, stepMs) => { | ||
const resp = await prometheus.pqlRangeQuery(query, startMs, endMs, stepMs, module.exports.getData) | ||
return JSON.parse(resp) | ||
} | ||
|
||
module.exports.instantQuery = async (query, timeMs) => { | ||
const resp = await prometheus.pqlInstantQuery(query, timeMs, module.exports.getData) | ||
return JSON.parse(resp) | ||
} | ||
|
||
module.exports.getData = async (matchers, fromMs, toMs) => { | ||
const matches = [] | ||
for (const matcher of matchers) { | ||
const _matcher = [ | ||
Sql.Eq('key', matcher[0]) | ||
] | ||
switch (matcher[1]) { | ||
case '=': | ||
_matcher.push(Sql.Eq('val', matcher[2])) | ||
break | ||
case '!=': | ||
_matcher.push(Sql.Ne('val', matcher[2])) | ||
break | ||
case '=~': | ||
_matcher.push(Sql.Eq(new Sql.Raw(`match(val, ${Sql.quoteVal(matcher[2])})`), 1)) | ||
break | ||
case '!~': | ||
_matcher.push(Sql.Ne(Sql.Raw(`match(val, ${Sql.quoteVal(matcher[2])})`), 1)) | ||
} | ||
matches.push(Sql.And(..._matcher)) | ||
} | ||
|
||
const idx = (new Sql.Select()) | ||
.select('fingerprint') | ||
.from('time_series_gin') | ||
.where(Sql.Or(...matches)) | ||
.having( | ||
Sql.Eq( | ||
new Sql.Raw('groupBitOr(' + matches.map( | ||
(m, i) => new Sql.Raw(`bitShiftLeft((${m})::UInt64, ${i})`) | ||
).join('+') + ')'), (1 << matches.length) - 1) | ||
).groupBy('fingerprint') | ||
const withIdx = new Sql.With('idx', idx, false) | ||
const raw = (new Sql.Select()) | ||
.with(withIdx) | ||
.select( | ||
[new Sql.Raw('argMaxMerge(last)'), 'value'], | ||
'fingerprint', | ||
[new Sql.Raw('intDiv(timestamp_ns, 15000000000) * 15000'), 'timestamp_ms']) | ||
.from('metrics_15s') | ||
.where( | ||
new Sql.And( | ||
new Sql.In('fingerprint', 'in', new Sql.WithReference(withIdx)), | ||
Sql.Gte('timestamp_ns', new Sql.Raw(`${fromMs}000000`)), | ||
Sql.Lte('timestamp_ns', new Sql.Raw(`${toMs}000000`)) | ||
) | ||
).groupBy('fingerprint', 'timestamp_ms') | ||
.orderBy('fingerprint', 'timestamp_ms') | ||
const timeSeries = (new Sql.Select()) | ||
.select( | ||
'fingerprint', | ||
[new Sql.Raw('arraySort(JSONExtractKeysAndValues(labels, \'String\'))'), 'labels'] | ||
).from('time_series') | ||
.where(new Sql.In('fingerprint', 'in', new Sql.WithReference(withIdx))) | ||
const withRaw = new Sql.With('raw', raw, false) | ||
const withTimeSeries = new Sql.With('timeSeries', timeSeries, false) | ||
const res = (new Sql.Select()) | ||
.with(withRaw, withTimeSeries) | ||
.select( | ||
[new Sql.Raw('any(labels)'), 'stream'], | ||
[new Sql.Raw('arraySort(groupArray((raw.timestamp_ms, raw.value)))'), 'values'] | ||
).from([new Sql.WithReference(withRaw), 'raw']) | ||
.join( | ||
[new Sql.WithReference(withTimeSeries), 'time_series'], | ||
'any left', | ||
Sql.Eq('time_series.fingerprint', new Sql.Raw('raw.fingerprint')) | ||
).groupBy('raw.fingerprint') | ||
.orderBy('raw.fingerprint') | ||
|
||
const db = DATABASE_NAME() | ||
console.log('!!!!!!!!!!! ' + res.toString()) | ||
const data = await rawRequest(res.toString() + ' FORMAT RowBinary', | ||
null, db, { responseType: 'arraybuffer' }) | ||
return new Uint8Array(data.data) | ||
} | ||
|
||
prometheus.getData = module.exports.getData |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
const { request } = require('./index'); | ||
|
||
(async () => { | ||
await new Promise((resolve => setTimeout(resolve, 1000))); | ||
const res = await request('test{}', Date.now() - 300000, Date.now(), 15000) | ||
console.log(res) | ||
})() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
module wasm_parts | ||
|
||
replace ( | ||
cloud.google.com/go v0.65.0 => cloud.google.com/go v0.102.1 | ||
github.com/InfluxCommunity/influxdb3-go v0.2.0 => github.com/akvlad/influxdb3-go v0.0.1 | ||
github.com/docker/distribution v2.7.1+incompatible => github.com/docker/distribution v2.8.0+incompatible | ||
k8s.io/client-go v12.0.0+incompatible => k8s.io/client-go v0.22.1 | ||
github.com/json-iterator/go v1.1.12 => ./json.iterator | ||
) | ||
|
||
require github.com/prometheus/prometheus v1.8.2-0.20220714142409-b41e0750abf5 | ||
|
||
require ( | ||
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 // indirect | ||
github.com/aws/aws-sdk-go v1.44.45 // indirect | ||
github.com/beorn7/perks v1.0.1 // indirect | ||
github.com/cespare/xxhash/v2 v2.1.2 // indirect | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/dennwc/varint v1.0.0 // indirect | ||
github.com/edsrzf/mmap-go v1.1.0 // indirect | ||
github.com/felixge/httpsnoop v1.0.3 // indirect | ||
github.com/go-kit/log v0.2.1 // indirect | ||
github.com/go-logfmt/logfmt v0.5.1 // indirect | ||
github.com/go-logr/logr v1.2.3 // indirect | ||
github.com/go-logr/stdr v1.2.2 // indirect | ||
github.com/gogo/protobuf v1.3.2 // indirect | ||
github.com/golang/protobuf v1.5.2 // indirect | ||
github.com/golang/snappy v0.0.4 // indirect | ||
github.com/grafana/regexp v0.0.0-20220304095617-2e8d9baf4ac2 // indirect | ||
github.com/jmespath/go-jmespath v0.4.0 // indirect | ||
github.com/jpillora/backoff v1.0.0 // indirect | ||
github.com/json-iterator/go v1.1.12 // indirect | ||
github.com/julienschmidt/httprouter v1.3.0 // indirect | ||
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect | ||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect | ||
github.com/modern-go/reflect2 v1.0.2 // indirect | ||
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f // indirect | ||
github.com/oklog/ulid v1.3.1 // indirect | ||
github.com/pkg/errors v0.9.1 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
github.com/prometheus/client_golang v1.12.2 // indirect | ||
github.com/prometheus/client_model v0.2.0 // indirect | ||
github.com/prometheus/common v0.37.0 // indirect | ||
github.com/prometheus/common/sigv4 v0.1.0 // indirect | ||
github.com/prometheus/procfs v0.7.3 // indirect | ||
github.com/stretchr/testify v1.8.0 // indirect | ||
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.32.0 // indirect | ||
go.opentelemetry.io/otel v1.7.0 // indirect | ||
go.opentelemetry.io/otel/metric v0.30.0 // indirect | ||
go.opentelemetry.io/otel/trace v1.7.0 // indirect | ||
go.uber.org/atomic v1.9.0 // indirect | ||
go.uber.org/goleak v1.1.12 // indirect | ||
golang.org/x/net v0.0.0-20220624214902-1bab6f366d9e // indirect | ||
golang.org/x/oauth2 v0.0.0-20220628200809-02e64fa58f26 // indirect | ||
golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f // indirect | ||
golang.org/x/sys v0.0.0-20220627191245-f75cf1eec38b // indirect | ||
golang.org/x/text v0.3.7 // indirect | ||
golang.org/x/time v0.0.0-20220609170525-579cf78fd858 // indirect | ||
google.golang.org/appengine v1.6.7 // indirect | ||
google.golang.org/protobuf v1.28.0 // indirect | ||
gopkg.in/yaml.v2 v2.4.0 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
) | ||
|
||
go 1.20 |
Oops, something went wrong.