Skip to content

Commit

Permalink
#34: Make metrics path configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
fstab committed May 30, 2018
1 parent f7828e5 commit edcf347
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
9 changes: 9 additions & 0 deletions config/v2/configV2.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ type ServerConfig struct {
Protocol string `yaml:",omitempty"`
Host string `yaml:",omitempty"`
Port int `yaml:",omitempty"`
Path string `yaml:",omitempty"`
Cert string `yaml:",omitempty"`
Key string `yaml:",omitempty"`
}
Expand Down Expand Up @@ -138,6 +139,9 @@ func (c *ServerConfig) addDefaults() {
if c.Port == 0 {
c.Port = 9144
}
if c.Path == "" {
c.Path = "/metrics"
}
}

func (cfg *Config) validate() error {
Expand Down Expand Up @@ -289,6 +293,8 @@ func (c *ServerConfig) validate() error {
return fmt.Errorf("Invalid 'server.protocol': '%v'. Expecting 'http' or 'https'.", c.Protocol)
case c.Port <= 0:
return fmt.Errorf("Invalid 'server.port': '%v'.", c.Port)
case !strings.HasPrefix(c.Path, "/"):
return fmt.Errorf("Invalid server configuration: 'server.path' must start with '/'.")
case c.Protocol == "https":
if c.Cert != "" && c.Key == "" {
return fmt.Errorf("Invalid server configuration: 'server.cert' must not be specified without 'server.key'")
Expand Down Expand Up @@ -365,6 +371,9 @@ func (cfg *Config) String() string {
if stripped.Input.FailOnMissingLogfileString == "true" {
stripped.Input.FailOnMissingLogfileString = ""
}
if stripped.Server.Path == "/metrics" {
stripped.Server.Path = ""
}
return stripped.marshalToString()
}

Expand Down
20 changes: 10 additions & 10 deletions grok_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func main() {
tail, err := startTailer(cfg)
exitOnError(err)
fmt.Print(startMsg(cfg))
serverErrors := startServer(cfg, "/metrics", prometheus.Handler())
serverErrors := startServer(cfg.Server, prometheus.Handler())

retentionTicker := time.NewTicker(cfg.Global.RetentionCheckInterval)

Expand Down Expand Up @@ -130,7 +130,7 @@ func startMsg(cfg *v2.Config) string {
host = hostname
}
}
return fmt.Sprintf("Starting server on %v://%v:%v/metrics\n", cfg.Server.Protocol, host, cfg.Server.Port)
return fmt.Sprintf("Starting server on %v://%v:%v%v\n", cfg.Server.Protocol, host, cfg.Server.Port, cfg.Server.Path)
}

func exitOnError(err error) {
Expand Down Expand Up @@ -245,21 +245,21 @@ func initSelfMonitoring(metrics []exporter.Metric) (*prometheus.CounterVec, *pro
return nLinesTotal, nMatchesByMetric, procTimeMicrosecondsByMetric, nErrorsByMetric
}

func startServer(cfg *v2.Config, path string, handler http.Handler) chan error {
func startServer(cfg v2.ServerConfig, handler http.Handler) chan error {
serverErrors := make(chan error)
go func() {
switch {
case cfg.Server.Protocol == "http":
serverErrors <- exporter.RunHttpServer(cfg.Server.Host, cfg.Server.Port, path, handler)
case cfg.Server.Protocol == "https":
if cfg.Server.Cert != "" && cfg.Server.Key != "" {
serverErrors <- exporter.RunHttpsServer(cfg.Server.Host, cfg.Server.Port, cfg.Server.Cert, cfg.Server.Key, path, handler)
case cfg.Protocol == "http":
serverErrors <- exporter.RunHttpServer(cfg.Host, cfg.Port, cfg.Path, handler)
case cfg.Protocol == "https":
if cfg.Cert != "" && cfg.Key != "" {
serverErrors <- exporter.RunHttpsServer(cfg.Host, cfg.Port, cfg.Cert, cfg.Key, cfg.Path, handler)
} else {
serverErrors <- exporter.RunHttpsServerWithDefaultKeys(cfg.Server.Host, cfg.Server.Port, path, handler)
serverErrors <- exporter.RunHttpsServerWithDefaultKeys(cfg.Host, cfg.Port, cfg.Path, handler)
}
default:
// This cannot happen, because cfg.validate() makes sure that protocol is either http or https.
serverErrors <- fmt.Errorf("Configuration error: Invalid 'server.protocol': '%v'. Expecting 'http' or 'https'.", cfg.Server.Protocol)
serverErrors <- fmt.Errorf("Configuration error: Invalid 'server.protocol': '%v'. Expecting 'http' or 'https'.", cfg.Protocol)
}
}()
return serverErrors
Expand Down

0 comments on commit edcf347

Please sign in to comment.