forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jmx.go
106 lines (87 loc) · 2.52 KB
/
jmx.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package jmx
import (
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/common/cfgwarn"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/metricbeat/helper"
"github.com/elastic/beats/metricbeat/mb"
"github.com/elastic/beats/metricbeat/mb/parse"
)
var (
metricsetName = "jolokia.jmx"
debugf = logp.MakeDebug(metricsetName)
)
// init registers the MetricSet with the central registry.
func init() {
if err := mb.Registry.AddMetricSet("jolokia", "jmx", New, hostParser); err != nil {
panic(err)
}
}
const (
// defaultScheme is the default scheme to use when it is not specified in
// the host config.
defaultScheme = "http"
// defaultPath is the default path to the ngx_http_stub_status_module endpoint on Nginx.
defaultPath = "/jolokia/?ignoreErrors=true&canonicalNaming=false"
)
var (
hostParser = parse.URLHostParserBuilder{
DefaultScheme: defaultScheme,
PathConfigKey: "path",
DefaultPath: defaultPath,
}.Build()
)
// MetricSet type defines all fields of the MetricSet
type MetricSet struct {
mb.BaseMetricSet
mapping map[string]string
namespace string
http *helper.HTTP
}
// New create a new instance of the MetricSet
func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
cfgwarn.Beta("The jolokia jmx metricset is beta")
// Additional configuration options
config := struct {
Namespace string `config:"namespace" validate:"required"`
Mappings []JMXMapping `config:"jmx.mappings" validate:"required"`
}{}
if err := base.Module().UnpackConfig(&config); err != nil {
return nil, err
}
body, mapping, err := buildRequestBodyAndMapping(config.Mappings)
if err != nil {
return nil, err
}
http := helper.NewHTTP(base)
http.SetMethod("POST")
http.SetBody(body)
if logp.IsDebug(metricsetName) {
debugf("The body for POST requests to jolokia host %v is: %v",
base.HostData().Host, string(body))
}
return &MetricSet{
BaseMetricSet: base,
mapping: mapping,
namespace: config.Namespace,
http: http,
}, nil
}
// Fetch methods implements the data gathering and data conversion to the right format
func (m *MetricSet) Fetch() (common.MapStr, error) {
body, err := m.http.FetchContent()
if err != nil {
return nil, err
}
if logp.IsDebug(metricsetName) {
debugf("The response body from jolokia host %v is: %v",
m.HostData().Host, string(body))
}
event, err := eventMapping(body, m.mapping)
if err != nil {
return nil, err
}
// Set dynamic namespace.
event[mb.NamespaceKey] = m.namespace
return event, nil
}