Skip to content

Commit

Permalink
refactor: openmetrics supports relabelconfigs (#584)
Browse files Browse the repository at this point in the history
  • Loading branch information
xzchaoo authored Aug 3, 2023
1 parent f928ead commit 5f796da
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
*/
package io.holoinsight.server.home.dal.model.dto;

import lombok.Data;

import java.util.Date;
import java.util.List;

import io.holoinsight.server.registry.model.OpenmetricsScraperTask;
import lombok.Data;

@Data
public class OpenmetricsScraperDTO {
Expand All @@ -19,7 +21,8 @@ public class OpenmetricsScraperDTO {
private String port;
private String scrapeInterval;
private String scrapeTimeout;

private List<OpenmetricsScraperTask.RelabelConfig> relabelConfigs;
private List<OpenmetricsScraperTask.RelabelConfig> metricRelabelConfigs;
private CloudMonitorRange collectRanges;


Expand All @@ -42,5 +45,4 @@ public class OpenmetricsScraperDTO {
* 修改者
*/
private String modifier;

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ private void upsert(OpenmetricsScraperDTO openmetricsScraperDTO) {
task.setScrapeInterval(openmetricsScraperDTO.getScrapeInterval());
task.setScrapeTimeout(openmetricsScraperDTO.getScrapeTimeout());
task.setScrapePort(openmetricsScraperDTO.getPort());
task.setRelabelConfigs(openmetricsScraperDTO.getRelabelConfigs());
task.setRelabelConfigs(openmetricsScraperDTO.getMetricRelabelConfigs());

// List<String> targets = new ArrayList<>();
//// 暂时 mock 一下
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,22 @@
*/
package io.holoinsight.server.home.biz.service.impl;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.reflect.TypeToken;

import io.holoinsight.server.common.J;
import io.holoinsight.server.home.biz.service.OpenmetricsScraperService;
import io.holoinsight.server.home.common.util.EventBusHolder;
import io.holoinsight.server.home.common.util.StringUtil;
Expand All @@ -13,23 +29,15 @@
import io.holoinsight.server.home.dal.model.dto.OpenmetricsScraperDTO;
import io.holoinsight.server.home.facade.page.MonitorPageRequest;
import io.holoinsight.server.home.facade.page.MonitorPageResult;
import io.holoinsight.server.common.J;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.gson.reflect.TypeToken;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import io.holoinsight.server.registry.model.OpenmetricsScraperTask;

@Service
public class OpenmetricsScraperServiceImpl extends
ServiceImpl<OpenmetricsScraperMapper, OpenmetricsScraper> implements OpenmetricsScraperService {

private static final Type RELABEL_CONFIG_LIST_TYPE =
new TypeToken<List<OpenmetricsScraperTask.RelabelConfig>>() {}.getType();

@Autowired
private OpenmetricsScraperConverter openmetricsScraperConverter;

Expand Down Expand Up @@ -70,18 +78,25 @@ public void deleteById(Long id) {

@Override
public OpenmetricsScraperDTO toDTO(OpenmetricsScraper model) {
if (model.getConf() == null) {
model.setConf(new HashMap<>());
Map<String, String> conf = model.getConf();
if (conf == null) {
conf = new HashMap<>();
model.setConf(conf);
}

OpenmetricsScraperDTO dto = openmetricsScraperConverter.doToDTO(model);
dto.setMetricsPath(model.getConf().get("metricsPath"));
dto.setSchema(model.getConf().get("schema"));
dto.setScrapeInterval(model.getConf().get("scrapeInterval"));
dto.setScrapeTimeout(model.getConf().get("scrapeTimeout"));
dto.setPort(model.getConf().get("port"));
dto.setCollectRanges(J.fromJson(model.getConf().get("collectRanges"),
(new TypeToken<CloudMonitorRange>() {}).getType()));
dto.setMetricsPath(conf.get("metricsPath"));
dto.setSchema(conf.get("schema"));
dto.setScrapeInterval(conf.get("scrapeInterval"));
dto.setScrapeTimeout(conf.get("scrapeTimeout"));
dto.setPort(conf.get("port"));

dto.setRelabelConfigs(J.fromJson(conf.get("relabelConfigs"), RELABEL_CONFIG_LIST_TYPE));
dto.setMetricRelabelConfigs(
J.fromJson(conf.get("metricRelabelConfigs"), RELABEL_CONFIG_LIST_TYPE));

dto.setCollectRanges(
J.fromJson(conf.get("collectRanges"), (new TypeToken<CloudMonitorRange>() {}).getType()));
return dto;
}

Expand All @@ -97,6 +112,8 @@ public OpenmetricsScraper toDO(OpenmetricsScraperDTO dto) {
model.getConf().put("scrapeTimeout", dto.getScrapeTimeout());
model.getConf().put("port", dto.getPort());
model.getConf().put("collectRanges", J.toJson(dto.getCollectRanges()));
model.getConf().put("relabelConfigs", J.toJson(dto.getRelabelConfigs()));
model.getConf().put("metricRelabelConfigs", J.toJson(dto.getMetricRelabelConfigs()));
return model;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
*/
package io.holoinsight.server.registry.model;

import java.util.List;
import java.util.Map;

import io.holoinsight.server.registry.model.integration.GaeaTask;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.util.List;
import java.util.Map;

@ToString
@Getter
@Setter
Expand All @@ -25,5 +26,19 @@ public class OpenmetricsScraperTask extends GaeaTask {
private Map<String, String> labels;
private Boolean honorLabels;
private Boolean honorTimestamps;
private List<RelabelConfig> relabelConfigs;
private List<RelabelConfig> metricRelabelConfigs;

/**
* Prometheus relabel config
*/
@Data
public static class RelabelConfig {
private List<String> sourceLabels;
private String targetLabel;
private String action;
private String separator;
private String replacement;
private String regex;
}
}

0 comments on commit 5f796da

Please sign in to comment.