Skip to content

Commit bcc837f

Browse files
authored
Merge pull request #1238 from mackerelio/add-prefix-option
[jvm] add prefix option
2 parents 2ea1ebf + f8cd5f4 commit bcc837f

File tree

3 files changed

+87
-19
lines changed

3 files changed

+87
-19
lines changed

mackerel-plugin-jvm/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ JVM(jstat) custom metrics plugin for mackerel.io agent.
66
## Synopsis
77

88
```shell
9-
mackerel-plugin-jvm -javaname=<javaname> [-pidfile=</path/to/pidfile>] [-jstatpath=</path/to/jstat] [-jpspath=/path/to/jps] [-jinfopath=/path/to/jinfo] [-remote=<host:port>]
9+
mackerel-plugin-jvm -javaname=<javaname> [-pidfile=</path/to/pidfile>] [-jstatpath=</path/to/jstat] [-jpspath=/path/to/jps] [-jinfopath=/path/to/jinfo] [-remote=<host:port>] [-metric-key=<metric key>] [-metric-label=<metric label>]
1010
```
1111

1212
## Requirements
@@ -38,8 +38,7 @@ You can check javaname by jps command.
3838
14822 Jps
3939
```
4040

41-
Please choose an arbitrary name as `javaname` when you use `pidfile` option.
42-
It is just used as a prefix of graph label.
41+
Please choose an arbitrary name as `javaname` when you use `pidfile` option. It is used as a mertric name and graph label.
4342

4443
## User to execute this plugin
4544

@@ -53,6 +52,7 @@ When the JVM option is enabled, this plugin is no longer able to work because wh
5352

5453
## References
5554

55+
- [Metric plugins - mackerel-plugin-jvm - Mackerel Docs](https://mackerel.io/docs/entry/plugins/mackerel-plugin-jvm)
5656
- https://github.com/sensu/sensu-community-plugins/blob/master/plugins/java/jstat-metrics.py
5757
- http://docs.oracle.com/javase/7/docs/technotes/tools/share/jstat.html
5858
- https://github.com/kazeburo/jstat2gf

mackerel-plugin-jvm/lib/jvm.go

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ var logger = logging.GetLogger("metrics.plugin.jvm")
1818

1919
// JVMPlugin plugin for JVM
2020
type JVMPlugin struct {
21-
Remote string
22-
Lvmid string
23-
JstatPath string
24-
JinfoPath string
25-
JavaName string
26-
Tempfile string
21+
Remote string
22+
Lvmid string
23+
JstatPath string
24+
JinfoPath string
25+
JavaName string
26+
Tempfile string
27+
MetricKey string
28+
MetricLabel string
2729
}
2830

2931
// # jps
@@ -242,11 +244,20 @@ func (m JVMPlugin) FetchMetrics() (map[string]interface{}, error) {
242244

243245
// GraphDefinition interface for mackerelplugin
244246
func (m JVMPlugin) GraphDefinition() map[string]mp.Graphs {
245-
rawJavaName := m.JavaName
246-
lowerJavaName := strings.ToLower(m.JavaName)
247+
metricLabel := m.MetricLabel
248+
if metricLabel == "" {
249+
metricLabel = m.JavaName
250+
}
251+
252+
javaName := m.MetricKey
253+
if javaName == "" {
254+
javaName = m.JavaName
255+
}
256+
lowerJavaName := strings.ToLower(javaName)
257+
247258
return map[string]mp.Graphs{
248259
fmt.Sprintf("jvm.%s.gc_events", lowerJavaName): {
249-
Label: fmt.Sprintf("JVM %s GC events", rawJavaName),
260+
Label: fmt.Sprintf("JVM %s GC events", metricLabel),
250261
Unit: "integer",
251262
Metrics: []mp.Metrics{
252263
{Name: "YGC", Label: "Young GC event", Diff: true},
@@ -255,7 +266,7 @@ func (m JVMPlugin) GraphDefinition() map[string]mp.Graphs {
255266
},
256267
},
257268
fmt.Sprintf("jvm.%s.gc_time", lowerJavaName): {
258-
Label: fmt.Sprintf("JVM %s GC time (sec)", rawJavaName),
269+
Label: fmt.Sprintf("JVM %s GC time (sec)", metricLabel),
259270
Unit: "float",
260271
Metrics: []mp.Metrics{
261272
{Name: "YGCT", Label: "Young GC time", Diff: true},
@@ -264,7 +275,7 @@ func (m JVMPlugin) GraphDefinition() map[string]mp.Graphs {
264275
},
265276
},
266277
fmt.Sprintf("jvm.%s.gc_time_percentage", lowerJavaName): {
267-
Label: fmt.Sprintf("JVM %s GC time percentage", rawJavaName),
278+
Label: fmt.Sprintf("JVM %s GC time percentage", metricLabel),
268279
Unit: "percentage",
269280
Metrics: []mp.Metrics{
270281
// gc_time_percentage is the percentage of gc time to 60 sec.
@@ -274,7 +285,7 @@ func (m JVMPlugin) GraphDefinition() map[string]mp.Graphs {
274285
},
275286
},
276287
fmt.Sprintf("jvm.%s.new_space", lowerJavaName): {
277-
Label: fmt.Sprintf("JVM %s New Space memory", rawJavaName),
288+
Label: fmt.Sprintf("JVM %s New Space memory", metricLabel),
278289
Unit: "float",
279290
Metrics: []mp.Metrics{
280291
{Name: "NGCMX", Label: "New max", Diff: false, Scale: 1024},
@@ -285,7 +296,7 @@ func (m JVMPlugin) GraphDefinition() map[string]mp.Graphs {
285296
},
286297
},
287298
fmt.Sprintf("jvm.%s.old_space", lowerJavaName): {
288-
Label: fmt.Sprintf("JVM %s Old Space memory", rawJavaName),
299+
Label: fmt.Sprintf("JVM %s Old Space memory", metricLabel),
289300
Unit: "float",
290301
Metrics: []mp.Metrics{
291302
{Name: "OGCMX", Label: "Old max", Diff: false, Scale: 1024},
@@ -294,7 +305,7 @@ func (m JVMPlugin) GraphDefinition() map[string]mp.Graphs {
294305
},
295306
},
296307
fmt.Sprintf("jvm.%s.perm_space", lowerJavaName): {
297-
Label: fmt.Sprintf("JVM %s Permanent Space", rawJavaName),
308+
Label: fmt.Sprintf("JVM %s Permanent Space", metricLabel),
298309
Unit: "float",
299310
Metrics: []mp.Metrics{
300311
{Name: "PGCMX", Label: "Perm max", Diff: false, Scale: 1024},
@@ -303,7 +314,7 @@ func (m JVMPlugin) GraphDefinition() map[string]mp.Graphs {
303314
},
304315
},
305316
fmt.Sprintf("jvm.%s.metaspace", lowerJavaName): {
306-
Label: fmt.Sprintf("JVM %s Metaspace", rawJavaName),
317+
Label: fmt.Sprintf("JVM %s Metaspace", metricLabel),
307318
Unit: "float",
308319
Metrics: []mp.Metrics{
309320
{Name: "MCMX", Label: "Metaspace capacity max", Diff: false, Scale: 1024},
@@ -315,7 +326,7 @@ func (m JVMPlugin) GraphDefinition() map[string]mp.Graphs {
315326
},
316327
},
317328
fmt.Sprintf("jvm.%s.memorySpace", lowerJavaName): {
318-
Label: fmt.Sprintf("JVM %s MemorySpace", rawJavaName),
329+
Label: fmt.Sprintf("JVM %s MemorySpace", metricLabel),
319330
Unit: "float",
320331
Metrics: []mp.Metrics{
321332
{Name: "oldSpaceRate", Label: "GC Old Memory Space", Diff: false},
@@ -373,6 +384,8 @@ func Do() {
373384
optJavaName := flag.String("javaname", "", "Java app name")
374385
optPidFile := flag.String("pidfile", "", "pidfile path")
375386
optTempfile := flag.String("tempfile", "", "Temp file name")
387+
optMetricKey := flag.String("metric-key", "", "Specifying the Name field in the Graph Definition")
388+
optMetricLabel := flag.String("metric-label", "", "Specifying the Label field in the Graph Definition")
376389
flag.Parse()
377390

378391
var jvm JVMPlugin
@@ -409,6 +422,8 @@ func Do() {
409422
}
410423

411424
jvm.JavaName = *optJavaName
425+
jvm.MetricKey = *optMetricKey
426+
jvm.MetricLabel = *optMetricLabel
412427

413428
helper := mp.NewMackerelPlugin(jvm)
414429
helper.Tempfile = *optTempfile

mackerel-plugin-jvm/lib/jvm_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,56 @@ func TestFetchMetrics(t *testing.T) {
107107
t.Errorf("fetchMetrics('-gc') = %v; want %v", actual, expected)
108108
}
109109
}
110+
111+
func TestGraphDefinition(t *testing.T) {
112+
tests := []struct {
113+
plugin JVMPlugin
114+
expectedKey string
115+
expectedLabel string
116+
}{
117+
{
118+
plugin: JVMPlugin{
119+
JavaName: "Tomcat",
120+
},
121+
expectedKey: "jvm.tomcat.gc_events",
122+
expectedLabel: "JVM Tomcat GC events",
123+
},
124+
{
125+
plugin: JVMPlugin{
126+
JavaName: "Tomcat",
127+
MetricKey: "foo.bar",
128+
MetricLabel: "Hoge Fuga",
129+
},
130+
expectedKey: "jvm.foo.bar.gc_events",
131+
expectedLabel: "JVM Hoge Fuga GC events",
132+
},
133+
{
134+
plugin: JVMPlugin{
135+
JavaName: "Tomcat",
136+
MetricLabel: "Hoge Fuga",
137+
},
138+
expectedKey: "jvm.tomcat.gc_events",
139+
expectedLabel: "JVM Hoge Fuga GC events",
140+
},
141+
{
142+
plugin: JVMPlugin{
143+
JavaName: "Tomcat",
144+
MetricKey: "foo.bar",
145+
},
146+
expectedKey: "jvm.foo.bar.gc_events",
147+
expectedLabel: "JVM Tomcat GC events",
148+
},
149+
}
150+
151+
for _, tt := range tests {
152+
graphDefs := tt.plugin.GraphDefinition()
153+
154+
metric, ok := graphDefs[tt.expectedKey]
155+
if !ok {
156+
t.Errorf("expected to have key %s but not found", tt.expectedKey)
157+
}
158+
if metric.Label != tt.expectedLabel {
159+
t.Errorf("expected to have label %s but got: %s", tt.expectedLabel, metric.Label)
160+
}
161+
}
162+
}

0 commit comments

Comments
 (0)