Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[query] Add lookback duration from query config #2913

Merged
merged 2 commits into from
Nov 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions scripts/docker-integration-tests/prometheus/m3coordinator.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ query:
value: hidden
strip:
- restricted_metrics_type

lookbackDuration: 10m
20 changes: 20 additions & 0 deletions scripts/docker-integration-tests/prometheus/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,25 @@ function test_prometheus_remote_write_map_tags {
retry_with_backoff prometheus_query_native
}

function test_query_lookback_applied {
# Note: this test depends on the config in m3coordinator.yml for this test
# and the following config value "lookbackDuration: 10m".
echo "Test lookback config respected"
now=$(date +"%s")
# Write into past less than the lookback duration.
eight_mins_ago=$(( now - 480 ))
prometheus_remote_write \
"lookback_test" $eight_mins_ago 42 \
true "Expected request to succeed" \
200 "Expected request to return status code 200" \
"unaggregated"

# Now query and ensure that the latest timestamp is within the last two steps
# from now.
ATTEMPTS=10 TIMEOUT=2 MAX_TIMEOUT=4 retry_with_backoff \
'[[ $(curl -s "0.0.0.0:7201/api/v1/query_range?query=lookback_test&step=15&start=$(expr $(date "+%s") - 600)&end=$(date "+%s")" | jq -r ".data.result[0].values[-1][0]") -gt $(expr $(date "+%s") - 30) ]]'
}

function test_query_limits_applied {
# Test the default series limit applied when directly querying
# coordinator (limit set to 100 in m3coordinator.yml)
Expand Down Expand Up @@ -373,6 +392,7 @@ test_prometheus_remote_write_empty_label_value_returns_400_status_code
test_prometheus_remote_write_duplicate_label_returns_400_status_code
test_prometheus_remote_write_too_old_returns_400_status_code
test_prometheus_remote_write_restrict_metrics_type
test_query_lookback_applied
test_query_limits_applied
test_query_restrict_metrics_type
test_prometheus_query_native_timeout
Expand Down
2 changes: 1 addition & 1 deletion src/cmd/services/m3query/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ var (
ExtendedMetrics: &defaultMetricsExtendedMetricsType,
}

// 5m is the default lookback in Prometheus
// 5m is the default lookback in Prometheus.
defaultLookbackDuration = 5 * time.Minute

defaultCarbonIngesterAggregationType = aggregation.Mean
Expand Down
26 changes: 18 additions & 8 deletions src/query/server/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -479,8 +479,12 @@ func Run(runOpts RunOptions) {
graphiteStorageOpts.RenderSeriesAllNaNs = cfg.Carbon.RenderSeriesAllNaNs
}

prometheusEngine := newPromQLEngine(cfg.Query, prometheusEngineRegistry,
prometheusEngine, err := newPromQLEngine(cfg, prometheusEngineRegistry,
instrumentOptions)
if err != nil {
logger.Fatal("unable to create PromQL engine", zap.Error(err))
}

handlerOptions, err := options.NewHandlerOptions(downsamplerAndWriter,
tagOptions, engine, prometheusEngine, m3dbClusters, clusterClient, cfg,
runOpts.DBConfig, fetchOptsBuilder, queryCtxOpts,
Expand Down Expand Up @@ -1154,18 +1158,24 @@ func newDownsamplerAndWriter(
}

func newPromQLEngine(
cfg config.QueryConfiguration,
cfg config.Configuration,
registry *extprom.Registry,
instrumentOpts instrument.Options,
) *prometheuspromql.Engine {
) (*prometheuspromql.Engine, error) {
lookbackDelta, err := cfg.LookbackDurationOrDefault()
if err != nil {
return nil, err
}

var (
kitLogger = kitlogzap.NewZapSugarLogger(instrumentOpts.Logger(), zapcore.InfoLevel)
opts = prometheuspromql.EngineOpts{
Logger: log.With(kitLogger, "component", "prometheus_engine"),
Reg: registry,
MaxSamples: cfg.Prometheus.MaxSamplesPerQueryOrDefault(),
Timeout: cfg.TimeoutOrDefault(),
Logger: log.With(kitLogger, "component", "prometheus_engine"),
Reg: registry,
MaxSamples: cfg.Query.Prometheus.MaxSamplesPerQueryOrDefault(),
Timeout: cfg.Query.TimeoutOrDefault(),
LookbackDelta: lookbackDelta,
}
)
return prometheuspromql.NewEngine(opts)
return prometheuspromql.NewEngine(opts), nil
}