Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Expose metrics through http endpoint #3717

Merged
merged 1 commit into from
Apr 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ coverage:
default:
# basic
target: auto
threshold: null
threshold: 0.1
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uhm... correct PR?

Copy link
Contributor Author

@ruflin ruflin Apr 3, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kind off, because otherwise the PR would fail :-) I will keep this one in ...

base: auto
# advanced
branches: null
Expand Down
13 changes: 13 additions & 0 deletions filebeat/filebeat.full.yml
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,19 @@ output.elasticsearch:
# dashboards and index pattern. Example: testbeat-*
#dashboards.index:

#================================ HTTP Endpoint ======================================
# Each beat can expose internal data points through a http endpoint. For security
# reason the endpoint is disabled by default. This feature is currently in beta.

# Defines if http endpoint is enabled
#http.enabled: false

# Host to expose the http endpoint to. It is recommended to use only localhost.
#http.host: localhost

# Port on which the http endpoint is exposed. Default is 5066
#http.port: 5066

#================================ Logging ======================================
# There are three options for the log output: syslog, file, stderr.
# Under Windows systems, the log files are per default sent to the file output,
Expand Down
13 changes: 13 additions & 0 deletions heartbeat/heartbeat.full.yml
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,19 @@ output.elasticsearch:
# dashboards and index pattern. Example: testbeat-*
#dashboards.index:

#================================ HTTP Endpoint ======================================
# Each beat can expose internal data points through a http endpoint. For security
# reason the endpoint is disabled by default. This feature is currently in beta.

# Defines if http endpoint is enabled
#http.enabled: false

# Host to expose the http endpoint to. It is recommended to use only localhost.
#http.host: localhost

# Port on which the http endpoint is exposed. Default is 5066
#http.port: 5066

#================================ Logging ======================================
# There are three options for the log output: syslog, file, stderr.
# Under Windows systems, the log files are per default sent to the file output,
Expand Down
13 changes: 13 additions & 0 deletions libbeat/_meta/config.full.yml
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,19 @@ output.elasticsearch:
# dashboards and index pattern. Example: testbeat-*
#dashboards.index:

#================================ HTTP Endpoint ======================================
# Each beat can expose internal data points through a http endpoint. For security
# reason the endpoint is disabled by default. This feature is currently in beta.

# Defines if http endpoint is enabled
#http.enabled: false

# Host to expose the http endpoint to. It is recommended to use only localhost.
#http.host: localhost

# Port on which the http endpoint is exposed. Default is 5066
#http.port: 5066

#================================ Logging ======================================
# There are three options for the log output: syslog, file, stderr.
# Under Windows systems, the log files are per default sent to the file output,
Expand Down
15 changes: 15 additions & 0 deletions libbeat/api/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package api

type Config struct {
Enabled bool
Host string
Port int
}

var (
DefaultConfig = Config{
Enabled: false,
Host: "localhost",
Port: 5066,
}
)
73 changes: 73 additions & 0 deletions libbeat/api/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package api

import (
"fmt"
"net/http"
"net/url"
"strconv"

"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/libbeat/monitoring"
)

// Start starts the metrics api endpoint on the configured host and port
func Start(cfg *common.Config, info common.BeatInfo) {
logp.Beta("Metrics endpoint is enabled.")
config := DefaultConfig
cfg.Unpack(&config)

logp.Info("Starting stats endpoint")
go func() {
mux := http.NewServeMux()

// register handlers
mux.HandleFunc("/", rootHandler(info))
mux.HandleFunc("/stats", statsHandler)

url := config.Host + ":" + strconv.Itoa(config.Port)
logp.Info("URL: %s", url)
endpoint := http.ListenAndServe(url, mux)
logp.Info("finished starting stats endpoint: %v", endpoint)
}()
}

func rootHandler(info common.BeatInfo) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
// Return error page
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}

w.Header().Set("Content-Type", "application/json; charset=utf-8")

