Skip to content

Commit

Permalink
Merge pull request #1238 from mackerelio/add-prefix-option
Browse files Browse the repository at this point in the history
[jvm] add prefix option
  • Loading branch information
masarasi authored Dec 26, 2024
2 parents 2ea1ebf + f8cd5f4 commit bcc837f
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 19 deletions.
6 changes: 3 additions & 3 deletions mackerel-plugin-jvm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ JVM(jstat) custom metrics plugin for mackerel.io agent.
## Synopsis

```shell
mackerel-plugin-jvm -javaname=<javaname> [-pidfile=</path/to/pidfile>] [-jstatpath=</path/to/jstat] [-jpspath=/path/to/jps] [-jinfopath=/path/to/jinfo] [-remote=<host:port>]
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>]
```

## Requirements
Expand Down Expand Up @@ -38,8 +38,7 @@ You can check javaname by jps command.
14822 Jps
```

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

## User to execute this plugin

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

## References

- [Metric plugins - mackerel-plugin-jvm - Mackerel Docs](https://mackerel.io/docs/entry/plugins/mackerel-plugin-jvm)
- https://github.com/sensu/sensu-community-plugins/blob/master/plugins/java/jstat-metrics.py
- http://docs.oracle.com/javase/7/docs/technotes/tools/share/jstat.html
- https://github.com/kazeburo/jstat2gf
47 changes: 31 additions & 16 deletions mackerel-plugin-jvm/lib/jvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ var logger = logging.GetLogger("metrics.plugin.jvm")

// JVMPlugin plugin for JVM
type JVMPlugin struct {
Remote string
Lvmid string
JstatPath string
JinfoPath string
JavaName string
Tempfile string
Remote string
Lvmid string
JstatPath string
JinfoPath string
JavaName string
Tempfile string
MetricKey string
MetricLabel string
}

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

// GraphDefinition interface for mackerelplugin
func (m JVMPlugin) GraphDefinition() map[string]mp.Graphs {
rawJavaName := m.JavaName
lowerJavaName := strings.ToLower(m.JavaName)
metricLabel := m.MetricLabel
if metricLabel == "" {
metricLabel = m.JavaName
}

javaName := m.MetricKey
if javaName == "" {
javaName = m.JavaName
}
lowerJavaName := strings.ToLower(javaName)

return map[string]mp.Graphs{
fmt.Sprintf("jvm.%s.gc_events", lowerJavaName): {
Label: fmt.Sprintf("JVM %s GC events", rawJavaName),
Label: fmt.Sprintf("JVM %s GC events", metricLabel),
Unit: "integer",
Metrics: []mp.Metrics{
{Name: "YGC", Label: "Young GC event", Diff: true},
Expand All @@ -255,7 +266,7 @@ func (m JVMPlugin) GraphDefinition() map[string]mp.Graphs {
},
},
fmt.Sprintf("jvm.%s.gc_time", lowerJavaName): {
Label: fmt.Sprintf("JVM %s GC time (sec)", rawJavaName),
Label: fmt.Sprintf("JVM %s GC time (sec)", metricLabel),
Unit: "float",
Metrics: []mp.Metrics{
{Name: "YGCT", Label: "Young GC time", Diff: true},
Expand All @@ -264,7 +275,7 @@ func (m JVMPlugin) GraphDefinition() map[string]mp.Graphs {
},
},
fmt.Sprintf("jvm.%s.gc_time_percentage", lowerJavaName): {
Label: fmt.Sprintf("JVM %s GC time percentage", rawJavaName),
Label: fmt.Sprintf("JVM %s GC time percentage", metricLabel),
Unit: "percentage",
Metrics: []mp.Metrics{
// gc_time_percentage is the percentage of gc time to 60 sec.
Expand All @@ -274,7 +285,7 @@ func (m JVMPlugin) GraphDefinition() map[string]mp.Graphs {
},
},
fmt.Sprintf("jvm.%s.new_space", lowerJavaName): {
Label: fmt.Sprintf("JVM %s New Space memory", rawJavaName),
Label: fmt.Sprintf("JVM %s New Space memory", metricLabel),
Unit: "float",
Metrics: []mp.Metrics{
{Name: "NGCMX", Label: "New max", Diff: false, Scale: 1024},
Expand All @@ -285,7 +296,7 @@ func (m JVMPlugin) GraphDefinition() map[string]mp.Graphs {
},
},
fmt.Sprintf("jvm.%s.old_space", lowerJavaName): {
Label: fmt.Sprintf("JVM %s Old Space memory", rawJavaName),
Label: fmt.Sprintf("JVM %s Old Space memory", metricLabel),
Unit: "float",
Metrics: []mp.Metrics{
{Name: "OGCMX", Label: "Old max", Diff: false, Scale: 1024},
Expand All @@ -294,7 +305,7 @@ func (m JVMPlugin) GraphDefinition() map[string]mp.Graphs {
},
},
fmt.Sprintf("jvm.%s.perm_space", lowerJavaName): {
Label: fmt.Sprintf("JVM %s Permanent Space", rawJavaName),
Label: fmt.Sprintf("JVM %s Permanent Space", metricLabel),
Unit: "float",
Metrics: []mp.Metrics{
{Name: "PGCMX", Label: "Perm max", Diff: false, Scale: 1024},
Expand All @@ -303,7 +314,7 @@ func (m JVMPlugin) GraphDefinition() map[string]mp.Graphs {
},
},
fmt.Sprintf("jvm.%s.metaspace", lowerJavaName): {
Label: fmt.Sprintf("JVM %s Metaspace", rawJavaName),
Label: fmt.Sprintf("JVM %s Metaspace", metricLabel),
Unit: "float",
Metrics: []mp.Metrics{
{Name: "MCMX", Label: "Metaspace capacity max", Diff: false, Scale: 1024},
Expand All @@ -315,7 +326,7 @@ func (m JVMPlugin) GraphDefinition() map[string]mp.Graphs {
},
},
fmt.Sprintf("jvm.%s.memorySpace", lowerJavaName): {
Label: fmt.Sprintf("JVM %s MemorySpace", rawJavaName),
Label: fmt.Sprintf("JVM %s MemorySpace", metricLabel),
Unit: "float",
Metrics: []mp.Metrics{
{Name: "oldSpaceRate", Label: "GC Old Memory Space", Diff: false},
Expand Down Expand Up @@ -373,6 +384,8 @@ func Do() {
optJavaName := flag.String("javaname", "", "Java app name")
optPidFile := flag.String("pidfile", "", "pidfile path")
optTempfile := flag.String("tempfile", "", "Temp file name")
optMetricKey := flag.String("metric-key", "", "Specifying the Name field in the Graph Definition")
optMetricLabel := flag.String("metric-label", "", "Specifying the Label field in the Graph Definition")
flag.Parse()

var jvm JVMPlugin
Expand Down Expand Up @@ -409,6 +422,8 @@ func Do() {
}

jvm.JavaName = *optJavaName
jvm.MetricKey = *optMetricKey
jvm.MetricLabel = *optMetricLabel

helper := mp.NewMackerelPlugin(jvm)
helper.Tempfile = *optTempfile
Expand Down
53 changes: 53 additions & 0 deletions mackerel-plugin-jvm/lib/jvm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,56 @@ func TestFetchMetrics(t *testing.T) {
t.Errorf("fetchMetrics('-gc') = %v; want %v", actual, expected)
}
}

func TestGraphDefinition(t *testing.T) {
tests := []struct {
plugin JVMPlugin
expectedKey string
expectedLabel string
}{
{
plugin: JVMPlugin{
JavaName: "Tomcat",
},
expectedKey: "jvm.tomcat.gc_events",
expectedLabel: "JVM Tomcat GC events",
},
{
plugin: JVMPlugin{
JavaName: "Tomcat",
MetricKey: "foo.bar",
MetricLabel: "Hoge Fuga",
},
expectedKey: "jvm.foo.bar.gc_events",
expectedLabel: "JVM Hoge Fuga GC events",
},
{
plugin: JVMPlugin{
JavaName: "Tomcat",
MetricLabel: "Hoge Fuga",
},
expectedKey: "jvm.tomcat.gc_events",
expectedLabel: "JVM Hoge Fuga GC events",
},
{
plugin: JVMPlugin{
JavaName: "Tomcat",
MetricKey: "foo.bar",
},
expectedKey: "jvm.foo.bar.gc_events",
expectedLabel: "JVM Tomcat GC events",
},
}

for _, tt := range tests {
graphDefs := tt.plugin.GraphDefinition()

metric, ok := graphDefs[tt.expectedKey]
if !ok {
t.Errorf("expected to have key %s but not found", tt.expectedKey)
}
if metric.Label != tt.expectedLabel {
t.Errorf("expected to have label %s but got: %s", tt.expectedLabel, metric.Label)
}
}
}

0 comments on commit bcc837f

Please sign in to comment.