-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from BonnierNews/issue-11
Expose Logstash/OS/JVM info as labels on a static metric
- Loading branch information
Showing
8 changed files
with
201 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
0.1.1 | ||
0.1.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package collector | ||
|
||
import ( | ||
"encoding/json" | ||
"github.com/prometheus/common/log" | ||
"net/http" | ||
) | ||
|
||
// HTTPHandler type | ||
type HTTPHandler struct { | ||
Endpoint string | ||
} | ||
|
||
// Get method for HTTPHandler | ||
func (h *HTTPHandler) Get() (http.Response, error) { | ||
response, err := http.Get(h.Endpoint) | ||
if err != nil { | ||
return http.Response{}, err | ||
} | ||
|
||
return *response, nil | ||
} | ||
|
||
// HTTPHandlerInterface interface | ||
type HTTPHandlerInterface interface { | ||
Get() (http.Response, error) | ||
} | ||
|
||
func getMetrics(h HTTPHandlerInterface, target interface{}) error { | ||
response, err := h.Get() | ||
if err != nil { | ||
log.Errorf("Cannot retrieve metrics: %s", err) | ||
return nil | ||
} | ||
|
||
defer func() { | ||
err = response.Body.Close() | ||
if err != nil { | ||
log.Errorf("Cannot close response body: %v", err) | ||
} | ||
}() | ||
|
||
if err := json.NewDecoder(response.Body).Decode(target); err != nil { | ||
log.Errorf("Cannot parse Logstash response json: %s", err) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package collector | ||
|
||
// NodeInfoResponse type | ||
type NodeInfoResponse struct { | ||
Host string `json:"host"` | ||
Version string `json:"version"` | ||
HTTPAddress string `json:"http_address"` | ||
ID string `json:"id"` | ||
Name string `json:"name"` | ||
Pipeline struct { | ||
Workers int `json:"workers"` | ||
BatchSize int `json:"batch_size"` | ||
BatchDelay int `json:"batch_delay"` | ||
ConfigReloadAutomatic bool `json:"config_reload_automatic"` | ||
ConfigReloadInterval int `json:"config_reload_interval"` | ||
} `json:"pipeline"` | ||
Os struct { | ||
Name string `json:"name"` | ||
Arch string `json:"arch"` | ||
Version string `json:"version"` | ||
AvailableProcessors int `json:"available_processors"` | ||
} `json:"os"` | ||
Jvm struct { | ||
Pid int `json:"pid"` | ||
Version string `json:"version"` | ||
VMName string `json:"vm_name"` | ||
VMVersion string `json:"vm_version"` | ||
VMVendor string `json:"vm_vendor"` | ||
StartTimeInMillis int64 `json:"start_time_in_millis"` | ||
Mem struct { | ||
HeapInitInBytes int `json:"heap_init_in_bytes"` | ||
HeapMaxInBytes int `json:"heap_max_in_bytes"` | ||
NonHeapInitInBytes int `json:"non_heap_init_in_bytes"` | ||
NonHeapMaxInBytes int `json:"non_heap_max_in_bytes"` | ||
} `json:"mem"` | ||
GcCollectors []string `json:"gc_collectors"` | ||
} `json:"jvm"` | ||
} | ||
|
||
// NodeInfo function | ||
func NodeInfo(endpoint string) (NodeInfoResponse, error) { | ||
var response NodeInfoResponse | ||
|
||
handler := &HTTPHandler{ | ||
Endpoint: endpoint + "/_node", | ||
} | ||
|
||
err := getMetrics(handler, &response) | ||
|
||
return response, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package collector | ||
|
||
import ( | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/common/log" | ||
"strconv" | ||
) | ||
|
||
// NodeInfoCollector type | ||
type NodeInfoCollector struct { | ||
endpoint string | ||
|
||
NodeInfos *prometheus.Desc | ||
OsInfos *prometheus.Desc | ||
JvmInfos *prometheus.Desc | ||
} | ||
|
||
// NewNodeInfoCollector function | ||
func NewNodeInfoCollector(logstashEndpoint string) (Collector, error) { | ||
const subsystem = "info" | ||
|
||
return &NodeInfoCollector{ | ||
endpoint: logstashEndpoint, | ||
|
||
NodeInfos: prometheus.NewDesc( | ||
prometheus.BuildFQName(Namespace, subsystem, "node"), | ||
"A metric with a constant '1' value labeled by Logstash version.", | ||
[]string{"version"}, | ||
nil, | ||
), | ||
|
||
OsInfos: prometheus.NewDesc( | ||
prometheus.BuildFQName(Namespace, subsystem, "os"), | ||
"A metric with a constant '1' value labeled by name, arch, version and available_processors to the OS running Logstash.", | ||
[]string{"name", "arch", "version", "available_processors"}, | ||
nil, | ||
), | ||
|
||
JvmInfos: prometheus.NewDesc( | ||
prometheus.BuildFQName(Namespace, subsystem, "jvm"), | ||
"A metric with a constant '1' value labeled by name, version and vendor of the JVM running Logstash.", | ||
[]string{"name", "version", "vendor"}, | ||
nil, | ||
), | ||
}, nil | ||
} | ||
|
||
// Collect function implements nodestats_collector collector | ||
func (c *NodeInfoCollector) Collect(ch chan<- prometheus.Metric) error { | ||
if desc, err := c.collect(ch); err != nil { | ||
log.Error("Failed collecting info metrics", desc, err) | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (c *NodeInfoCollector) collect(ch chan<- prometheus.Metric) (*prometheus.Desc, error) { | ||
stats, err := NodeInfo(c.endpoint) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
ch <- prometheus.MustNewConstMetric( | ||
c.NodeInfos, | ||
prometheus.CounterValue, | ||
float64(1), | ||
stats.Version, | ||
) | ||
|
||
ch <- prometheus.MustNewConstMetric( | ||
c.OsInfos, | ||
prometheus.CounterValue, | ||
float64(1), | ||
stats.Os.Name, | ||
stats.Os.Arch, | ||
stats.Os.Version, | ||
strconv.Itoa(stats.Os.AvailableProcessors), | ||
) | ||
|
||
ch <- prometheus.MustNewConstMetric( | ||
c.JvmInfos, | ||
prometheus.CounterValue, | ||
float64(1), | ||
stats.Jvm.VMName, | ||
stats.Jvm.VMVersion, | ||
stats.Jvm.VMVendor, | ||
) | ||
|
||
return nil, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters