Skip to content

Commit 3db66bc

Browse files
committed
feat(id): include instance ID in hash ID, return instance ID via mbean-invoke
1 parent c6ef977 commit 3db66bc

File tree

6 files changed

+93
-9
lines changed

6 files changed

+93
-9
lines changed

src/main/java/io/cryostat/agent/Agent.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import java.io.IOException;
1919
import java.lang.instrument.Instrumentation;
20+
import java.lang.management.ManagementFactory;
2021
import java.net.URI;
2122
import java.net.URISyntaxException;
2223
import java.nio.file.Path;
@@ -36,6 +37,8 @@
3637

3738
import javax.inject.Named;
3839
import javax.inject.Singleton;
40+
import javax.management.MBeanServer;
41+
import javax.management.ObjectName;
3942

4043
import io.cryostat.agent.ConfigModule.URIRange;
4144
import io.cryostat.agent.VersionInfo.Semver;
@@ -73,6 +76,7 @@
7376
+ " JVMs")
7477
public class Agent implements Callable<Integer>, Consumer<AgentArgs> {
7578

79+
private static final String MXBEAN_OBJECT_NAME = "io.cryostat.agent.CryostatAgent:name=agent";
7680
private static final AtomicBoolean needsCleanup = new AtomicBoolean(true);
7781
private static final String ALL_PIDS = "*";
7882
static final String AUTO_ATTACH_PID = "0";
@@ -240,6 +244,10 @@ public void accept(AgentArgs args) {
240244
try {
241245
final Client client = DaggerAgent_Client.builder().build();
242246

247+
CryostatAgentMXBean mxBean = client.agentMXBean();
248+
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
249+
mbs.registerMBean(mxBean, new ObjectName(MXBEAN_OBJECT_NAME));
250+
243251
boolean sample = client.fleetSampleValue() < client.fleetSamplingRatio();
244252
log.trace(
245253
"fleetSampleValue: {} , fleetSampleRatio: {}",
@@ -378,6 +386,8 @@ interface Client {
378386
@Named(ConfigModule.CRYOSTAT_AGENT_BASEURI_RANGE)
379387
URIRange uriRange();
380388

389+
CryostatAgent agentMXBean();
390+
381391
WebServer webServer();
382392

383393
Registration registration();

src/main/java/io/cryostat/agent/ConfigModule.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,13 @@ public static String provideCryostatAgentInstanceId(Config config) {
819819
.orElseGet(() -> UUID.randomUUID().toString());
820820
}
821821

822+
@Provides
823+
@Singleton
824+
public static CryostatAgentMXBean provideCryostatAgentMXBean(
825+
@Named(CRYOSTAT_AGENT_INSTANCE_ID) String id) {
826+
return new CryostatAgent(id);
827+
}
828+
822829
@Provides
823830
@Singleton
824831
@Named(CRYOSTAT_AGENT_APP_NAME)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright The Cryostat Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.cryostat.agent;
17+
18+
import javax.inject.Inject;
19+
import javax.inject.Named;
20+
21+
public class CryostatAgent implements CryostatAgentMXBean {
22+
23+
private final String id;
24+
25+
@Inject
26+
CryostatAgent(@Named(ConfigModule.CRYOSTAT_AGENT_INSTANCE_ID) String id) {
27+
this.id = id;
28+
}
29+
30+
@Override
31+
public String getId() {
32+
return id.toString();
33+
}
34+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright The Cryostat Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.cryostat.agent;
17+
18+
public interface CryostatAgentMXBean {
19+
public String getId();
20+
}

src/main/java/io/cryostat/agent/MainModule.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -825,9 +825,9 @@ public static FlightRecorderHelper provideFlightRecorderHelper() {
825825
@Provides
826826
@Singleton
827827
@Named(JVM_ID)
828-
public static String provideJvmId() {
828+
public static String provideJvmId(@Named(ConfigModule.CRYOSTAT_AGENT_INSTANCE_ID) String id) {
829829
try {
830-
return JvmIdentifier.getLocal().getHash();
830+
return JvmIdentifier.getLocal(id).getHash();
831831
} catch (IDException e) {
832832
throw new RuntimeException(e);
833833
}

src/main/java/io/cryostat/agent/remote/InvokeContext.java

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.InputStream;
2020
import java.io.OutputStream;
2121
import java.lang.management.ManagementFactory;
22+
import java.nio.file.Files;
2223
import java.nio.file.Paths;
2324
import java.util.Objects;
2425
import java.util.UUID;
@@ -45,7 +46,6 @@ class InvokeContext extends MutatingRemoteContext {
4546

4647
private static final String DUMP_THREADS = "threadPrint";
4748
private static final String DUMP_THREADS_TO_FIlE = "threadDumpToFile";
48-
private static final String DIAGNOSTIC_BEAN_NAME = "com.sun.management:type=DiagnosticCommand";
4949
private final Logger log = LoggerFactory.getLogger(getClass());
5050
private final ObjectMapper mapper;
5151

@@ -74,7 +74,12 @@ public void handle(HttpExchange exchange) throws IOException {
7474
mapper.readValue(body, MBeanInvocationRequest.class);
7575
String requestId = "";
7676
String filename =
77-
config.getValue(ConfigModule.CRYOSTAT_AGENT_APP_NAME, String.class);
77+
Files.createTempFile(
78+
config.getValue(
79+
ConfigModule.CRYOSTAT_AGENT_APP_NAME,
80+
String.class),
81+
(String) null)
82+
.toString();
7883
if (!req.isValid()) {
7984
exchange.sendResponseHeaders(
8085
HttpStatus.SC_BAD_REQUEST, BODY_LENGTH_NONE);
@@ -138,16 +143,24 @@ static class MBeanInvocationRequest<T> {
138143
public Object[] parameters;
139144
public String[] signature;
140145

146+
private static final String CRYOSTAT_AGENT_BEAN_NAME =
147+
"io.cryostat.agent.CryostatAgent:name=agent";
141148
private static final String HOTSPOT_DIAGNOSTIC_BEAN_NAME =
142149
"com.sun.management:type=HotSpotDiagnostic";
150+
private static final String DIAGNOSTIC_COMMAND_BEAN_NAME =
151+
"com.sun.management:type=DiagnosticCommand";
143152

144153
public boolean isValid() {
145-
if (this.beanName.equals(ManagementFactory.MEMORY_MXBEAN_NAME)
146-
|| this.beanName.equals(HOTSPOT_DIAGNOSTIC_BEAN_NAME)) {
154+
if (CRYOSTAT_AGENT_BEAN_NAME.equals(beanName)) {
147155
return true;
148-
} else if (this.beanName.equals(DIAGNOSTIC_BEAN_NAME)
149-
&& (this.operation.equals(DUMP_THREADS)
150-
|| this.operation.equals(DUMP_THREADS_TO_FIlE))) {
156+
}
157+
if (ManagementFactory.MEMORY_MXBEAN_NAME.equals(beanName)
158+
|| HOTSPOT_DIAGNOSTIC_BEAN_NAME.equals(beanName)) {
159+
return true;
160+
}
161+
if (DIAGNOSTIC_COMMAND_BEAN_NAME.equals(beanName)
162+
&& (DUMP_THREADS.equals(this.operation)
163+
|| DUMP_THREADS_TO_FIlE.equals(this.operation))) {
151164
return true;
152165
}
153166
return false;

0 commit comments

Comments
 (0)