data := common.MapStr{
"version": info.Version,
"beat": info.Beat,
"name": info.Name,
"uuid": info.UUID,
"hostname": info.Hostname,
}

print(w, data, r.URL)
}
}

// statsHandler report expvar and all libbeat/monitoring metrics
func statsHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")

data := monitoring.CollectStructSnapshot(nil, monitoring.Full, false)

print(w, data, r.URL)
}

func print(w http.ResponseWriter, data common.MapStr, u *url.URL) {
query := u.Query()
if _, ok := query["pretty"]; ok {
fmt.Fprintf(w, data.StringToPrint())
} else {
fmt.Fprintf(w, data.String())
}
}
6 changes: 6 additions & 0 deletions libbeat/beat/beat.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import (
"github.com/satori/go.uuid"

"github.com/elastic/beats/filebeat/input/file"
"github.com/elastic/beats/libbeat/api"
"github.com/elastic/beats/libbeat/cfgfile"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/dashboards"
Expand Down Expand Up @@ -110,6 +111,7 @@ type BeatConfig struct {
Processors processors.PluginConfig `config:"processors"`
Path paths.Path `config:"path"`
Dashboards *common.Config `config:"dashboards"`
Http *common.Config `config:"http"`
}

var (
Expand Down Expand Up @@ -252,6 +254,10 @@ func (b *Beat) launch(bt Creator) error {
defer logp.Info("%s stopped.", b.Info.Beat)
defer logp.LogTotalExpvars(&b.Config.Logging)

if b.Config.Http.Enabled() {
api.Start(b.Config.Http, b.Info)
}

return beater.Run(b)
}

Expand Down
13 changes: 13 additions & 0 deletions metricbeat/metricbeat.full.yml
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,19 @@ output.elasticsearch:
# dashboards and index pattern. Example: testbeat-*
#dashboards.index:

#================================ HTTP Endpoint ======================================
# Each beat can expose internal data points through a http endpoint. For security
# reason the endpoint is disabled by default. This feature is currently in beta.

# Defines if http endpoint is enabled
#http.enabled: false

# Host to expose the http endpoint to. It is recommended to use only localhost.
#http.host: localhost

# Port on which the http endpoint is exposed. Default is 5066
#http.port: 5066

#================================ Logging ======================================
# There are three options for the log output: syslog, file, stderr.
# Under Windows systems, the log files are per default sent to the file output,
Expand Down
13 changes: 13 additions & 0 deletions packetbeat/packetbeat.full.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1043,6 +1043,19 @@ output.elasticsearch:
# dashboards and index pattern. Example: testbeat-*
#dashboards.index:

#================================ HTTP Endpoint ======================================
# Each beat can expose internal data points through a http endpoint. For security
# reason the endpoint is disabled by default. This feature is currently in beta.

# Defines if http endpoint is enabled
#http.enabled: false

# Host to expose the http endpoint to. It is recommended to use only localhost.
#http.host: localhost

# Port on which the http endpoint is exposed. Default is 5066
#http.port: 5066

#================================ Logging ======================================
# There are three options for the log output: syslog, file, stderr.
# Under Windows systems, the log files are per default sent to the file output,
Expand Down
13 changes: 13 additions & 0 deletions winlogbeat/winlogbeat.full.yml
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,19 @@ output.elasticsearch:
# dashboards and index pattern. Example: testbeat-*
#dashboards.index:

#================================ HTTP Endpoint ======================================
# Each beat can expose internal data points through a http endpoint. For security
# reason the endpoint is disabled by default. This feature is currently in beta.

# Defines if http endpoint is enabled
#http.enabled: false

# Host to expose the http endpoint to. It is recommended to use only localhost.
#http.host: localhost

# Port on which the http endpoint is exposed. Default is 5066
#http.port: 5066

#================================ Logging ======================================
# There are three options for the log output: syslog, file, stderr.
# Under Windows systems, the log files are per default sent to the file output,
Expand Down