Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(API): update Agent to use V4 endpoints #469

Merged
merged 2 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 4 additions & 26 deletions src/main/java/io/cryostat/agent/CryostatClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import jdk.jfr.Recording;
import org.apache.commons.io.FileUtils;
Expand All @@ -73,8 +72,8 @@

public class CryostatClient {

private static final String DISCOVERY_API_PATH = "/api/v2.2/discovery";
private static final String CREDENTIALS_API_PATH = "/api/v2.2/credentials";
private static final String DISCOVERY_API_PATH = "/api/v4/discovery";
private static final String CREDENTIALS_API_PATH = "/api/v4/credentials";

private final Logger log = LoggerFactory.getLogger(getClass());

Expand Down Expand Up @@ -160,18 +159,7 @@ public CompletableFuture<PluginInfo> register(
.thenApply(
res -> {
try (InputStream is = res.getEntity().getContent()) {
return mapper.readValue(is, ObjectNode.class);
} catch (IOException e) {
log.error("Unable to parse response as JSON", e);
throw new RegistrationException(e);
}
})
.thenApply(
node -> {
try {
return mapper.readValue(
node.get("data").get("result").toString(),
PluginInfo.class);
return mapper.readValue(is, PluginInfo.class);
} catch (IOException e) {
log.error("Unable to parse response as JSON", e);
throw new RegistrationException(e);
Expand Down Expand Up @@ -231,18 +219,8 @@ private CompletableFuture<Integer> queryExistingCredentials(URI callback) {
.thenApply(
res -> {
try (InputStream is = res.getEntity().getContent()) {
return mapper.readValue(is, ObjectNode.class);
} catch (IOException e) {
log.error("Unable to parse response as JSON", e);
throw new RegistrationException(e);
}
})
.thenApply(
node -> {
try {
return mapper.readValue(
node.get("data").get("result").toString(),
new TypeReference<List<StoredCredential>>() {});
is, new TypeReference<List<StoredCredential>>() {});
} catch (IOException e) {
log.error("Unable to parse response as JSON", e);
throw new RegistrationException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ public boolean isInsightsEnabled(PluginInfo pluginInfo) {
// Check if the user has opted out
boolean optingOut =
config.getOptionalValue(RHT_INSIGHTS_JAVA_OPT_OUT, boolean.class).orElse(false);
return pluginInfo.getEnv().containsKey(INSIGHTS_SVC) && !optingOut;
return pluginInfo.getEnvAsMap().containsKey(INSIGHTS_SVC) && !optingOut;
}

public void runInsightsAgent(PluginInfo pluginInfo) {
log.info("Starting Red Hat Insights client");
String server = pluginInfo.getEnv().get(INSIGHTS_SVC);
String server = pluginInfo.getEnvAsMap().get(INSIGHTS_SVC);
Objects.requireNonNull(server, "Insights server is missing");
String appName = config.getValue(ConfigModule.CRYOSTAT_AGENT_APP_NAME, String.class);

Expand Down
61 changes: 61 additions & 0 deletions src/main/java/io/cryostat/agent/model/KeyValue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright The Cryostat Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.cryostat.agent.model;

import java.util.Objects;

public class KeyValue {
private final String key;
private final String value;

public KeyValue(String key, String value) {
this.key = Objects.requireNonNull(key);
this.value = value;
}

public String getKey() {
return key;
}

public String getValue() {
return value;
}

@Override
public int hashCode() {
return Objects.hash(key, value);
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
KeyValue other = (KeyValue) obj;
return Objects.equals(key, other.key) && Objects.equals(value, other.value);
}

@Override
public String toString() {
return "KeyValue [key=" + key + ", value=" + value + "]";
}
}
2 changes: 2 additions & 0 deletions src/main/java/io/cryostat/agent/model/MBeanInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ public MBeanInfo() {
getJvmID(getRuntimeMetrics()));
}

protected final void finalize() {}

public MBeanMetrics getMBeanMetrics() {
return mBeanMetrics;
}
Expand Down
38 changes: 30 additions & 8 deletions src/main/java/io/cryostat/agent/model/PluginInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,33 @@
*/
package io.cryostat.agent.model;

import java.util.HashMap;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

public class PluginInfo {

private String id;
private String token;
private Map<String, String> env = new HashMap<>();
private List<KeyValue> env = new ArrayList<>();

public PluginInfo() {}

public PluginInfo(String id, String token, Map<String, String> env) {
public PluginInfo(String id, String token, List<KeyValue> env) {
this.id = id;
this.token = token;
this.env.putAll(env);
this.env.addAll(env);
}

public PluginInfo(String id, String token, Map<String, String> env) {
this(
id,
token,
env.entrySet().stream()
.map(e -> new KeyValue(e.getKey(), e.getValue()))
.collect(Collectors.toList()));
}

public void copyFrom(PluginInfo o) {
Expand All @@ -55,8 +66,12 @@ public String getToken() {
return token;
}

public Map<String, String> getEnv() {
return new HashMap<>(env);
public List<KeyValue> getEnv() {
return new ArrayList<>(env);
}

public Map<String, String> getEnvAsMap() {
return env.stream().collect(Collectors.toMap(KeyValue::getKey, KeyValue::getValue));
}

public void setId(String id) {
Expand All @@ -67,9 +82,16 @@ public void setToken(String token) {
this.token = token;
}

public void setEnv(Map<String, String> env) {
public void setEnv(List<KeyValue> env) {
this.env.clear();
this.env.putAll(env);
this.env.addAll(env);
}

public void setEnvFromMap(Map<String, String> env) {
this.setEnv(
env.entrySet().stream()
.map(e -> new KeyValue(e.getKey(), e.getValue()))
.collect(Collectors.toList()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ void setupEach() {

Map<String, String> env =
Collections.singletonMap("INSIGHTS_SVC", "http://insights-proxy.example.com:8080");
when(pluginInfo.getEnv()).thenReturn(env);
when(pluginInfo.getEnvAsMap()).thenReturn(env);

this.helper = new InsightsAgentHelper(config, instrumentation);
}
Expand All @@ -101,7 +101,7 @@ void testInsightsEnabled() {

@Test
void testInsightsDisabled() {
when(pluginInfo.getEnv()).thenReturn(Collections.emptyMap());
when(pluginInfo.getEnvAsMap()).thenReturn(Collections.emptyMap());
Assertions.assertFalse(helper.isInsightsEnabled(pluginInfo));
}

Expand Down
Loading