0
# JVM Attributes
1
2
JVM runtime information including name, vendor details, and uptime tracking, providing essential system identification and runtime metrics.
3
4
## Core Imports
5
6
```java
7
import com.codahale.metrics.jvm.JvmAttributeGaugeSet;
8
import com.codahale.metrics.MetricRegistry;
9
import java.lang.management.RuntimeMXBean;
10
```
11
12
## Capabilities
13
14
### JvmAttributeGaugeSet
15
16
Provides key JVM runtime attributes for system identification and uptime monitoring.
17
18
```java { .api }
19
/**
20
* A set of gauges for the JVM name, vendor, and uptime.
21
*/
22
public class JvmAttributeGaugeSet implements MetricSet {
23
/**
24
* Creates a new set of JVM attribute gauges using the default RuntimeMXBean.
25
*/
26
public JvmAttributeGaugeSet();
27
28
/**
29
* Creates a new set of JVM attribute gauges with a custom RuntimeMXBean.
30
* @param runtime JVM management interface with access to system properties
31
*/
32
public JvmAttributeGaugeSet(RuntimeMXBean runtime);
33
34
/**
35
* Returns all JVM attribute metrics.
36
* @return map of metric names to Metric instances
37
*/
38
public Map<String, Metric> getMetrics();
39
}
40
```
41
42
**Metrics Provided:**
43
44
- `name` - JVM name and process identifier (e.g., "12345@hostname")
45
- `vendor` - Formatted vendor information including VM vendor, name, version, and specification version
46
- `uptime` - JVM uptime in milliseconds since start
47
48
**Usage Examples:**
49
50
```java
51
import com.codahale.metrics.MetricRegistry;
52
import com.codahale.metrics.jvm.JvmAttributeGaugeSet;
53
import java.lang.management.ManagementFactory;
54
55
// Basic usage with default RuntimeMXBean
56
MetricRegistry registry = new MetricRegistry();
57
registry.registerAll(new JvmAttributeGaugeSet());
58
59
// Access JVM attributes
60
Gauge<String> jvmName = registry.getGauges().get("name");
61
Gauge<String> jvmVendor = registry.getGauges().get("vendor");
62
Gauge<Long> jvmUptime = registry.getGauges().get("uptime");
63
64
System.out.println("JVM Name: " + jvmName.getValue());
65
System.out.println("JVM Vendor: " + jvmVendor.getValue());
66
System.out.println("JVM Uptime: " + jvmUptime.getValue() + " ms");
67
68
// Convert uptime to more readable format
69
long uptimeMs = jvmUptime.getValue();
70
long uptimeSeconds = uptimeMs / 1000;
71
long uptimeMinutes = uptimeSeconds / 60;
72
long uptimeHours = uptimeMinutes / 60;
73
long uptimeDays = uptimeHours / 24;
74
75
System.out.printf("JVM Uptime: %d days, %d hours, %d minutes, %d seconds%n",
76
uptimeDays,
77
uptimeHours % 24,
78
uptimeMinutes % 60,
79
uptimeSeconds % 60);
80
81
// Custom RuntimeMXBean usage
82
RuntimeMXBean customRuntimeBean = ManagementFactory.getRuntimeMXBean();
83
registry.registerAll(new JvmAttributeGaugeSet(customRuntimeBean));
84
85
// Extract process ID from JVM name
86
String jvmNameValue = jvmName.getValue();
87
String processId = jvmNameValue.split("@")[0];
88
String hostname = jvmNameValue.split("@")[1];
89
System.out.println("Process ID: " + processId);
90
System.out.println("Hostname: " + hostname);
91
```
92
93
**Vendor Information Format:**
94
95
The vendor metric combines multiple runtime properties in a formatted string:
96
```
97
{VM Vendor} {VM Name} {VM Version} ({Specification Version})
98
```
99
100
Example vendor strings:
101
- `"Eclipse Adoptium OpenJDK 64-Bit Server VM 11.0.16+8 (11)"`
102
- `"Oracle Corporation Java HotSpot(TM) 64-Bit Server VM 1.8.0_301-b09 (1.8)"`
103
- `"Azul Systems, Inc. OpenJDK 64-Bit Server VM 17.0.4+8-LTS (17)"`
104
105
**Monitoring Applications:**
106
107
- **System Identification**: Identify JVM version and vendor in multi-environment deployments
108
- **Uptime Tracking**: Monitor application availability and restart frequency
109
- **Process Monitoring**: Extract process IDs for system-level monitoring integration
110
- **Environment Validation**: Ensure applications run on expected JVM versions
111
- **Performance Correlation**: Correlate performance metrics with JVM uptime patterns
112
113
**Integration Examples:**
114
115
```java
116
// Health check integration
117
public class JvmHealthCheck extends HealthCheck {
118
private final Gauge<Long> uptimeGauge;
119
120
public JvmHealthCheck(MetricRegistry registry) {
121
this.uptimeGauge = registry.getGauges().get("uptime");
122
}
123
124
@Override
125
protected Result check() {
126
long uptime = uptimeGauge.getValue();
127
if (uptime < 60000) { // Less than 1 minute
128
return Result.unhealthy("JVM recently restarted");
129
}
130
return Result.healthy("JVM uptime: " + uptime + "ms");
131
}
132
}
133
134
// Logging integration
135
Logger logger = LoggerFactory.getLogger(Application.class);
136
logger.info("Starting application on {}", jvmVendor.getValue());
137
logger.info("JVM process: {}", jvmName.getValue());
138
```