Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
lhpqaq committed Jan 4, 2025
1 parent 01ce16f commit f4190d0
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,12 @@ private Map<String, List<String>> getClusterHosts() {
Map<String, List<String>> clusterHosts = new HashMap<>();
List<HostPO> hostPOs = hostDao.findAll();
for (HostPO hostPO : hostPOs) {
String clusterName = hostPO.getClusterName();
Long clusterId = hostPO.getClusterId();
if (clusterId == null) {
continue;
}
String host = hostPO.getIpv4();
String clusterName = clusterDao.findById(clusterId).getName();
if (clusterHosts.containsKey(clusterName)) {
clusterHosts.get(clusterName).add(host);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ protected CommandRequest getCommandRequest() {
if (stackDTO.getStackName().equals("infra")) {
commandPayload.setClusterHosts((Map<String, List<String>>) properties.get("clusterHosts"));
}

CommandRequest.Builder builder = CommandRequest.newBuilder();
builder.setType(CommandType.COMPONENT);
builder.setPayload(JsonUtils.writeAsString(commandPayload));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Slf4j
@NoArgsConstructor
Expand Down Expand Up @@ -79,9 +81,7 @@ public String stackHome() {
}

public Map<String, List<String>> getClusterHosts() {
if (clusterHosts == null) {
log.error("Cluster hosts is null, please check the command payload");
}
return clusterHosts;
// In Component Status stage, clusterHosts is null
return Objects.requireNonNullElseGet(clusterHosts, HashMap::new);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public class PrometheusParams extends InfraParams {

public PrometheusParams(CommandPayload commandPayload) {
super(commandPayload);
setAgentScrapeJob();
scrapeJobs = new ArrayList<>();
scrapeJobs.add(prometheusScrapeJob);
scrapeJobs.add(agentScrapeJob);
Expand All @@ -76,15 +77,6 @@ public String getServiceName() {
return "prometheus";
}

protected List<String> getAllHost() {
List<String> ips = LocalSettings.hosts().get("all");
List<String> hosts = new ArrayList<>();
for (String ip : ips) {
hosts.add(MessageFormat.format("{0}:{1}", ip, BM_AGENT_PORT));
}
return hosts;
}

@GlobalParams
public Map<String, Object> prometheusJob() {
Map<String, Object> configuration = LocalSettings.configurations(getServiceName(), "prometheus");
Expand All @@ -101,26 +93,6 @@ public Map<String, Object> prometheusJob() {
return configuration;
}

@GlobalParams
public Map<String, Object> agentJob() {
Map<String, Object> job = new HashMap<>();
job.put("name", BM_AGENT_JOB_NAME);
job.put("targets_file", targetsConfigFile(BM_AGENT_JOB_NAME));
job.put("metrics_path", "/actuator/prometheus");

List<Map<String, Object>> agentTargets = new ArrayList<>();
getClusterHosts().forEach((cluster, hosts) -> {
Map<String, Object> agentTarget = new HashMap<>();
agentTarget.put("targets", hosts);
agentTarget.put("labels", Map.of(AGENT_TARGET_LABEL, cluster));
agentTargets.add(agentTarget);
});
job.put("targets_list", agentTargets);

agentScrapeJob = job;
return LocalSettings.configurations(getServiceName(), "prometheus");
}

@GlobalParams
public Map<String, Object> configs() {
Map<String, Object> configuration = LocalSettings.configurations(getServiceName(), "prometheus");
Expand All @@ -141,4 +113,24 @@ public Map<String, Object> rules() {
public String listenAddress() {
return MessageFormat.format("0.0.0.0:{0}", prometheusPort);
}

public void setAgentScrapeJob() {
agentScrapeJob = new HashMap<>();
agentScrapeJob.put("name", BM_AGENT_JOB_NAME);
agentScrapeJob.put("targets_file", targetsConfigFile(BM_AGENT_JOB_NAME));
agentScrapeJob.put("metrics_path", "/actuator/prometheus");

List<Map<String, Object>> agentTargets = new ArrayList<>();
getClusterHosts().forEach((cluster, hosts) -> {
Map<String, Object> agentTarget = new HashMap<>();
List<String> targets = new ArrayList<>();
for (String host : hosts) {
targets.add(MessageFormat.format("{0}:{1}", host, BM_AGENT_PORT));
}
agentTarget.put("targets", targets);
agentTarget.put("labels", Map.of(AGENT_TARGET_LABEL, cluster));
agentTargets.add(agentTarget);
});
agentScrapeJob.put("targets_list", agentTargets);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class PrometheusSetup {

// private static final String AGENT_TARGET_LABEL = "cluster";

// @SuppressWarnings("unchecked")
public static ShellResult config(Params params) {
PrometheusParams prometheusParams = (PrometheusParams) params;
String user = prometheusParams.user();
Expand All @@ -63,33 +60,6 @@ public static ShellResult config(Params params) {
Constants.PERMISSION_644,
prometheusParams.getGlobalParamsMap());

// Map<String, List<String>> prometheusTargets = new HashMap<>();
// prometheusTargets.put("targets", (List<String>)
// prometheusParams.getPrometheusScrapeJob().get("targets_list"));
// LinuxFileUtils.toFile(
// ConfigType.JSON,
// (String) prometheusParams.getPrometheusScrapeJob().get("targets_file"),
// user,
// group,
// Constants.PERMISSION_644,
// List.of(prometheusTargets));
//
// List<Map<String, Object>> agentTargets = new ArrayList<>();
// prometheusParams.getClusterHosts().forEach((cluster, hosts) -> {
// Map<String, Object> agentTarget = new HashMap<>();
// agentTarget.put("targets", hosts);
// agentTarget.put("labels", Map.of(AGENT_TARGET_LABEL, cluster));
// agentTargets.add(agentTarget);
// });
//
// LinuxFileUtils.toFile(
// ConfigType.JSON,
// (String) prometheusParams.getAgentScrapeJob().get("targets_file"),
// user,
// group,
// Constants.PERMISSION_644,
// agentTargets);

for (int i = 0; i < prometheusParams.getScrapeJobs().size(); i++) {
Map<String, Object> job = prometheusParams.getScrapeJobs().get(i);
LinuxFileUtils.toFile(
Expand Down

0 comments on commit f4190d0

Please sign in to comment.