2015-06-15 18:36:32 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								// Copyright 2015 The Prometheus Authors
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// Licensed under the Apache License, Version 2.0 (the "License");
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// you may not use this file except in compliance with the License.
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// You may obtain a copy of the License at
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								//
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// http://www.apache.org/licenses/LICENSE-2.0
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								//
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// Unless required by applicable law or agreed to in writing, software
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// distributed under the License is distributed on an "AS IS" BASIS,
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// See the License for the specific language governing permissions and
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// limitations under the License.
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-02-18 06:52:44 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// The main package for the Prometheus server executable.
  
						 
					
						
							
								
									
										
										
										
											2015-06-15 18:36:32 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								package  main  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								import  (  
						 
					
						
							
								
									
										
										
										
											2017-10-25 12:21:42 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"context" 
							 
						 
					
						
							
								
									
										
										
										
											2015-06-15 18:23:02 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"fmt" 
							 
						 
					
						
							
								
									
										
										
										
											2020-04-23 08:05:55 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"io" 
							 
						 
					
						
							
								
									
										
										
										
											2020-02-17 19:41:04 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"math" 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-21 00:48:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"net" 
							 
						 
					
						
							
								
									
										
										
										
											2017-10-06 18:22:19 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"net/http" 
							 
						 
					
						
							
								
									
										
										
										
											2015-06-15 18:36:32 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									_  "net/http/pprof"  // Comment this line to disable pprof endpoint.
 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-21 00:48:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"net/url" 
							 
						 
					
						
							
								
									
										
										
										
											2015-06-15 18:36:32 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									"os" 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									"os/signal" 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-20 23:38:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"path/filepath" 
							 
						 
					
						
							
								
									
										
										
										
											2019-01-17 23:01:06 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"regexp" 
							 
						 
					
						
							
								
									
										
										
										
											2017-09-04 19:10:32 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"runtime" 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-21 00:48:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"strings" 
							 
						 
					
						
							
								
									
										
										
										
											2018-01-18 02:14:24 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"sync" 
							 
						 
					
						
							
								
									
										
										
										
											2015-06-15 18:36:32 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									"syscall" 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									"time" 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-02-18 19:25:36 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"github.com/alecthomas/units" 
							 
						 
					
						
							
								
									
										
										
										
											2017-08-12 02:45:52 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"github.com/go-kit/kit/log" 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									"github.com/go-kit/kit/log/level" 
							 
						 
					
						
							
								
									
										
										
										
											2019-03-26 07:01:12 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									conntrack  "github.com/mwitkow/go-conntrack" 
							 
						 
					
						
							
								
									
										
										
										
											2019-04-19 20:55:28 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"github.com/oklog/run" 
							 
						 
					
						
							
								
									
										
										
										
											2020-04-23 08:05:55 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"github.com/opentracing/opentracing-go" 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-21 00:48:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"github.com/pkg/errors" 
							 
						 
					
						
							
								
									
										
										
										
											2015-06-24 00:04:04 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"github.com/prometheus/client_golang/prometheus" 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-21 00:48:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"github.com/prometheus/common/model" 
							 
						 
					
						
							
								
									
										
										
										
											2019-03-26 07:01:12 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"github.com/prometheus/common/promlog" 
							 
						 
					
						
							
								
									
										
										
										
											2016-05-05 19:46:51 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"github.com/prometheus/common/version" 
							 
						 
					
						
							
								
									
										
										
										
											2020-04-23 08:05:55 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									jcfg  "github.com/uber/jaeger-client-go/config" 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									jprom  "github.com/uber/jaeger-lib/metrics/prometheus" 
							 
						 
					
						
							
								
									
										
										
										
											2020-07-30 15:45:42 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"go.uber.org/atomic" 
							 
						 
					
						
							
								
									
										
										
										
											2019-03-26 07:01:12 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									kingpin  "gopkg.in/alecthomas/kingpin.v2" 
							 
						 
					
						
							
								
									
										
										
										
											2019-01-04 05:44:29 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"k8s.io/klog" 
							 
						 
					
						
							
								
									
										
											 
										
											
												promql: Allow per-query contexts.
For Weaveworks' Frankenstein, we need to support multitenancy. In
Frankenstein, we initially solved this without modifying the promql
package at all: we constructed a new promql.Engine for every
query and injected a storage implementation into that engine which would
be primed to only collect data for a given user.
This is problematic to upstream, however. Prometheus assumes that there
is only one engine: the query concurrency gate is part of the engine,
and the engine contains one central cancellable context to shut down all
queries. Also, creating a new engine for every query seems like overkill.
Thus, we want to be able to pass per-query contexts into a single engine.
This change gets rid of the promql.Engine's built-in base context and
allows passing in a per-query context instead. Central cancellation of
all queries is still possible by deriving all passed-in contexts from
one central one, but this is now the responsibility of the caller. The
central query context is now created in main() and passed into the
relevant components (web handler / API, rule manager).
In a next step, the per-query context would have to be passed to the
storage implementation, so that the storage can implement multi-tenancy
or other features based on the contextual information.
											 
										 
										
											2016-09-15 19:52:50 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-09-09 00:34:20 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									promlogflag  "github.com/prometheus/common/promlog/flag" 
							 
						 
					
						
							
								
									
										
										
										
											2015-06-15 18:36:32 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									"github.com/prometheus/prometheus/config" 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-25 21:13:54 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"github.com/prometheus/prometheus/discovery" 
							 
						 
					
						
							
								
									
										
										
										
											2016-03-01 19:37:22 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"github.com/prometheus/prometheus/notifier" 
							 
						 
					
						
							
								
									
										
										
										
											2020-02-17 19:41:04 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"github.com/prometheus/prometheus/pkg/labels" 
							 
						 
					
						
							
								
									
										
										
										
											2020-01-08 21:28:43 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"github.com/prometheus/prometheus/pkg/logging" 
							 
						 
					
						
							
								
									
										
										
										
											2019-01-17 23:01:06 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"github.com/prometheus/prometheus/pkg/relabel" 
							 
						 
					
						
							
								
									
										
										
										
											2019-03-26 07:01:12 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									prom_runtime  "github.com/prometheus/prometheus/pkg/runtime" 
							 
						 
					
						
							
								
									
										
										
										
											2015-06-15 18:36:32 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									"github.com/prometheus/prometheus/promql" 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									"github.com/prometheus/prometheus/rules" 
							 
						 
					
						
							
								
									
										
										
										
											2018-02-01 17:55:07 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"github.com/prometheus/prometheus/scrape" 
							 
						 
					
						
							
								
									
										
										
										
											2017-05-10 17:44:13 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"github.com/prometheus/prometheus/storage" 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									"github.com/prometheus/prometheus/storage/remote" 
							 
						 
					
						
							
								
									
										
										
										
											2020-02-06 23:58:38 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"github.com/prometheus/prometheus/tsdb" 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-24 15:59:05 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"github.com/prometheus/prometheus/util/strutil" 
							 
						 
					
						
							
								
									
										
										
										
											2015-06-15 18:36:32 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									"github.com/prometheus/prometheus/web" 
							 
						 
					
						
							
								
									
										
										
										
											2020-08-20 20:48:26 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									_  "github.com/prometheus/prometheus/discovery/install"  // Register service discovery implementations.
 
							 
						 
					
						
							
								
									
										
										
										
											2015-06-15 18:36:32 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								)  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-06-21 00:48:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								var  (  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									configSuccess  =  prometheus . NewGauge ( prometheus . GaugeOpts { 
							 
						 
					
						
							
								
									
										
										
										
											2018-03-22 00:08:37 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										Name :  "prometheus_config_last_reload_successful" , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										Help :  "Whether the last configuration reload attempt was successful." , 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-21 00:48:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									} ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									configSuccessTime  =  prometheus . NewGauge ( prometheus . GaugeOpts { 
							 
						 
					
						
							
								
									
										
										
										
											2018-03-22 00:08:37 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										Name :  "prometheus_config_last_reload_success_timestamp_seconds" , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										Help :  "Timestamp of the last successful configuration reload." , 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-21 00:48:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									} ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-01-18 21:48:36 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									defaultRetentionString    =  "15d" 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									defaultRetentionDuration  model . Duration 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-21 00:48:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								)  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								func  init ( )  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									prometheus . MustRegister ( version . NewCollector ( "prometheus" ) ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-01-18 21:48:36 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									var  err  error 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									defaultRetentionDuration ,  err  =  model . ParseDuration ( defaultRetentionString ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									if  err  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										panic ( err ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-21 00:48:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-06-15 18:36:32 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								func  main ( )  {  
						 
					
						
							
								
									
										
										
										
											2017-09-04 19:10:32 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									if  os . Getenv ( "DEBUG" )  !=  ""  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										runtime . SetBlockProfileRate ( 20 ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										runtime . SetMutexProfileFraction ( 20 ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-01-18 21:48:36 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									var  ( 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										oldFlagRetentionDuration  model . Duration 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										newFlagRetentionDuration  model . Duration 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-06-21 00:48:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									cfg  :=  struct  { 
							 
						 
					
						
							
								
									
										
										
										
											2017-10-23 15:50:13 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										configFile  string 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-21 00:48:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-05-23 22:03:54 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										localStoragePath     string 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										notifier             notifier . Options 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										notifierTimeout      model . Duration 
							 
						 
					
						
							
								
									
										
										
										
											2018-08-02 18:18:24 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										forGracePeriod       model . Duration 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										outageTolerance      model . Duration 
							 
						 
					
						
							
								
									
										
										
										
											2018-08-28 00:41:42 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										resendDelay          model . Duration 
							 
						 
					
						
							
								
									
										
										
										
											2018-05-23 22:03:54 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										web                  web . Options 
							 
						 
					
						
							
								
									
										
										
										
											2020-02-18 19:25:36 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										tsdb                 tsdbOptions 
							 
						 
					
						
							
								
									
										
										
										
											2018-05-23 22:03:54 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										lookbackDelta        model . Duration 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										webTimeout           model . Duration 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										queryTimeout         model . Duration 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										queryConcurrency     int 
							 
						 
					
						
							
								
									
										
										
										
											2018-10-02 19:59:19 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										queryMaxSamples      int 
							 
						 
					
						
							
								
									
										
										
										
											2018-05-23 22:03:54 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										RemoteFlushDeadline  model . Duration 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-21 00:48:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-01-17 23:01:06 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										prometheusURL    string 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										corsRegexString  string 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-21 00:48:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-11-23 21:22:40 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										promlogConfig  promlog . Config 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-21 00:48:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									} { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										notifier :  notifier . Options { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											Registerer :  prometheus . DefaultRegisterer , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} , 
							 
						 
					
						
							
								
									
										
										
										
											2019-06-24 21:48:15 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										web :  web . Options { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											Registerer :  prometheus . DefaultRegisterer , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											Gatherer :    prometheus . DefaultGatherer , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} , 
							 
						 
					
						
							
								
									
										
										
										
											2018-11-23 21:22:40 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										promlogConfig :  promlog . Config { } , 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-21 00:48:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-06-20 23:38:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									a  :=  kingpin . New ( filepath . Base ( os . Args [ 0 ] ) ,  "The Prometheus monitoring server" ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									a . Version ( version . Print ( "prometheus" ) ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									a . HelpFlag . Short ( 'h' ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									a . Flag ( "config.file" ,  "Prometheus configuration file path." ) . 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										Default ( "prometheus.yml" ) . StringVar ( & cfg . configFile ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-10-17 06:00:05 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									a . Flag ( "web.listen-address" ,  "Address to listen on for UI, API, and telemetry." ) . 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-20 23:38:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										Default ( "0.0.0.0:9090" ) . StringVar ( & cfg . web . ListenAddress ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									a . Flag ( "web.read-timeout" , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										"Maximum duration before timing out read of the request, and closing idle connections." ) . 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										Default ( "5m" ) . SetValue ( & cfg . webTimeout ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									a . Flag ( "web.max-connections" ,  "Maximum number of simultaneous connections." ) . 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										Default ( "512" ) . IntVar ( & cfg . web . MaxConnections ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									a . Flag ( "web.external-url" , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										"The URL under which Prometheus is externally reachable (for example, if Prometheus is served via a reverse proxy). Used for generating relative and absolute links back to Prometheus itself. If the URL has a path portion, it will be used to prefix all HTTP endpoints served by Prometheus. If omitted, relevant URL components will be derived automatically." ) . 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										PlaceHolder ( "<URL>" ) . StringVar ( & cfg . prometheusURL ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									a . Flag ( "web.route-prefix" , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										"Prefix for the internal routes of web endpoints. Defaults to path of --web.external-url." ) . 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										PlaceHolder ( "<path>" ) . StringVar ( & cfg . web . RoutePrefix ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									a . Flag ( "web.user-assets" ,  "Path to static asset directory, available at /user." ) . 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										PlaceHolder ( "<path>" ) . StringVar ( & cfg . web . UserAssetsPath ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-10 21:44:29 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									a . Flag ( "web.enable-lifecycle" ,  "Enable shutdown and reload via HTTP request." ) . 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										Default ( "false" ) . BoolVar ( & cfg . web . EnableLifecycle ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-20 23:38:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-20 23:58:19 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									a . Flag ( "web.enable-admin-api" ,  "Enable API endpoints for admin control actions." ) . 
							 
						 
					
						
							
								
									
										
										
										
											2017-07-10 15:29:41 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										Default ( "false" ) . BoolVar ( & cfg . web . EnableAdminAPI ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-06-20 23:38:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									a . Flag ( "web.console.templates" ,  "Path to the console template directory, available at /consoles." ) . 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										Default ( "consoles" ) . StringVar ( & cfg . web . ConsoleTemplatesPath ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									a . Flag ( "web.console.libraries" ,  "Path to the console library directory." ) . 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										Default ( "console_libraries" ) . StringVar ( & cfg . web . ConsoleLibrariesPath ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-11-21 12:45:06 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									a . Flag ( "web.page-title" ,  "Document title of Prometheus instance." ) . 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										Default ( "Prometheus Time Series Collection and Processing Server" ) . StringVar ( & cfg . web . PageTitle ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-05-15 22:59:06 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									a . Flag ( "web.cors.origin" ,  ` Regex for CORS origin. It is fully anchored. Example: 'https?://(domain1|domain2)\.com' ` ) . 
							 
						 
					
						
							
								
									
										
										
										
											2019-04-10 20:22:05 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										Default ( ".*" ) . StringVar ( & cfg . corsRegexString ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-06-20 23:38:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									a . Flag ( "storage.tsdb.path" ,  "Base path for metrics storage." ) . 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										Default ( "data/" ) . StringVar ( & cfg . localStoragePath ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-12-24 20:13:48 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									a . Flag ( "storage.tsdb.min-block-duration" ,  "Minimum duration of a data block before being persisted. For use in testing." ) . 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										Hidden ( ) . Default ( "2h" ) . SetValue ( & cfg . tsdb . MinBlockDuration ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-20 23:38:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									a . Flag ( "storage.tsdb.max-block-duration" , 
							 
						 
					
						
							
								
									
										
										
										
											2019-05-15 22:59:06 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										"Maximum duration compacted blocks may span. For use in testing. (Defaults to 10% of the retention period.)" ) . 
							 
						 
					
						
							
								
									
										
										
										
											2017-12-24 20:13:48 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										Hidden ( ) . PlaceHolder ( "<duration>" ) . SetValue ( & cfg . tsdb . MaxBlockDuration ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-20 23:38:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-01-03 22:13:21 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									a . Flag ( "storage.tsdb.wal-segment-size" , 
							 
						 
					
						
							
								
									
										
										
										
											2019-05-15 22:59:06 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										"Size at which to split the tsdb WAL segment files. Example: 100MB" ) . 
							 
						 
					
						
							
								
									
										
										
										
											2019-01-03 22:13:21 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										Hidden ( ) . PlaceHolder ( "<bytes>" ) . BytesVar ( & cfg . tsdb . WALSegmentSize ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-05-15 22:59:06 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									a . Flag ( "storage.tsdb.retention" ,  "[DEPRECATED] How long to retain samples in storage. This flag has been deprecated, use \"storage.tsdb.retention.time\" instead." ) . 
							 
						 
					
						
							
								
									
										
										
										
											2019-02-19 19:53:43 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										SetValue ( & oldFlagRetentionDuration ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-01-18 21:48:36 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-11-30 16:00:51 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									a . Flag ( "storage.tsdb.retention.time" ,  "How long to retain samples in storage. When this flag is set it overrides \"storage.tsdb.retention\". If neither this flag nor \"storage.tsdb.retention\" nor \"storage.tsdb.retention.size\" is set, the retention time defaults to " + defaultRetentionString + ". Units Supported: y, w, d, h, m, s, ms." ) . 
							 
						 
					
						
							
								
									
										
										
										
											2019-02-19 19:53:43 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										SetValue ( & newFlagRetentionDuration ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-01-18 21:48:36 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-05-11 19:15:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									a . Flag ( "storage.tsdb.retention.size" ,  "[EXPERIMENTAL] Maximum number of bytes that can be stored for blocks. A unit is required, supported units: B, KB, MB, GB, TB, PB, EB. Ex: \"512MB\". This flag is experimental and can be changed in future releases." ) . 
							 
						 
					
						
							
								
									
										
										
										
											2019-02-19 19:53:43 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										BytesVar ( & cfg . tsdb . MaxBytes ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-20 23:38:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-06-22 21:02:10 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									a . Flag ( "storage.tsdb.no-lockfile" ,  "Do not create lockfile in data directory." ) . 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										Default ( "false" ) . BoolVar ( & cfg . tsdb . NoLockfile ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-05-15 22:59:06 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									a . Flag ( "storage.tsdb.allow-overlapping-blocks" ,  "[EXPERIMENTAL] Allow overlapping blocks, which in turn enables vertical compaction and vertical query merge." ) . 
							 
						 
					
						
							
								
									
										
										
										
											2019-03-05 03:42:45 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										Default ( "false" ) . BoolVar ( & cfg . tsdb . AllowOverlappingBlocks ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-07-03 21:23:13 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									a . Flag ( "storage.tsdb.wal-compression" ,  "Compress the tsdb WAL." ) . 
							 
						 
					
						
							
								
									
										
										
										
											2020-06-19 00:59:40 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										Default ( "true" ) . BoolVar ( & cfg . tsdb . WALCompression ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-07-03 21:23:13 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-05-23 22:03:54 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									a . Flag ( "storage.remote.flush-deadline" ,  "How long to wait flushing sample on shutdown or config reload." ) . 
							 
						 
					
						
							
								
									
										
										
										
											2018-05-24 22:40:24 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										Default ( "1m" ) . PlaceHolder ( "<duration>" ) . SetValue ( & cfg . RemoteFlushDeadline ) 
							 
						 
					
						
							
								
									
										
										
										
											2018-05-23 22:03:54 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-08-20 04:16:10 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									a . Flag ( "storage.remote.read-sample-limit" ,  "Maximum overall number of samples to return via the remote read interface, in a single query. 0 means no limit. This limit is ignored for streamed response types." ) . 
							 
						 
					
						
							
								
									
										
										
										
											2018-09-26 03:07:34 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										Default ( "5e7" ) . IntVar ( & cfg . web . RemoteReadSampleLimit ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									a . Flag ( "storage.remote.read-concurrent-limit" ,  "Maximum number of concurrent remote read calls. 0 means no limit." ) . 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										Default ( "10" ) . IntVar ( & cfg . web . RemoteReadConcurrencyLimit ) 
							 
						 
					
						
							
								
									
										
										
										
											2018-09-05 21:50:50 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-08-20 04:16:10 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									a . Flag ( "storage.remote.read-max-bytes-in-frame" ,  "Maximum number of bytes in a single frame for streaming remote read response types before marshalling. Note that client might have limit on frame size as well. 1MB as recommended by protobuf by default." ) . 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										Default ( "1048576" ) . IntVar ( & cfg . web . RemoteReadBytesInFrame ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-05-15 22:59:06 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									a . Flag ( "rules.alert.for-outage-tolerance" ,  "Max time to tolerate prometheus outage for restoring \"for\" state of alert." ) . 
							 
						 
					
						
							
								
									
										
										
										
											2018-08-02 18:18:24 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										Default ( "1h" ) . SetValue ( & cfg . outageTolerance ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-05-15 22:59:06 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									a . Flag ( "rules.alert.for-grace-period" ,  "Minimum duration between alert and restored \"for\" state. This is maintained only for alerts with configured \"for\" time greater than grace period." ) . 
							 
						 
					
						
							
								
									
										
										
										
											2018-08-02 18:18:24 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										Default ( "10m" ) . SetValue ( & cfg . forGracePeriod ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-08-28 23:05:00 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									a . Flag ( "rules.alert.resend-delay" ,  "Minimum amount of time to wait before resending an alert to Alertmanager." ) . 
							 
						 
					
						
							
								
									
										
										
										
											2018-08-28 00:41:42 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										Default ( "1m" ) . SetValue ( & cfg . resendDelay ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-03-21 00:54:36 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									a . Flag ( "alertmanager.notification-queue-capacity" ,  "The capacity of the queue for pending Alertmanager notifications." ) . 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-20 23:38:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										Default ( "10000" ) . IntVar ( & cfg . notifier . QueueCapacity ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-07-21 05:48:35 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									a . Flag ( "alertmanager.timeout" ,  "Timeout for sending alerts to Alertmanager." ) . 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-21 00:48:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										Default ( "10s" ) . SetValue ( & cfg . notifierTimeout ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-20 23:38:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-02-10 07:58:23 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									a . Flag ( "query.lookback-delta" ,  "The maximum lookback duration for retrieving metrics during expression evaluations and federation." ) . 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-20 23:38:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										Default ( "5m" ) . SetValue ( & cfg . lookbackDelta ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									a . Flag ( "query.timeout" ,  "Maximum time a query may take before being aborted." ) . 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-21 00:48:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										Default ( "2m" ) . SetValue ( & cfg . queryTimeout ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-20 23:38:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									a . Flag ( "query.max-concurrency" ,  "Maximum number of queries executed concurrently." ) . 
							 
						 
					
						
							
								
									
										
										
										
											2018-01-10 00:44:23 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										Default ( "20" ) . IntVar ( & cfg . queryConcurrency ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-05-15 22:59:06 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									a . Flag ( "query.max-samples" ,  "Maximum number of samples a single query can load into memory. Note that queries will fail if they try to load more samples than this into memory, so this also limits the number of samples a query can return." ) . 
							 
						 
					
						
							
								
									
										
										
										
											2018-10-02 19:59:19 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										Default ( "50000000" ) . IntVar ( & cfg . queryMaxSamples ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-20 23:38:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-11-23 21:22:40 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									promlogflag . AddFlags ( a ,  & cfg . promlogConfig ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-09-09 00:34:20 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-06-21 00:48:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									_ ,  err  :=  a . Parse ( os . Args [ 1 : ] ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									if  err  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2017-10-09 22:25:50 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										fmt . Fprintln ( os . Stderr ,  errors . Wrapf ( err ,  "Error parsing commandline arguments" ) ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-20 23:38:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										a . Usage ( os . Args [ 1 : ] ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										os . Exit ( 2 ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-21 00:48:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-02-19 19:53:43 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									logger  :=  promlog . New ( & cfg . promlogConfig ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-06-21 00:48:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									cfg . web . ExternalURL ,  err  =  computeExternalURL ( cfg . prometheusURL ,  cfg . web . ListenAddress ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									if  err  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										fmt . Fprintln ( os . Stderr ,  errors . Wrapf ( err ,  "parse external URL %q" ,  cfg . prometheusURL ) ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-20 23:38:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										os . Exit ( 2 ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-01-17 23:01:06 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									cfg . web . CORSOrigin ,  err  =  compileCORSRegexString ( cfg . corsRegexString ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									if  err  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										fmt . Fprintln ( os . Stderr ,  errors . Wrapf ( err ,  "could not compile CORS regex string %q" ,  cfg . corsRegexString ) ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										os . Exit ( 2 ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-06-21 23:56:59 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// Throw error for invalid config before starting other components.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									if  _ ,  err  :=  config . LoadFile ( cfg . configFile ) ;  err  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										level . Error ( logger ) . Log ( "msg" ,  fmt . Sprintf ( "Error loading config (--config.file=%s)" ,  cfg . configFile ) ,  "err" ,  err ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										os . Exit ( 2 ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-06-21 00:48:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									cfg . web . ReadTimeout  =  time . Duration ( cfg . webTimeout ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// Default -web.route-prefix to path of -web.external-url.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									if  cfg . web . RoutePrefix  ==  ""  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										cfg . web . RoutePrefix  =  cfg . web . ExternalURL . Path 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// RoutePrefix must always be at least '/'.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									cfg . web . RoutePrefix  =  "/"  +  strings . Trim ( cfg . web . RoutePrefix ,  "/" ) 
							 
						 
					
						
							
								
									
										
										
										
											2015-06-15 18:36:32 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-02-19 19:53:43 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									{  // Time retention settings.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										if  oldFlagRetentionDuration  !=  0  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											level . Warn ( logger ) . Log ( "deprecation_notice" ,  "'storage.tsdb.retention' flag is deprecated use 'storage.tsdb.retention.time' instead." ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											cfg . tsdb . RetentionDuration  =  oldFlagRetentionDuration 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
									
										
										
										
											2019-01-18 21:48:36 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-02-19 19:53:43 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										// When the new flag is set it takes precedence.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										if  newFlagRetentionDuration  !=  0  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											cfg . tsdb . RetentionDuration  =  newFlagRetentionDuration 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
									
										
										
										
											2019-01-18 22:48:06 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-02-19 19:53:43 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										if  cfg . tsdb . RetentionDuration  ==  0  &&  cfg . tsdb . MaxBytes  ==  0  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											cfg . tsdb . RetentionDuration  =  defaultRetentionDuration 
							 
						 
					
						
							
								
									
										
										
										
											2020-04-11 16:22:18 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											level . Info ( logger ) . Log ( "msg" ,  "No time or size retention was set so using the default time retention" ,  "duration" ,  defaultRetentionDuration ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-02-19 19:53:43 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
									
										
										
										
											2019-01-18 22:48:06 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-03-12 01:18:57 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										// Check for overflows. This limits our max retention to 100y.
 
							 
						 
					
						
							
								
									
										
										
										
											2019-02-19 19:53:43 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										if  cfg . tsdb . RetentionDuration  <  0  { 
							 
						 
					
						
							
								
									
										
										
										
											2019-03-12 01:18:57 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											y ,  err  :=  model . ParseDuration ( "100y" ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											if  err  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												panic ( err ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											cfg . tsdb . RetentionDuration  =  y 
							 
						 
					
						
							
								
									
										
										
										
											2020-04-11 16:22:18 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											level . Warn ( logger ) . Log ( "msg" ,  "Time retention value is too high. Limiting to: " + y . String ( ) ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-01-18 22:48:06 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
									
										
										
										
											2019-02-19 19:53:43 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									{  // Max block size  settings.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										if  cfg . tsdb . MaxBlockDuration  ==  0  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											maxBlockDuration ,  err  :=  model . ParseDuration ( "31d" ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											if  err  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												panic ( err ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											// When the time retention is set and not too big use to define the max block duration.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											if  cfg . tsdb . RetentionDuration  !=  0  &&  cfg . tsdb . RetentionDuration / 10  <  maxBlockDuration  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												maxBlockDuration  =  cfg . tsdb . RetentionDuration  /  10 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											} 
							 
						 
					
						
							
								
									
										
										
										
											2019-01-18 22:48:06 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-02-19 19:53:43 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											cfg . tsdb . MaxBlockDuration  =  maxBlockDuration 
							 
						 
					
						
							
								
									
										
										
										
											2019-01-18 22:48:06 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-21 00:48:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2015-09-02 01:18:39 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-07-22 21:39:51 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									noStepSubqueryInterval  :=  & safePromQLNoStepSubqueryInterval { } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									noStepSubqueryInterval . Set ( config . DefaultGlobalConfig . EvaluationInterval ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-21 00:48:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-12-04 22:01:12 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// Above level 6, the k8s client would log bearer tokens in clear-text.
 
							 
						 
					
						
							
								
									
										
										
										
											2019-01-04 05:44:29 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									klog . ClampLevel ( 6 ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									klog . SetLogger ( log . With ( logger ,  "component" ,  "k8s_client_runtime" ) ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-16 18:22:44 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-10-23 15:49:28 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									level . Info ( logger ) . Log ( "msg" ,  "Starting Prometheus" ,  "version" ,  version . Info ( ) ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-08-12 02:45:52 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									level . Info ( logger ) . Log ( "build_context" ,  version . BuildContext ( ) ) 
							 
						 
					
						
							
								
									
										
										
										
											2018-08-22 18:41:11 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									level . Info ( logger ) . Log ( "host_details" ,  prom_runtime . Uname ( ) ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									level . Info ( logger ) . Log ( "fd_limits" ,  prom_runtime . FdLimits ( ) ) 
							 
						 
					
						
							
								
									
										
										
										
											2020-03-23 22:47:11 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									level . Info ( logger ) . Log ( "vm_limits" ,  prom_runtime . VMLimits ( ) ) 
							 
						 
					
						
							
								
									
										
										
										
											2016-05-05 19:46:51 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-09-18 18:32:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									var  ( 
							 
						 
					
						
							
								
									
										
										
										
											2020-02-17 19:41:04 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										localStorage   =  & readyStorage { } 
							 
						 
					
						
							
								
									
										
										
										
											2018-09-08 05:26:04 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										remoteStorage  =  remote . NewStorage ( log . With ( logger ,  "component" ,  "remote" ) ,  prometheus . DefaultRegisterer ,  localStorage . StartTime ,  cfg . localStoragePath ,  time . Duration ( cfg . RemoteFlushDeadline ) ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-09-18 18:32:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										fanoutStorage  =  storage . NewFanout ( logger ,  localStorage ,  remoteStorage ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-08-12 02:45:52 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									) 
							 
						 
					
						
							
								
									
										
										
										
											2016-09-20 04:47:51 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-06-26 07:32:44 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									var  ( 
							 
						 
					
						
							
								
									
										
										
										
											2017-12-01 20:59:24 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										ctxWeb ,  cancelWeb  =  context . WithCancel ( context . Background ( ) ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										ctxRule            =  context . Background ( ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-26 23:15:15 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-12-18 19:13:18 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										notifierManager  =  notifier . NewManager ( & cfg . notifier ,  log . With ( logger ,  "component" ,  "notifier" ) ) 
							 
						 
					
						
							
								
									
										
										
										
											2018-01-26 07:32:36 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										ctxScrape ,  cancelScrape  =  context . WithCancel ( context . Background ( ) ) 
							 
						 
					
						
							
								
									
										
										
										
											2018-10-18 23:46:59 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										discoveryManagerScrape   =  discovery . NewManager ( ctxScrape ,  log . With ( logger ,  "component" ,  "discovery manager scrape" ) ,  discovery . Name ( "scrape" ) ) 
							 
						 
					
						
							
								
									
										
										
										
											2018-01-26 07:32:36 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										ctxNotify ,  cancelNotify  =  context . WithCancel ( context . Background ( ) ) 
							 
						 
					
						
							
								
									
										
										
										
											2018-10-18 23:46:59 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										discoveryManagerNotify   =  discovery . NewManager ( ctxNotify ,  log . With ( logger ,  "component" ,  "discovery manager notify" ) ,  discovery . Name ( "notify" ) ) 
							 
						 
					
						
							
								
									
										
										
										
											2018-01-26 07:32:36 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-02-01 17:55:07 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										scrapeManager  =  scrape . NewManager ( log . With ( logger ,  "component" ,  "scrape manager" ) ,  fanoutStorage ) 
							 
						 
					
						
							
								
									
										
										
										
											2018-01-10 00:44:23 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-10-02 19:59:19 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										opts  =  promql . EngineOpts { 
							 
						 
					
						
							
								
									
										
										
										
											2020-07-22 21:39:51 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											Logger :                    log . With ( logger ,  "component" ,  "query engine" ) , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											Reg :                       prometheus . DefaultRegisterer , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											MaxSamples :                cfg . queryMaxSamples , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											Timeout :                   time . Duration ( cfg . queryTimeout ) , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											ActiveQueryTracker :        promql . NewActiveQueryTracker ( cfg . localStoragePath ,  cfg . queryConcurrency ,  log . With ( logger ,  "component" ,  "activeQueryTracker" ) ) , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											LookbackDelta :             time . Duration ( cfg . lookbackDelta ) , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											NoStepSubqueryIntervalFn :  noStepSubqueryInterval . Get , 
							 
						 
					
						
							
								
									
										
										
										
											2018-10-02 19:59:19 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
									
										
										
										
											2019-07-31 23:12:43 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-10-02 19:59:19 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										queryEngine  =  promql . NewEngine ( opts ) 
							 
						 
					
						
							
								
									
										
										
										
											2018-01-10 00:44:23 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										ruleManager  =  rules . NewManager ( & rules . ManagerOptions { 
							 
						 
					
						
							
								
									
										
										
										
											2018-08-02 18:18:24 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											Appendable :       fanoutStorage , 
							 
						 
					
						
							
								
									
										
										
										
											2020-06-27 02:06:36 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											Queryable :        localStorage , 
							 
						 
					
						
							
								
									
										
										
										
											2018-08-02 18:18:24 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											QueryFunc :        rules . EngineQueryFunc ( queryEngine ,  fanoutStorage ) , 
							 
						 
					
						
							
								
									
										
										
										
											2018-12-18 19:13:18 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											NotifyFunc :       sendAlerts ( notifierManager ,  cfg . web . ExternalURL . String ( ) ) , 
							 
						 
					
						
							
								
									
										
										
										
											2018-08-02 18:18:24 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											Context :          ctxRule , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											ExternalURL :      cfg . web . ExternalURL , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											Registerer :       prometheus . DefaultRegisterer , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											Logger :           log . With ( logger ,  "component" ,  "rule manager" ) , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											OutageTolerance :  time . Duration ( cfg . outageTolerance ) , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											ForGracePeriod :   time . Duration ( cfg . forGracePeriod ) , 
							 
						 
					
						
							
								
									
										
										
										
											2018-08-28 00:41:42 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											ResendDelay :      time . Duration ( cfg . resendDelay ) , 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-26 23:15:15 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										} ) 
							 
						 
					
						
							
								
									
										
										
										
											2015-06-26 07:32:44 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-11-26 23:15:15 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									cfg . web . Context  =  ctxWeb 
							 
						 
					
						
							
								
									
										
										
										
											2020-02-18 19:25:36 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									cfg . web . TSDBRetentionDuration  =  cfg . tsdb . RetentionDuration 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									cfg . web . TSDBMaxBytes  =  cfg . tsdb . MaxBytes 
							 
						 
					
						
							
								
									
										
										
										
											2020-05-07 17:03:48 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									cfg . web . TSDBDir  =  cfg . localStoragePath 
							 
						 
					
						
							
								
									
										
										
										
											2020-04-30 00:16:14 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									cfg . web . LocalStorage  =  localStorage 
							 
						 
					
						
							
								
									
										
										
										
											2017-09-18 18:32:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									cfg . web . Storage  =  fanoutStorage 
							 
						 
					
						
							
								
									
										
										
										
											2016-09-16 06:58:06 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									cfg . web . QueryEngine  =  queryEngine 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-25 21:13:54 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									cfg . web . ScrapeManager  =  scrapeManager 
							 
						 
					
						
							
								
									
										
										
										
											2016-09-16 06:58:06 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									cfg . web . RuleManager  =  ruleManager 
							 
						 
					
						
							
								
									
										
										
										
											2018-12-18 19:13:18 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									cfg . web . Notifier  =  notifierManager 
							 
						 
					
						
							
								
									
										
										
										
											2020-02-10 07:58:23 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									cfg . web . LookbackDelta  =  time . Duration ( cfg . lookbackDelta ) 
							 
						 
					
						
							
								
									
										
										
										
											2015-06-15 18:36:32 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-09-16 06:58:06 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									cfg . web . Version  =  & web . PrometheusVersion { 
							 
						 
					
						
							
								
									
										
										
										
											2016-05-05 19:46:51 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										Version :    version . Version , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										Revision :   version . Revision , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										Branch :     version . Branch , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										BuildUser :  version . BuildUser , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										BuildDate :  version . BuildDate , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										GoVersion :  version . GoVersion , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-09-16 06:58:06 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									cfg . web . Flags  =  map [ string ] string { } 
							 
						 
					
						
							
								
									
										
											 
										
											
												api: Added v1/status/flags endpoint. (#3864)
Endpoint URL: /api/v1/status/flags
Example Output:
```json
{
  "status": "success",
  "data": {
    "alertmanager.notification-queue-capacity": "10000",
    "alertmanager.timeout": "10s",
    "completion-bash": "false",
    "completion-script-bash": "false",
    "completion-script-zsh": "false",
    "config.file": "my_cool_prometheus.yaml",
    "help": "false",
    "help-long": "false",
    "help-man": "false",
    "log.level": "info",
    "query.lookback-delta": "5m",
    "query.max-concurrency": "20",
    "query.timeout": "2m",
    "storage.tsdb.max-block-duration": "36h",
    "storage.tsdb.min-block-duration": "2h",
    "storage.tsdb.no-lockfile": "false",
    "storage.tsdb.path": "data/",
    "storage.tsdb.retention": "15d",
    "version": "false",
    "web.console.libraries": "console_libraries",
    "web.console.templates": "consoles",
    "web.enable-admin-api": "false",
    "web.enable-lifecycle": "false",
    "web.external-url": "",
    "web.listen-address": "0.0.0.0:9090",
    "web.max-connections": "512",
    "web.read-timeout": "5m",
    "web.route-prefix": "/",
    "web.user-assets": ""
  }
}
```
Signed-off-by: Bartek Plotka <bwplotka@gmail.com>
											 
										 
										
											2018-02-21 16:49:02 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// Exclude kingpin default flags to expose only Prometheus ones.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									boilerplateFlags  :=  kingpin . New ( "" ,  "" ) . Version ( "" ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-20 23:38:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									for  _ ,  f  :=  range  a . Model ( ) . Flags  { 
							 
						 
					
						
							
								
									
										
											 
										
											
												api: Added v1/status/flags endpoint. (#3864)
Endpoint URL: /api/v1/status/flags
Example Output:
```json
{
  "status": "success",
  "data": {
    "alertmanager.notification-queue-capacity": "10000",
    "alertmanager.timeout": "10s",
    "completion-bash": "false",
    "completion-script-bash": "false",
    "completion-script-zsh": "false",
    "config.file": "my_cool_prometheus.yaml",
    "help": "false",
    "help-long": "false",
    "help-man": "false",
    "log.level": "info",
    "query.lookback-delta": "5m",
    "query.max-concurrency": "20",
    "query.timeout": "2m",
    "storage.tsdb.max-block-duration": "36h",
    "storage.tsdb.min-block-duration": "2h",
    "storage.tsdb.no-lockfile": "false",
    "storage.tsdb.path": "data/",
    "storage.tsdb.retention": "15d",
    "version": "false",
    "web.console.libraries": "console_libraries",
    "web.console.templates": "consoles",
    "web.enable-admin-api": "false",
    "web.enable-lifecycle": "false",
    "web.external-url": "",
    "web.listen-address": "0.0.0.0:9090",
    "web.max-connections": "512",
    "web.read-timeout": "5m",
    "web.route-prefix": "/",
    "web.user-assets": ""
  }
}
```
Signed-off-by: Bartek Plotka <bwplotka@gmail.com>
											 
										 
										
											2018-02-21 16:49:02 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										if  boilerplateFlags . GetFlag ( f . Name )  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											continue 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-09-16 06:58:06 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										cfg . web . Flags [ f . Name ]  =  f . Value . String ( ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-20 23:38:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2016-09-16 06:58:06 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-07-31 23:12:43 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// Depends on cfg.web.ScrapeManager so needs to be after cfg.web.ScrapeManager = scrapeManager.
 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-26 23:15:15 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									webHandler  :=  web . New ( log . With ( logger ,  "component" ,  "web" ) ,  & cfg . web ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-10-06 18:22:19 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// Monitor outgoing connections on default transport with conntrack.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									http . DefaultTransport . ( * http . Transport ) . DialContext  =  conntrack . NewDialContextFunc ( 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										conntrack . DialWithTracing ( ) , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-08-07 03:48:52 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									reloaders  :=  [ ] reloader { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										{ 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											name :      "remote_storage" , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											reloader :  remoteStorage . ApplyConfig , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} ,  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											name :      "web_handler" , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											reloader :  webHandler . ApplyConfig , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} ,  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											name :  "query_engine" , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											reloader :  func ( cfg  * config . Config )  error  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												if  cfg . GlobalConfig . QueryLogFile  ==  ""  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
													queryEngine . SetQueryLogger ( nil ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
													return  nil 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												} 
							 
						 
					
						
							
								
									
										
										
										
											2020-01-08 21:28:43 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-08-07 03:48:52 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												l ,  err  :=  logging . NewJSONFileLogger ( cfg . GlobalConfig . QueryLogFile ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-24 15:22:57 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												if  err  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2020-08-07 03:48:52 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
													return  err 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-24 15:22:57 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												} 
							 
						 
					
						
							
								
									
										
										
										
											2020-08-07 03:48:52 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												queryEngine . SetQueryLogger ( l ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												return  nil 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											} , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} ,  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											// The Scrape and notifier managers need to reload before the Discovery manager as
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											// they need to read the most updated config when receiving the new targets list.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											name :      "scrape" , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											reloader :  scrapeManager . ApplyConfig , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} ,  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											name :  "scrape_sd" , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											reloader :  func ( cfg  * config . Config )  error  { 
							 
						 
					
						
							
								
									
										
										
										
											2020-08-20 20:48:26 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												c  :=  make ( map [ string ] discovery . Configs ) 
							 
						 
					
						
							
								
									
										
										
										
											2020-08-07 03:48:52 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												for  _ ,  v  :=  range  cfg . ScrapeConfigs  { 
							 
						 
					
						
							
								
									
										
										
										
											2020-08-20 20:48:26 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
													c [ v . JobName ]  =  v . ServiceDiscoveryConfigs 
							 
						 
					
						
							
								
									
										
										
										
											2020-08-07 03:48:52 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												return  discoveryManagerScrape . ApplyConfig ( c ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											} , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} ,  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											name :      "notify" , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											reloader :  notifierManager . ApplyConfig , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} ,  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											name :  "notify_sd" , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											reloader :  func ( cfg  * config . Config )  error  { 
							 
						 
					
						
							
								
									
										
										
										
											2020-08-20 20:48:26 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												c  :=  make ( map [ string ] discovery . Configs ) 
							 
						 
					
						
							
								
									
										
										
										
											2020-08-07 03:48:52 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												for  k ,  v  :=  range  cfg . AlertingConfig . AlertmanagerConfigs . ToMap ( )  { 
							 
						 
					
						
							
								
									
										
										
										
											2020-08-20 20:48:26 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
													c [ k ]  =  v . ServiceDiscoveryConfigs 
							 
						 
					
						
							
								
									
										
										
										
											2020-08-07 03:48:52 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												return  discoveryManagerNotify . ApplyConfig ( c ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											} , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} ,  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											name :  "rules" , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											reloader :  func ( cfg  * config . Config )  error  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												// Get all rule files matching the configuration paths.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												var  files  [ ] string 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												for  _ ,  pat  :=  range  cfg . RuleFiles  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
													fs ,  err  :=  filepath . Glob ( pat ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
													if  err  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
														// The only error can be a bad pattern.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
														return  errors . Wrapf ( err ,  "error retrieving rule files for %s" ,  pat ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
													} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
													files  =  append ( files ,  fs ... ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												return  ruleManager . Update ( 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
													time . Duration ( cfg . GlobalConfig . EvaluationInterval ) , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
													files , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
													cfg . GlobalConfig . ExternalLabels , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											} , 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-23 22:48:14 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										} , 
							 
						 
					
						
							
								
									
										
										
										
											2015-06-15 18:36:32 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-11-11 20:06:13 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									prometheus . MustRegister ( configSuccess ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									prometheus . MustRegister ( configSuccessTime ) 
							 
						 
					
						
							
								
									
										
										
										
											2015-06-15 18:36:32 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-09-18 18:32:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// Start all components while we wait for TSDB to open but only load
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// initial config and mark ourselves as ready after it completed.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									dbOpen  :=  make ( chan  struct { } ) 
							 
						 
					
						
							
								
									
										
										
										
											2018-01-18 02:14:24 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// sync.Once is used to make sure we can close the channel at different execution stages(SIGTERM or when the config is loaded).
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									type  closeOnce  struct  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										C      chan  struct { } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										once   sync . Once 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										Close  func ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// Wait until the server is ready to handle reloading.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									reloadReady  :=  & closeOnce { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										C :  make ( chan  struct { } ) , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									reloadReady . Close  =  func ( )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										reloadReady . once . Do ( func ( )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											close ( reloadReady . C ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-11 20:06:13 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-04-23 08:05:55 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									closer ,  err  :=  initTracing ( logger ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									if  err  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										level . Error ( logger ) . Log ( "msg" ,  "Unable to init tracing" ,  "err" ,  err ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										os . Exit ( 2 ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									defer  closer . Close ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-04-19 20:55:28 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									var  g  run . Group 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-11 20:06:13 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									{ 
							 
						 
					
						
							
								
									
										
										
										
											2018-04-02 02:19:30 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										// Termination handler.
 
							 
						 
					
						
							
								
									
										
										
										
											2018-11-14 16:56:42 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										term  :=  make ( chan  os . Signal ,  1 ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-11 20:06:13 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										signal . Notify ( term ,  os . Interrupt ,  syscall . SIGTERM ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										cancel  :=  make ( chan  struct { } ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										g . Add ( 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											func ( )  error  { 
							 
						 
					
						
							
								
									
										
										
										
											2018-01-18 02:14:24 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												// Don't forget to release the reloadReady channel so that waiting blocks can exit normally.
 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-11 20:06:13 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												select  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												case  <- term : 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
													level . Warn ( logger ) . Log ( "msg" ,  "Received SIGTERM, exiting gracefully..." ) 
							 
						 
					
						
							
								
									
										
										
										
											2018-01-18 02:14:24 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
													reloadReady . Close ( ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-11 20:06:13 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												case  <- webHandler . Quit ( ) : 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
													level . Warn ( logger ) . Log ( "msg" ,  "Received termination request via web service, exiting gracefully..." ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												case  <- cancel : 
							 
						 
					
						
							
								
									
										
										
										
											2018-01-18 02:14:24 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
													reloadReady . Close ( ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-11 20:06:13 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												return  nil 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											} , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											func ( err  error )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												close ( cancel ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											} , 
							 
						 
					
						
							
								
									
										
										
										
											2017-09-18 18:32:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										) 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-11 20:06:13 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-26 23:15:15 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									{ 
							 
						 
					
						
							
								
									
										
										
										
											2018-04-02 02:19:30 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										// Scrape discovery manager.
 
							 
						 
					
						
							
								
									
										
										
										
											2017-12-31 01:27:50 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										g . Add ( 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											func ( )  error  { 
							 
						 
					
						
							
								
									
										
										
										
											2018-01-26 07:32:36 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												err  :=  discoveryManagerScrape . Run ( ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-12-31 01:27:50 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												level . Info ( logger ) . Log ( "msg" ,  "Scrape discovery manager stopped" ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												return  err 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											} , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											func ( err  error )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												level . Info ( logger ) . Log ( "msg" ,  "Stopping scrape discovery manager..." ) 
							 
						 
					
						
							
								
									
										
										
										
											2018-01-26 07:32:36 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												cancelScrape ( ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-12-31 01:27:50 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											} , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									{ 
							 
						 
					
						
							
								
									
										
										
										
											2018-04-02 02:19:30 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										// Notify discovery manager.
 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-26 23:15:15 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										g . Add ( 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											func ( )  error  { 
							 
						 
					
						
							
								
									
										
										
										
											2018-01-26 07:32:36 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												err  :=  discoveryManagerNotify . Run ( ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-12-31 01:27:50 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												level . Info ( logger ) . Log ( "msg" ,  "Notify discovery manager stopped" ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-26 23:15:15 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												return  err 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											} , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											func ( err  error )  { 
							 
						 
					
						
							
								
									
										
										
										
											2017-12-31 01:27:50 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												level . Info ( logger ) . Log ( "msg" ,  "Stopping notify discovery manager..." ) 
							 
						 
					
						
							
								
									
										
										
										
											2018-01-26 07:32:36 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												cancelNotify ( ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-26 23:15:15 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											} , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									{ 
							 
						 
					
						
							
								
									
										
										
										
											2018-04-02 02:19:30 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										// Scrape manager.
 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-26 23:15:15 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										g . Add ( 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											func ( )  error  { 
							 
						 
					
						
							
								
									
										
										
										
											2018-01-17 20:02:13 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												// When the scrape manager receives a new targets list
 
							 
						 
					
						
							
								
									
										
										
										
											2018-01-18 02:14:24 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												// it needs to read a valid config for each job.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												// It depends on the config being in sync with the discovery manager so
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												// we wait until the config is fully loaded.
 
							 
						 
					
						
							
								
									
										
										
										
											2018-02-26 15:58:10 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												<- reloadReady . C 
							 
						 
					
						
							
								
									
										
										
										
											2018-01-17 20:02:13 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-12-31 01:27:50 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												err  :=  scrapeManager . Run ( discoveryManagerScrape . SyncCh ( ) ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-26 23:15:15 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												level . Info ( logger ) . Log ( "msg" ,  "Scrape manager stopped" ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												return  err 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											} , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											func ( err  error )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												// Scrape manager needs to be stopped before closing the local TSDB
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												// so that it doesn't try to write samples to a closed storage.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												level . Info ( logger ) . Log ( "msg" ,  "Stopping scrape manager..." ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												scrapeManager . Stop ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											} , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-11 20:06:13 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									{ 
							 
						 
					
						
							
								
									
										
										
										
											2018-04-02 02:19:30 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										// Reload handler.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-11-11 20:06:13 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										// Make sure that sighup handler is registered with a redirect to the channel before the potentially
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										// long and synchronous tsdb init.
 
							 
						 
					
						
							
								
									
										
										
										
											2018-11-14 16:56:42 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										hup  :=  make ( chan  os . Signal ,  1 ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-11 20:06:13 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										signal . Notify ( hup ,  syscall . SIGHUP ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										cancel  :=  make ( chan  struct { } ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										g . Add ( 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											func ( )  error  { 
							 
						 
					
						
							
								
									
										
										
										
											2018-02-26 15:58:10 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												<- reloadReady . C 
							 
						 
					
						
							
								
									
										
										
										
											2015-06-15 18:36:32 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-11-11 20:06:13 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												for  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
													select  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
													case  <- hup : 
							 
						 
					
						
							
								
									
										
										
										
											2020-07-22 21:39:51 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
														if  err  :=  reloadConfig ( cfg . configFile ,  logger ,  noStepSubqueryInterval ,  reloaders ... ) ;  err  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-11 20:06:13 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
															level . Error ( logger ) . Log ( "msg" ,  "Error reloading config" ,  "err" ,  err ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
														} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
													case  rc  :=  <- webHandler . Reload ( ) : 
							 
						 
					
						
							
								
									
										
										
										
											2020-07-22 21:39:51 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
														if  err  :=  reloadConfig ( cfg . configFile ,  logger ,  noStepSubqueryInterval ,  reloaders ... ) ;  err  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-11 20:06:13 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
															level . Error ( logger ) . Log ( "msg" ,  "Error reloading config" ,  "err" ,  err ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
															rc  <-  err 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
														}  else  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
															rc  <-  nil 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
														} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
													case  <- cancel : 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
														return  nil 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
													} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												} 
							 
						 
					
						
							
								
									
										
										
										
											2017-09-18 18:32:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-11-11 20:06:13 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											} , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											func ( err  error )  { 
							 
						 
					
						
							
								
									
										
										
										
											2018-07-04 20:41:16 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												// Wait for any in-progress reloads to complete to avoid
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												// reloading things after they have been shutdown.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												cancel  <-  struct { } { } 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-11 20:06:13 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											} , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										) 
							 
						 
					
						
							
								
									
										
										
										
											2017-09-18 18:32:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-11 20:06:13 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									{ 
							 
						 
					
						
							
								
									
										
										
										
											2018-04-02 02:19:30 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										// Initial configuration loading.
 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-11 20:06:13 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										cancel  :=  make ( chan  struct { } ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										g . Add ( 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											func ( )  error  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												select  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												case  <- dbOpen : 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												// In case a shutdown is initiated before the dbOpen is released
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												case  <- cancel : 
							 
						 
					
						
							
								
									
										
										
										
											2018-01-18 02:14:24 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
													reloadReady . Close ( ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-11 20:06:13 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
													return  nil 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												} 
							 
						 
					
						
							
								
									
										
										
										
											2017-09-18 18:32:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-07-22 21:39:51 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												if  err  :=  reloadConfig ( cfg . configFile ,  logger ,  noStepSubqueryInterval ,  reloaders ... ) ;  err  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2019-03-26 07:01:12 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
													return  errors . Wrapf ( err ,  "error loading config from %q" ,  cfg . configFile ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-11 20:06:13 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												} 
							 
						 
					
						
							
								
									
										
										
										
											2017-09-18 18:32:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-18 02:14:24 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												reloadReady . Close ( ) 
							 
						 
					
						
							
								
									
										
										
										
											2018-01-17 21:06:56 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-11-11 20:06:13 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												webHandler . Ready ( ) 
							 
						 
					
						
							
								
									
										
										
										
											2018-01-18 02:14:24 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												level . Info ( logger ) . Log ( "msg" ,  "Server is ready to receive web requests." ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-11 20:06:13 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												<- cancel 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												return  nil 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											} , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											func ( err  error )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												close ( cancel ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											} , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2018-07-04 20:41:16 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									{ 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										// Rule manager.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										g . Add ( 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											func ( )  error  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												<- reloadReady . C 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												ruleManager . Run ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												return  nil 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											} , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											func ( err  error )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												ruleManager . Stop ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											} , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-11 20:06:13 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									{ 
							 
						 
					
						
							
								
									
										
										
										
											2018-04-02 02:19:30 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										// TSDB.
 
							 
						 
					
						
							
								
									
										
										
										
											2020-02-12 00:34:09 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										opts  :=  cfg . tsdb . ToTSDBOptions ( ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-11 20:06:13 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										cancel  :=  make ( chan  struct { } ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										g . Add ( 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											func ( )  error  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												level . Info ( logger ) . Log ( "msg" ,  "Starting TSDB ..." ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-01-03 22:13:21 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												if  cfg . tsdb . WALSegmentSize  !=  0  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
													if  cfg . tsdb . WALSegmentSize  <  10 * 1024 * 1024  ||  cfg . tsdb . WALSegmentSize  >  256 * 1024 * 1024  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
														return  errors . New ( "flag 'storage.tsdb.wal-segment-size' must be set between 10MB and 256MB" ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
													} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												} 
							 
						 
					
						
							
								
									
										
										
										
											2020-02-15 02:48:55 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												db ,  err  :=  openDBWithMetrics ( 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-11 20:06:13 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
													cfg . localStoragePath , 
							 
						 
					
						
							
								
									
										
										
										
											2020-02-15 02:48:55 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
													logger , 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-11 20:06:13 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
													prometheus . DefaultRegisterer , 
							 
						 
					
						
							
								
									
										
										
										
											2020-02-12 00:34:09 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
													& opts , 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-11 20:06:13 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												if  err  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2019-03-26 07:01:12 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
													return  errors . Wrapf ( err ,  "opening storage failed" ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-11 20:06:13 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												} 
							 
						 
					
						
							
								
									
										
										
										
											2020-02-15 02:48:55 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-07-30 17:22:44 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												switch  fsType  :=  prom_runtime . Statfs ( cfg . localStoragePath ) ;  fsType  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												case  "NFS_SUPER_MAGIC" : 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
													level . Warn ( logger ) . Log ( "fs_type" ,  fsType ,  "msg" ,  "This filesystem is not supported and may lead to data corruption and data loss. Please carefully read https://prometheus.io/docs/prometheus/latest/storage/ to learn more about supported filesystems." ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												default : 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
													level . Info ( logger ) . Log ( "fs_type" ,  fsType ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-11-11 20:06:13 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												level . Info ( logger ) . Log ( "msg" ,  "TSDB started" ) 
							 
						 
					
						
							
								
									
										
										
										
											2019-02-19 19:53:43 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												level . Debug ( logger ) . Log ( "msg" ,  "TSDB options" , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
													"MinBlockDuration" ,  cfg . tsdb . MinBlockDuration , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
													"MaxBlockDuration" ,  cfg . tsdb . MaxBlockDuration , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
													"MaxBytes" ,  cfg . tsdb . MaxBytes , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
													"NoLockfile" ,  cfg . tsdb . NoLockfile , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
													"RetentionDuration" ,  cfg . tsdb . RetentionDuration , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
													"WALSegmentSize" ,  cfg . tsdb . WALSegmentSize , 
							 
						 
					
						
							
								
									
										
										
										
											2019-03-05 03:42:45 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
													"AllowOverlappingBlocks" ,  cfg . tsdb . AllowOverlappingBlocks , 
							 
						 
					
						
							
								
									
										
										
										
											2019-07-03 21:23:13 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
													"WALCompression" ,  cfg . tsdb . WALCompression , 
							 
						 
					
						
							
								
									
										
										
										
											2019-02-19 19:53:43 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												) 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-11 20:06:13 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												startTimeMargin  :=  int64 ( 2  *  time . Duration ( cfg . tsdb . MinBlockDuration ) . Seconds ( )  *  1000 ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												localStorage . Set ( db ,  startTimeMargin ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												close ( dbOpen ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												<- cancel 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												return  nil 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											} , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											func ( err  error )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												if  err  :=  fanoutStorage . Close ( ) ;  err  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
													level . Error ( logger ) . Log ( "msg" ,  "Error stopping storage" ,  "err" ,  err ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												close ( cancel ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											} , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									{ 
							 
						 
					
						
							
								
									
										
										
										
											2018-04-02 02:19:30 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										// Web handler.
 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-11 20:06:13 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										g . Add ( 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											func ( )  error  { 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-25 21:13:54 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												if  err  :=  webHandler . Run ( ctxWeb ) ;  err  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2019-03-26 07:01:12 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
													return  errors . Wrapf ( err ,  "error starting web server" ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-11 20:06:13 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												return  nil 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											} , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											func ( err  error )  { 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-25 21:13:54 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												cancelWeb ( ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-11 20:06:13 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											} , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									{ 
							 
						 
					
						
							
								
									
										
										
										
											2018-04-02 02:19:30 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										// Notifier.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-11-11 20:06:13 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										// Calling notifier.Stop() before ruleManager.Stop() will cause a panic if the ruleManager isn't running,
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										// so keep this interrupt after the ruleManager.Stop().
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										g . Add ( 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											func ( )  error  { 
							 
						 
					
						
							
								
									
										
										
										
											2018-01-17 21:06:56 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												// When the notifier manager receives a new targets list
 
							 
						 
					
						
							
								
									
										
										
										
											2018-01-18 02:14:24 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												// it needs to read a valid config for each job.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												// It depends on the config being in sync with the discovery manager
 
							 
						 
					
						
							
								
									
										
										
										
											2018-01-17 21:06:56 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												// so we wait until the config is fully loaded.
 
							 
						 
					
						
							
								
									
										
										
										
											2018-02-26 15:58:10 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												<- reloadReady . C 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-12-18 19:13:18 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												notifierManager . Run ( discoveryManagerNotify . SyncCh ( ) ) 
							 
						 
					
						
							
								
									
										
										
										
											2018-01-18 02:14:24 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												level . Info ( logger ) . Log ( "msg" ,  "Notifier manager stopped" ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-11 20:06:13 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												return  nil 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											} , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											func ( err  error )  { 
							 
						 
					
						
							
								
									
										
										
										
											2018-12-18 19:13:18 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												notifierManager . Stop ( ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-11 20:06:13 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											} , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									if  err  :=  g . Run ( ) ;  err  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										level . Error ( logger ) . Log ( "err" ,  err ) 
							 
						 
					
						
							
								
									
										
										
										
											2018-06-21 15:32:26 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										os . Exit ( 1 ) 
							 
						 
					
						
							
								
									
										
										
										
											2015-06-15 18:36:32 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2017-08-12 02:45:52 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									level . Info ( logger ) . Log ( "msg" ,  "See you next time!" ) 
							 
						 
					
						
							
								
									
										
										
										
											2015-06-15 18:36:32 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-02-15 02:48:55 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								func  openDBWithMetrics ( dir  string ,  logger  log . Logger ,  reg  prometheus . Registerer ,  opts  * tsdb . Options )  ( * tsdb . DB ,  error )  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									db ,  err  :=  tsdb . Open ( 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										dir , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										log . With ( logger ,  "component" ,  "tsdb" ) , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										reg , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										opts , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									if  err  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										return  nil ,  err 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									reg . MustRegister ( 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										prometheus . NewGaugeFunc ( prometheus . GaugeOpts { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											Name :  "prometheus_tsdb_lowest_timestamp_seconds" , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											Help :  "Lowest timestamp value stored in the database." , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} ,  func ( )  float64  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											bb  :=  db . Blocks ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											if  len ( bb )  ==  0  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												return  float64 ( db . Head ( ) . MinTime ( )  /  1000 ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											return  float64 ( db . Blocks ( ) [ 0 ] . Meta ( ) . MinTime  /  1000 ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} ) ,  prometheus . NewGaugeFunc ( prometheus . GaugeOpts { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											Name :  "prometheus_tsdb_head_min_time_seconds" , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											Help :  "Minimum time bound of the head block." , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} ,  func ( )  float64  {  return  float64 ( db . Head ( ) . MinTime ( )  /  1000 )  } ) , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										prometheus . NewGaugeFunc ( prometheus . GaugeOpts { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											Name :  "prometheus_tsdb_head_max_time_seconds" , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											Help :  "Maximum timestamp of the head block." , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} ,  func ( )  float64  {  return  float64 ( db . Head ( ) . MaxTime ( )  /  1000 )  } ) , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									return  db ,  nil 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-07-22 21:39:51 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								type  safePromQLNoStepSubqueryInterval  struct  {  
						 
					
						
							
								
									
										
										
										
											2020-07-30 15:45:42 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									value  atomic . Int64 
							 
						 
					
						
							
								
									
										
										
										
											2020-07-22 21:39:51 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								func  durationToInt64Millis ( d  time . Duration )  int64  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									return  int64 ( d  /  time . Millisecond ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								func  ( i  * safePromQLNoStepSubqueryInterval )  Set ( ev  model . Duration )  {  
						 
					
						
							
								
									
										
										
										
											2020-07-30 15:45:42 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									i . value . Store ( durationToInt64Millis ( time . Duration ( ev ) ) ) 
							 
						 
					
						
							
								
									
										
										
										
											2020-07-22 21:39:51 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								func  ( i  * safePromQLNoStepSubqueryInterval )  Get ( int64 )  int64  {  
						 
					
						
							
								
									
										
										
										
											2020-07-30 15:45:42 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									return  i . value . Load ( ) 
							 
						 
					
						
							
								
									
										
										
										
											2020-07-22 21:39:51 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-08-07 03:48:52 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								type  reloader  struct  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									name      string 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									reloader  func ( * config . Config )  error 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								func  reloadConfig ( filename  string ,  logger  log . Logger ,  noStepSuqueryInterval  * safePromQLNoStepSubqueryInterval ,  rls  ... reloader )  ( err  error )  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									start  :=  time . Now ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									timings  :=  [ ] interface { } { } 
							 
						 
					
						
							
								
									
										
										
										
											2017-08-12 02:45:52 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									level . Info ( logger ) . Log ( "msg" ,  "Loading configuration file" ,  "filename" ,  filename ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-09-02 01:18:39 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									defer  func ( )  { 
							 
						 
					
						
							
								
									
										
										
										
											2016-07-11 22:24:54 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										if  err  ==  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2015-09-02 01:18:39 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											configSuccess . Set ( 1 ) 
							 
						 
					
						
							
								
									
										
										
										
											2018-01-27 15:48:13 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											configSuccessTime . SetToCurrentTime ( ) 
							 
						 
					
						
							
								
									
										
										
										
											2015-09-02 01:18:39 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										}  else  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											configSuccess . Set ( 0 ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} ( ) 
							 
						 
					
						
							
								
									
										
										
										
											2015-06-15 18:36:32 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-08-06 00:30:37 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									conf ,  err  :=  config . LoadFile ( filename ) 
							 
						 
					
						
							
								
									
										
										
										
											2015-06-15 18:36:32 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									if  err  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2019-03-26 07:01:12 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										return  errors . Wrapf ( err ,  "couldn't load configuration (--config.file=%q)" ,  filename ) 
							 
						 
					
						
							
								
									
										
										
										
											2015-06-15 18:36:32 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-08-12 09:23:18 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									failed  :=  false 
							 
						 
					
						
							
								
									
										
										
										
											2015-06-15 18:36:32 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									for  _ ,  rl  :=  range  rls  { 
							 
						 
					
						
							
								
									
										
										
										
											2020-08-07 03:48:52 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										rstart  :=  time . Now ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										if  err  :=  rl . reloader ( conf ) ;  err  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2017-08-12 02:45:52 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											level . Error ( logger ) . Log ( "msg" ,  "Failed to apply configuration" ,  "err" ,  err ) 
							 
						 
					
						
							
								
									
										
										
										
											2016-08-12 09:23:18 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											failed  =  true 
							 
						 
					
						
							
								
									
										
										
										
											2016-07-11 22:24:54 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
									
										
										
										
											2020-08-07 03:48:52 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										timings  =  append ( timings ,  rl . name ,  time . Since ( rstart ) ) 
							 
						 
					
						
							
								
									
										
										
										
											2015-06-15 18:36:32 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2016-08-12 09:23:18 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									if  failed  { 
							 
						 
					
						
							
								
									
										
										
										
											2019-03-26 07:01:12 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										return  errors . Errorf ( "one or more errors occurred while applying the new configuration (--config.file=%q)" ,  filename ) 
							 
						 
					
						
							
								
									
										
										
										
											2016-08-12 09:23:18 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2018-12-18 03:16:28 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-07-22 21:39:51 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									noStepSuqueryInterval . Set ( conf . GlobalConfig . EvaluationInterval ) 
							 
						 
					
						
							
								
									
										
										
										
											2020-08-07 03:48:52 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									l  :=  [ ] interface { } { "msg" ,  "Completed loading of configuration file" ,  "filename" ,  filename ,  "totalDuration" ,  time . Since ( start ) } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									level . Info ( logger ) . Log ( append ( l ,  timings ... ) ... ) 
							 
						 
					
						
							
								
									
										
										
										
											2016-08-12 09:23:18 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									return  nil 
							 
						 
					
						
							
								
									
										
										
										
											2015-06-15 18:36:32 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
									
										
										
										
											2017-06-21 00:48:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-10-05 18:16:15 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								func  startsOrEndsWithQuote ( s  string )  bool  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									return  strings . HasPrefix ( s ,  "\"" )  ||  strings . HasPrefix ( s ,  "'" )  || 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										strings . HasSuffix ( s ,  "\"" )  ||  strings . HasSuffix ( s ,  "'" ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-01-17 23:01:06 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// compileCORSRegexString compiles given string and adds anchors
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								func  compileCORSRegexString ( s  string )  ( * regexp . Regexp ,  error )  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									r ,  err  :=  relabel . NewRegexp ( s ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									if  err  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										return  nil ,  err 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									return  r . Regexp ,  nil 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-06-21 00:48:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// computeExternalURL computes a sanitized external URL from a raw input. It infers unset
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// URL parts from the OS and the given listen address.
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								func  computeExternalURL ( u ,  listenAddr  string )  ( * url . URL ,  error )  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									if  u  ==  ""  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										hostname ,  err  :=  os . Hostname ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										if  err  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											return  nil ,  err 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										_ ,  port ,  err  :=  net . SplitHostPort ( listenAddr ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										if  err  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											return  nil ,  err 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										u  =  fmt . Sprintf ( "http://%s:%s/" ,  hostname ,  port ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-10-05 18:16:15 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									if  startsOrEndsWithQuote ( u )  { 
							 
						 
					
						
							
								
									
										
										
										
											2019-03-26 07:01:12 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										return  nil ,  errors . New ( "URL must not begin or end with quotes" ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-21 00:48:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									eu ,  err  :=  url . Parse ( u ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									if  err  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										return  nil ,  err 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									ppref  :=  strings . TrimRight ( eu . Path ,  "/" ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									if  ppref  !=  ""  &&  ! strings . HasPrefix ( ppref ,  "/" )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										ppref  =  "/"  +  ppref 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									eu . Path  =  ppref 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									return  eu ,  nil 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
									
										
										
										
											2017-11-24 15:59:05 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-12-18 19:15:46 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								type  sender  interface  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									Send ( alerts  ... * notifier . Alert ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2018-01-23 00:17:33 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// sendAlerts implements the rules.NotifyFunc for a Notifier.
  
						 
					
						
							
								
									
										
										
										
											2018-12-18 19:15:46 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								func  sendAlerts ( s  sender ,  externalURL  string )  rules . NotifyFunc  {  
						 
					
						
							
								
									
										
										
										
											2018-08-05 03:31:12 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									return  func ( ctx  context . Context ,  expr  string ,  alerts  ... * rules . Alert )  { 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-24 15:59:05 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										var  res  [ ] * notifier . Alert 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										for  _ ,  alert  :=  range  alerts  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											a  :=  & notifier . Alert { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												StartsAt :      alert . FiredAt , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												Labels :        alert . Labels , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												Annotations :   alert . Annotations , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												GeneratorURL :  externalURL  +  strutil . TableLinkForExpression ( expr ) , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											if  ! alert . ResolvedAt . IsZero ( )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												a . EndsAt  =  alert . ResolvedAt 
							 
						 
					
						
							
								
									
										
										
										
											2018-08-28 23:05:00 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											}  else  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												a . EndsAt  =  alert . ValidUntil 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-24 15:59:05 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											res  =  append ( res ,  a ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										if  len ( alerts )  >  0  { 
							 
						 
					
						
							
								
									
										
										
										
											2018-12-18 19:15:46 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											s . Send ( res ... ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-11-24 15:59:05 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
									
										
										
										
											2020-02-17 19:41:04 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// readyStorage implements the Storage interface while allowing to set the actual
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// storage at a later point in time.
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								type  readyStorage  struct  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									mtx              sync . RWMutex 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									db               * tsdb . DB 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									startTimeMargin  int64 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// Set the storage.
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								func  ( s  * readyStorage )  Set ( db  * tsdb . DB ,  startTimeMargin  int64 )  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									s . mtx . Lock ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									defer  s . mtx . Unlock ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									s . db  =  db 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									s . startTimeMargin  =  startTimeMargin 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-04-30 00:16:14 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// get is internal, you should use readyStorage as the front implementation layer.
  
						 
					
						
							
								
									
										
										
										
											2020-02-17 19:41:04 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								func  ( s  * readyStorage )  get ( )  * tsdb . DB  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									s . mtx . RLock ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									x  :=  s . db 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									s . mtx . RUnlock ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									return  x 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// StartTime implements the Storage interface.
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								func  ( s  * readyStorage )  StartTime ( )  ( int64 ,  error )  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									if  x  :=  s . get ( ) ;  x  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										var  startTime  int64 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										if  len ( x . Blocks ( ) )  >  0  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											startTime  =  x . Blocks ( ) [ 0 ] . Meta ( ) . MinTime 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										}  else  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											startTime  =  time . Now ( ) . Unix ( )  *  1000 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										// Add a safety margin as it may take a few minutes for everything to spin up.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										return  startTime  +  s . startTimeMargin ,  nil 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									return  math . MaxInt64 ,  tsdb . ErrNotReady 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// Querier implements the Storage interface.
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								func  ( s  * readyStorage )  Querier ( ctx  context . Context ,  mint ,  maxt  int64 )  ( storage . Querier ,  error )  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									if  x  :=  s . get ( ) ;  x  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										return  x . Querier ( ctx ,  mint ,  maxt ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									return  nil ,  tsdb . ErrNotReady 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-06-24 21:41:52 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// ChunkQuerier implements the Storage interface.
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								func  ( s  * readyStorage )  ChunkQuerier ( ctx  context . Context ,  mint ,  maxt  int64 )  ( storage . ChunkQuerier ,  error )  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									if  x  :=  s . get ( ) ;  x  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										return  x . ChunkQuerier ( ctx ,  mint ,  maxt ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									return  nil ,  tsdb . ErrNotReady 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-02-17 19:41:04 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// Appender implements the Storage interface.
  
						 
					
						
							
								
									
										
										
										
											2020-07-24 22:10:51 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								func  ( s  * readyStorage )  Appender ( ctx  context . Context )  storage . Appender  {  
						 
					
						
							
								
									
										
										
										
											2020-02-17 19:41:04 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									if  x  :=  s . get ( ) ;  x  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2020-07-24 22:10:51 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										return  x . Appender ( ctx ) 
							 
						 
					
						
							
								
									
										
										
										
											2020-02-17 19:41:04 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									return  notReadyAppender { } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								type  notReadyAppender  struct { }  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								func  ( n  notReadyAppender )  Add ( l  labels . Labels ,  t  int64 ,  v  float64 )  ( uint64 ,  error )  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									return  0 ,  tsdb . ErrNotReady 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								func  ( n  notReadyAppender )  AddFast ( ref  uint64 ,  t  int64 ,  v  float64 )  error  {  return  tsdb . ErrNotReady  }  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								func  ( n  notReadyAppender )  Commit ( )  error  {  return  tsdb . ErrNotReady  }  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								func  ( n  notReadyAppender )  Rollback ( )  error  {  return  tsdb . ErrNotReady  }  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// Close implements the Storage interface.
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								func  ( s  * readyStorage )  Close ( )  error  {  
						 
					
						
							
								
									
										
										
										
											2020-04-30 00:16:14 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									if  x  :=  s . get ( ) ;  x  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2020-02-17 19:41:04 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										return  x . Close ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									return  nil 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
									
										
										
										
											2020-02-18 19:25:36 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-04-30 00:16:14 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// CleanTombstones implements the api_v1.TSDBAdminStats and api_v2.TSDBAdmin interfaces.
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								func  ( s  * readyStorage )  CleanTombstones ( )  error  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									if  x  :=  s . get ( ) ;  x  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										return  x . CleanTombstones ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									return  tsdb . ErrNotReady 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// Delete implements the api_v1.TSDBAdminStats and api_v2.TSDBAdmin interfaces.
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								func  ( s  * readyStorage )  Delete ( mint ,  maxt  int64 ,  ms  ... * labels . Matcher )  error  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									if  x  :=  s . get ( ) ;  x  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										return  x . Delete ( mint ,  maxt ,  ms ... ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									return  tsdb . ErrNotReady 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// Snapshot implements the api_v1.TSDBAdminStats and api_v2.TSDBAdmin interfaces.
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								func  ( s  * readyStorage )  Snapshot ( dir  string ,  withHead  bool )  error  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									if  x  :=  s . get ( ) ;  x  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										return  x . Snapshot ( dir ,  withHead ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									return  tsdb . ErrNotReady 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// Stats implements the api_v1.TSDBAdminStats interface.
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								func  ( s  * readyStorage )  Stats ( statsByLabelName  string )  ( * tsdb . Stats ,  error )  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									if  x  :=  s . get ( ) ;  x  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										return  x . Head ( ) . Stats ( statsByLabelName ) ,  nil 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									return  nil ,  tsdb . ErrNotReady 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-02-18 19:25:36 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// tsdbOptions is tsdb.Option version with defined units.
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// This is required as tsdb.Option fields are unit agnostic (time).
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								type  tsdbOptions  struct  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									WALSegmentSize          units . Base2Bytes 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									RetentionDuration       model . Duration 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									MaxBytes                units . Base2Bytes 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									NoLockfile              bool 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									AllowOverlappingBlocks  bool 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									WALCompression          bool 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									StripeSize              int 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									MinBlockDuration        model . Duration 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									MaxBlockDuration        model . Duration 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								func  ( opts  tsdbOptions )  ToTSDBOptions ( )  tsdb . Options  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									return  tsdb . Options { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										WALSegmentSize :          int ( opts . WALSegmentSize ) , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										RetentionDuration :       int64 ( time . Duration ( opts . RetentionDuration )  /  time . Millisecond ) , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										MaxBytes :                int64 ( opts . MaxBytes ) , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										NoLockfile :              opts . NoLockfile , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										AllowOverlappingBlocks :  opts . AllowOverlappingBlocks , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										WALCompression :          opts . WALCompression , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										StripeSize :              opts . StripeSize , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										MinBlockDuration :        int64 ( time . Duration ( opts . MinBlockDuration )  /  time . Millisecond ) , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										MaxBlockDuration :        int64 ( time . Duration ( opts . MaxBlockDuration )  /  time . Millisecond ) , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
									
										
										
										
											2020-04-23 08:05:55 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								func  initTracing ( logger  log . Logger )  ( io . Closer ,  error )  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// Set tracing configuration defaults.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									cfg  :=  & jcfg . Configuration { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										ServiceName :  "prometheus" , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										Disabled :     true , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// Available options can be seen here:
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// https://github.com/jaegertracing/jaeger-client-go#environment-variables
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									cfg ,  err  :=  cfg . FromEnv ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									if  err  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										return  nil ,  errors . Wrap ( err ,  "unable to get tracing config from environment" ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									jLogger  :=  jaegerLogger { logger :  log . With ( logger ,  "component" ,  "tracing" ) } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									tracer ,  closer ,  err  :=  cfg . NewTracer ( 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										jcfg . Logger ( jLogger ) , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										jcfg . Metrics ( jprom . New ( ) ) , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									if  err  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										return  nil ,  errors . Wrap ( err ,  "unable to init tracing" ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									opentracing . SetGlobalTracer ( tracer ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									return  closer ,  nil 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								type  jaegerLogger  struct  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									logger  log . Logger 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								func  ( l  jaegerLogger )  Error ( msg  string )  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									level . Error ( l . logger ) . Log ( "msg" ,  msg ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								func  ( l  jaegerLogger )  Infof ( msg  string ,  args  ... interface { } )  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									keyvals  :=  [ ] interface { } { "msg" ,  fmt . Sprintf ( msg ,  args ... ) } 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									level . Info ( l . logger ) . Log ( keyvals ... ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}