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  (  
						 
					
						
							
								
									
										
										
										
											2015-06-15 18:23:02 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"fmt" 
							 
						 
					
						
							
								
									
										
										
										
											2015-06-15 18:36:32 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									_  "net/http/pprof"  // Comment this line to disable pprof endpoint.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									"os" 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									"os/signal" 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									"syscall" 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									"time" 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-06-24 00:04:04 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"github.com/prometheus/client_golang/prometheus" 
							 
						 
					
						
							
								
									
										
										
										
											2016-05-05 19:46:51 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"github.com/prometheus/common/log" 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									"github.com/prometheus/common/version" 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-15 19:08:08 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"github.com/spf13/cobra" 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									"github.com/spf13/pflag" 
							 
						 
					
						
							
								
									
										
											 
										
											
												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 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"golang.org/x/net/context" 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-06-15 18:36:32 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									"github.com/prometheus/prometheus/config" 
							 
						 
					
						
							
								
									
										
										
										
											2016-03-01 19:37:22 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"github.com/prometheus/prometheus/notifier" 
							 
						 
					
						
							
								
									
										
										
										
											2015-06-15 18:36:32 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									"github.com/prometheus/prometheus/promql" 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									"github.com/prometheus/prometheus/retrieval" 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									"github.com/prometheus/prometheus/rules" 
							 
						 
					
						
							
								
									
										
										
										
											2016-12-29 16:27:30 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									"github.com/prometheus/prometheus/storage/tsdb" 
							 
						 
					
						
							
								
									
										
										
										
											2015-06-15 18:36:32 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									"github.com/prometheus/prometheus/web" 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								)  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								func  main ( )  {  
						 
					
						
							
								
									
										
										
										
											2017-06-15 19:08:08 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									newRootCmd ( ) . Execute ( ) 
							 
						 
					
						
							
								
									
										
										
										
											2015-06-15 18:36:32 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-09-02 01:18:39 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								var  (  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									configSuccess  =  prometheus . NewGauge ( prometheus . GaugeOpts { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										Namespace :  "prometheus" , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										Name :       "config_last_reload_successful" , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										Help :       "Whether the last configuration reload attempt was successful." , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									configSuccessTime  =  prometheus . NewGauge ( prometheus . GaugeOpts { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										Namespace :  "prometheus" , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										Name :       "config_last_reload_success_timestamp_seconds" , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										Help :       "Timestamp of the last successful configuration reload." , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								)  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-05-05 19:46:51 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								func  init ( )  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									prometheus . MustRegister ( version . NewCollector ( "prometheus" ) ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-06-15 19:08:08 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								func  newRootCmd ( )  * cobra . Command  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									rootCmd  :=  & cobra . Command { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										Use :    "prometheus" , 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-16 18:25:13 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										Short :  "prometheus --config.file=prometheus.yaml" , 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-15 19:08:08 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										Run :  func ( cmd  * cobra . Command ,  args  [ ] string )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											os . Exit ( Main ( ) ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										} , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									rootCmd . PersistentFlags ( ) . BoolVar ( 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										& cfg . printVersion ,  "version" ,  false , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										"Print version information." , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									rootCmd . PersistentFlags ( ) . StringVar ( 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										& cfg . configFile ,  "config.file" ,  "prometheus.yml" , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										"Prometheus configuration file name." , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// Web.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									rootCmd . PersistentFlags ( ) . StringVar ( 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										& cfg . web . ListenAddress ,  "web.listen-address" ,  ":9090" , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										"Address to listen on for the web interface, API, and telemetry." , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-06-16 21:07:34 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									rootCmd . PersistentFlags ( ) . Var ( 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										& cfg . webTimeout ,  "web.read-timeout" , 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-15 19:08:08 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										"Maximum duration before timing out read of the request, and closing idle connections." , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									rootCmd . PersistentFlags ( ) . IntVar ( 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										& cfg . web . MaxConnections ,  "web.max-connections" ,  512 , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										"Maximum number of simultaneous connections." , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									rootCmd . PersistentFlags ( ) . StringVar ( 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										& cfg . prometheusURL ,  "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." , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									rootCmd . PersistentFlags ( ) . StringVar ( 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										& cfg . web . RoutePrefix ,  "web.route-prefix" ,  "" , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										"Prefix for the internal routes of web endpoints. Defaults to path of -web.external-url." , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									rootCmd . PersistentFlags ( ) . StringVar ( 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										& cfg . web . MetricsPath ,  "web.telemetry-path" ,  "/metrics" , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										"Path under which to expose metrics." , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									rootCmd . PersistentFlags ( ) . StringVar ( 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										& cfg . web . UserAssetsPath ,  "web.user-assets" ,  "" , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										"Path to static asset directory, available at /user." , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									rootCmd . PersistentFlags ( ) . BoolVar ( 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										& cfg . web . EnableQuit ,  "web.enable-remote-shutdown" ,  false , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										"Enable remote service shutdown." , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									rootCmd . PersistentFlags ( ) . StringVar ( 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										& cfg . web . ConsoleTemplatesPath ,  "web.console.templates" ,  "consoles" , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										"Path to the console template directory, available at /consoles." , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									rootCmd . PersistentFlags ( ) . StringVar ( 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										& cfg . web . ConsoleLibrariesPath ,  "web.console.libraries" ,  "console_libraries" , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										"Path to the console library directory." , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// Storage.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									rootCmd . PersistentFlags ( ) . StringVar ( 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										& cfg . localStoragePath ,  "storage.local.path" ,  "data" , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										"Base path for metrics storage." , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									rootCmd . PersistentFlags ( ) . BoolVar ( 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										& cfg . tsdb . NoLockfile ,  "storage.tsdb.no-lockfile" ,  false , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										"Disable lock file usage." , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									) 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-16 21:07:34 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									rootCmd . PersistentFlags ( ) . Var ( 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										& cfg . tsdb . MinBlockDuration ,  "storage.tsdb.min-block-duration" , 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-15 19:08:08 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										"Minimum duration of a data block before being persisted." , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									) 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-16 21:07:34 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									rootCmd . PersistentFlags ( ) . Var ( 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										& cfg . tsdb . MaxBlockDuration ,  "storage.tsdb.max-block-duration" , 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-15 19:08:08 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										"Maximum duration compacted blocks may span. (Defaults to 10% of the retention period)" , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									) 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-16 21:07:34 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									rootCmd . PersistentFlags ( ) . Var ( 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										& cfg . tsdb . Retention ,  "storage.tsdb.retention" , 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-15 19:08:08 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										"How long to retain samples in the storage." , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									rootCmd . PersistentFlags ( ) . StringVar ( 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										& cfg . localStorageEngine ,  "storage.local.engine" ,  "persisted" , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										"Local storage engine. Supported values are: 'persisted' (full local storage with on-disk persistence) and 'none' (no local storage)." , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// Alertmanager.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									rootCmd . PersistentFlags ( ) . IntVar ( 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										& cfg . notifier . QueueCapacity ,  "alertmanager.notification-queue-capacity" ,  10000 , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										"The capacity of the queue for pending alert manager notifications." , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									) 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-16 21:07:34 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									rootCmd . PersistentFlags ( ) . Var ( 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										& cfg . notifierTimeout ,  "alertmanager.timeout" , 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-15 19:08:08 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										"Alert manager HTTP API timeout." , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// Query engine.
 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-16 21:07:34 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									rootCmd . PersistentFlags ( ) . Var ( 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										& cfg . lookbackDelta ,  "query.lookback-delta" , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										"The delta difference allowed for retrieving metrics during expression evaluations." , 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-15 19:08:08 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									) 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-16 21:07:34 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									rootCmd . PersistentFlags ( ) . Var ( 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										& cfg . queryTimeout ,  "query.timeout" , 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-15 19:08:08 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										"Maximum time a query may take before being aborted." , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									rootCmd . PersistentFlags ( ) . IntVar ( 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										& cfg . queryEngine . MaxConcurrentQueries ,  "query.max-concurrency" ,  20 , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										"Maximum number of queries executed concurrently." , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-06-16 18:22:44 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// Logging.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									rootCmd . PersistentFlags ( ) . StringVar ( 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										& cfg . logLevel ,  "log.level" ,  "info" , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										"Only log messages with the given severity or above. Valid levels: [debug, info, warn, error, fatal]" , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									rootCmd . PersistentFlags ( ) . StringVar ( 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										& cfg . logFormat ,  "log.format" ,  "logger:stderr" , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										` Set the log target and format. Example: "logger:syslog?appname=bob&local=7" or "logger:stdout?json=true" ` , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									) 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-15 19:08:08 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-06-16 18:22:44 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									cfg . fs  =  rootCmd . PersistentFlags ( ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-15 19:08:08 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									return  rootCmd 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-01-19 18:29:00 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								// Main manages the stup and shutdown lifecycle of the entire Prometheus server.
  
						 
					
						
							
								
									
										
										
										
											2015-06-15 18:36:32 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								func  Main ( )  int  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									if  err  :=  parse ( os . Args [ 1 : ] ) ;  err  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2016-02-19 19:18:19 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										log . Error ( err ) 
							 
						 
					
						
							
								
									
										
										
										
											2015-06-15 18:36:32 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										return  2 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-06-16 18:22:44 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									logger  :=  log . NewLogger ( os . Stdout ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									logger . SetLevel ( cfg . logLevel ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									logger . SetFormat ( cfg . logFormat ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-06-15 18:36:32 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									if  cfg . printVersion  { 
							 
						 
					
						
							
								
									
										
										
										
											2016-05-05 19:46:51 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										fmt . Fprintln ( os . Stdout ,  version . Print ( "prometheus" ) ) 
							 
						 
					
						
							
								
									
										
										
										
											2015-06-15 18:36:32 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										return  0 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-06-16 18:22:44 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									logger . Infoln ( "Starting prometheus" ,  version . Info ( ) ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									logger . Infoln ( "Build context" ,  version . BuildContext ( ) ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									logger . Infoln ( "Host details" ,  Uname ( ) ) 
							 
						 
					
						
							
								
									
										
										
										
											2016-05-05 19:46:51 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-06-15 18:36:32 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									var  ( 
							 
						 
					
						
							
								
									
										
										
										
											2016-12-29 16:27:30 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										// sampleAppender = storage.Fanout{}
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										reloadables  [ ] Reloadable 
							 
						 
					
						
							
								
									
										
										
										
											2015-06-15 18:36:32 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									) 
							 
						 
					
						
							
								
									
										
										
										
											2016-08-30 00:48:20 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-05-10 00:00:54 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// Make sure that sighup handler is registered with a redirect to the channel before the potentially
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// long and synchronous tsdb init.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									hup  :=  make ( chan  os . Signal ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									hupReady  :=  make ( chan  bool ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									signal . Notify ( hup ,  syscall . SIGHUP ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-16 18:22:44 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									logger . Infoln ( "Starting tsdb" ) 
							 
						 
					
						
							
								
									
										
										
										
											2017-02-28 16:33:14 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									localStorage ,  err  :=  tsdb . Open ( cfg . localStoragePath ,  prometheus . DefaultRegisterer ,  & cfg . tsdb ) 
							 
						 
					
						
							
								
									
										
										
										
											2016-12-23 20:51:59 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									if  err  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										log . Errorf ( "Opening storage failed: %s" ,  err ) 
							 
						 
					
						
							
								
									
										
										
										
											2016-12-29 16:27:30 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										return  1 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-16 18:22:44 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									logger . Infoln ( "tsdb started" ) 
							 
						 
					
						
							
								
									
										
										
										
											2016-12-29 16:27:30 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-02-28 16:43:16 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// remoteStorage := &remote.Storage{}
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// sampleAppender = append(sampleAppender, remoteStorage)
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// reloadables = append(reloadables, remoteStorage)
 
							 
						 
					
						
							
								
									
										
										
										
											2016-09-20 04:47:51 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-06-16 18:22:44 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									cfg . queryEngine . Logger  =  logger 
							 
						 
					
						
							
								
									
										
										
										
											2015-06-26 07:32:44 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									var  ( 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-16 18:22:44 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										notifier        =  notifier . New ( & cfg . notifier ,  logger ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										targetManager   =  retrieval . NewTargetManager ( localStorage ,  logger ) 
							 
						 
					
						
							
								
									
										
										
										
											2016-09-16 06:58:06 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										queryEngine     =  promql . NewEngine ( localStorage ,  & cfg . queryEngine ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										ctx ,  cancelCtx  =  context . WithCancel ( context . Background ( ) ) 
							 
						 
					
						
							
								
									
										
										
										
											2015-06-26 07:32:44 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-06-15 18:36:32 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									ruleManager  :=  rules . NewManager ( & rules . ManagerOptions { 
							 
						 
					
						
							
								
									
										
										
										
											2017-01-13 21:48:01 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										Appendable :   localStorage , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										Notifier :     notifier , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										QueryEngine :  queryEngine , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										Context :      ctx , 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										ExternalURL :  cfg . web . ExternalURL , 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-16 18:22:44 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										Logger :       logger , 
							 
						 
					
						
							
								
									
										
										
										
											2015-06-15 18:36:32 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									} ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-09-16 06:58:06 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									cfg . web . Context  =  ctx 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									cfg . web . Storage  =  localStorage 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									cfg . web . QueryEngine  =  queryEngine 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									cfg . web . TargetManager  =  targetManager 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									cfg . web . RuleManager  =  ruleManager 
							 
						 
					
						
							
								
									
										
										
										
											2016-11-24 01:23:09 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									cfg . web . Notifier  =  notifier 
							 
						 
					
						
							
								
									
										
										
										
											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 { } 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-15 19:08:08 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									cfg . fs . VisitAll ( func ( f  * pflag . Flag )  { 
							 
						 
					
						
							
								
									
										
										
										
											2016-09-16 06:58:06 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										cfg . web . Flags [ f . Name ]  =  f . Value . String ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									webHandler  :=  web . New ( & cfg . web ) 
							 
						 
					
						
							
								
									
										
										
										
											2015-06-15 18:36:32 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-05-13 23:59:59 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									reloadables  =  append ( reloadables ,  targetManager ,  ruleManager ,  webHandler ,  notifier ) 
							 
						 
					
						
							
								
									
										
										
										
											2015-09-02 00:47:48 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-06-16 18:22:44 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									if  err  :=  reloadConfig ( cfg . configFile ,  logger ,  reloadables ... ) ;  err  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										logger . Errorf ( "Error loading config: %s" ,  err ) 
							 
						 
					
						
							
								
									
										
										
										
											2015-06-24 00:04:04 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										return  1 
							 
						 
					
						
							
								
									
										
										
										
											2015-06-15 18:36:32 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// Wait for reload or termination signals. Start the handler for SIGHUP as
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// early as possible, but ignore it until we are ready to handle reloading
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// our config.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									go  func ( )  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										<- hupReady 
							 
						 
					
						
							
								
									
										
										
										
											2015-08-11 15:08:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										for  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											select  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											case  <- hup : 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-16 18:22:44 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												if  err  :=  reloadConfig ( cfg . configFile ,  logger ,  reloadables ... ) ;  err  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
													logger . Errorf ( "Error reloading config: %s" ,  err ) 
							 
						 
					
						
							
								
									
										
										
										
											2016-07-11 22:24:54 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											case  rc  :=  <- webHandler . Reload ( ) : 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-16 18:22:44 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
												if  err  :=  reloadConfig ( cfg . configFile ,  logger ,  reloadables ... ) ;  err  !=  nil  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
													logger . Errorf ( "Error reloading config: %s" ,  err ) 
							 
						 
					
						
							
								
									
										
										
										
											2016-07-11 22:24:54 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
													rc  <-  err 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												}  else  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
													rc  <-  nil 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
												} 
							 
						 
					
						
							
								
									
										
										
										
											2015-08-11 15:08:17 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											} 
							 
						 
					
						
							
								
									
										
										
										
											2015-06-15 18:36:32 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-18 23:47:31 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// Start all components. The order is NOT arbitrary.
 
							 
						 
					
						
							
								
									
										
										
										
											2015-06-15 18:36:32 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									defer  func ( )  { 
							 
						 
					
						
							
								
									
										
										
										
											2016-12-29 16:27:30 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										if  err  :=  localStorage . Close ( ) ;  err  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-16 18:22:44 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											logger . Errorln ( "Error stopping storage:" ,  err ) 
							 
						 
					
						
							
								
									
										
										
										
											2015-06-15 18:36:32 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									} ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-02-28 16:43:16 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// defer remoteStorage.Stop()
 
							 
						 
					
						
							
								
									
										
										
										
											2016-09-20 04:47:51 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-09-02 01:18:39 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									prometheus . MustRegister ( configSuccess ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									prometheus . MustRegister ( configSuccessTime ) 
							 
						 
					
						
							
								
									
										
										
										
											2015-06-15 18:36:32 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-09-22 04:59:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// The notifier is a dependency of the rule manager. It has to be
 
							 
						 
					
						
							
								
									
										
										
										
											2016-01-18 23:47:31 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// started before and torn down afterwards.
 
							 
						 
					
						
							
								
									
										
										
										
											2016-03-01 19:37:22 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									go  notifier . Run ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									defer  notifier . Stop ( ) 
							 
						 
					
						
							
								
									
										
										
										
											2015-06-15 18:36:32 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-18 23:47:31 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									go  ruleManager . Run ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									defer  ruleManager . Stop ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2015-06-15 18:36:32 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									go  targetManager . Run ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									defer  targetManager . Stop ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2016-01-18 23:47:31 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									// Shutting down the query engine before the rule manager will cause pending queries
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// to be canceled and ensures a quick shutdown of the rule manager.
 
							 
						 
					
						
							
								
									
										
										
										
											2016-09-16 06:58:06 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									defer  cancelCtx ( ) 
							 
						 
					
						
							
								
									
										
										
										
											2015-06-15 18:36:32 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									go  webHandler . Run ( ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									// Wait for reload or termination signals.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									close ( hupReady )  // Unblock SIGHUP handler.
 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									term  :=  make ( chan  os . Signal ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									signal . Notify ( term ,  os . Interrupt ,  syscall . SIGTERM ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									select  { 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									case  <- term : 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-16 18:22:44 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										logger . Warn ( "Received SIGTERM, exiting gracefully..." ) 
							 
						 
					
						
							
								
									
										
										
										
											2015-06-15 18:36:32 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									case  <- webHandler . Quit ( ) : 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-16 18:22:44 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										logger . Warn ( "Received termination request via web service, exiting gracefully..." ) 
							 
						 
					
						
							
								
									
										
										
										
											2015-08-21 00:23:57 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									case  err  :=  <- webHandler . ListenError ( ) : 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-16 18:22:44 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										logger . Errorln ( "Error starting web server, exiting gracefully:" ,  err ) 
							 
						 
					
						
							
								
									
										
										
										
											2015-06-15 18:36:32 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-06-16 18:22:44 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									logger . Info ( "See you next time!" ) 
							 
						 
					
						
							
								
									
										
										
										
											2015-06-15 18:36:32 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									return  0 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// Reloadable things can change their internal state to match a new config
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								// and handle failure gracefully.
  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								type  Reloadable  interface  {  
						 
					
						
							
								
									
										
										
										
											2016-07-11 22:24:54 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									ApplyConfig ( * config . Config )  error 
							 
						 
					
						
							
								
									
										
										
										
											2015-06-15 18:36:32 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								}  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2017-06-16 18:22:44 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								func  reloadConfig ( filename  string ,  logger  log . Logger ,  rls  ... Reloadable )  ( err  error )  {  
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									logger . Infof ( "Loading configuration file %s" ,  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 ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
											configSuccessTime . Set ( float64 ( time . Now ( ) . Unix ( ) ) ) 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
										}  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  { 
							 
						 
					
						
							
								
									
										
										
										
											2016-07-11 22:24:54 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										return  fmt . Errorf ( "couldn't load configuration (-config.file=%s): %v" ,  filename ,  err ) 
							 
						 
					
						
							
								
									
										
										
										
											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  { 
							 
						 
					
						
							
								
									
										
										
										
											2016-08-12 09:23:18 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										if  err  :=  rl . ApplyConfig ( conf ) ;  err  !=  nil  { 
							 
						 
					
						
							
								
									
										
										
										
											2017-06-16 18:22:44 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											logger . Error ( "Failed to apply configuration: " ,  err ) 
							 
						 
					
						
							
								
									
										
										
										
											2016-08-12 09:23:18 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
											failed  =  true 
							 
						 
					
						
							
								
									
										
										
										
											2016-07-11 22:24:54 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										} 
							 
						 
					
						
							
								
									
										
										
										
											2015-06-15 18:36:32 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
									
										
										
										
											2016-08-12 09:23:18 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									if  failed  { 
							 
						 
					
						
							
								
									
										
										
										
											2016-09-15 11:23:28 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
										return  fmt . Errorf ( "one or more errors occurred while applying the new configuration (-config.file=%s)" ,  filename ) 
							 
						 
					
						
							
								
									
										
										
										
											2016-08-12 09:23:18 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
									} 
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
									return  nil 
							 
						 
					
						
							
								
									
										
										
										
											2015-06-15 18:36:32 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								}