forked from alibaba/Sentinel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
liumeng
committed
Jul 11, 2024
1 parent
b4cb335
commit 6b9151a
Showing
19 changed files
with
1,683 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/metric/MetricSender.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.alibaba.csp.sentinel.dashboard.metric; | ||
|
||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.MetricEntity; | ||
|
||
/** | ||
* 监控数据发送器 | ||
* | ||
* @author <a href="mailto:shiyindaxiaojie@gmail.com">gyl</a> | ||
* @since 1.8.2 | ||
*/ | ||
public interface MetricSender { | ||
|
||
void send(Iterable<MetricEntity> iterable); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
...n/java/com/alibaba/csp/sentinel/dashboard/repository/metric/influxdb/InfluxDBConnect.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package com.alibaba.csp.sentinel.dashboard.repository.metric.influxdb; | ||
|
||
import okhttp3.OkHttpClient; | ||
import org.influxdb.InfluxDB; | ||
import org.influxdb.InfluxDBFactory; | ||
import org.influxdb.dto.*; | ||
import org.influxdb.impl.InfluxDBResultMapper; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
@Component | ||
@ConditionalOnProperty(name = "sentinel.metrics.type", havingValue = "influxdb") | ||
public class InfluxDBConnect { | ||
private static final Logger log = LoggerFactory.getLogger(InfluxDBConnect.class); | ||
|
||
private final InfluxDBMetricsProperties influxDBMetricsProperties; | ||
private final OkHttpClientProperties okHttpClientProperties; | ||
private InfluxDB influxDB; | ||
private InfluxDBResultMapper resultMapper = new InfluxDBResultMapper(); | ||
|
||
public InfluxDBConnect(InfluxDBMetricsProperties influxDBMetricsProperties, OkHttpClientProperties okHttpClientProperties) { | ||
this.influxDBMetricsProperties = influxDBMetricsProperties; | ||
this.okHttpClientProperties = okHttpClientProperties; | ||
build(); | ||
} | ||
|
||
private void build() { | ||
|
||
OkHttpClient.Builder okHttpClient = new OkHttpClient.Builder() | ||
.connectTimeout(okHttpClientProperties.getConnectTimeout(), TimeUnit.SECONDS) | ||
.readTimeout(okHttpClientProperties.getReadTimeout(), TimeUnit.SECONDS) | ||
.writeTimeout(okHttpClientProperties.getWriteTimeout(), TimeUnit.SECONDS) | ||
.retryOnConnectionFailure(okHttpClientProperties.getRetryOnConnectionFailure()); | ||
if (influxDBMetricsProperties.getUsername() == null || influxDBMetricsProperties.getPassword() == null) { | ||
influxDB = InfluxDBFactory.connect(influxDBMetricsProperties.getUrl(), okHttpClient); | ||
} else { | ||
influxDB = InfluxDBFactory.connect(influxDBMetricsProperties.getUrl(), | ||
influxDBMetricsProperties.getUsername(), influxDBMetricsProperties.getPassword(), okHttpClient); | ||
} | ||
Pong pong = influxDB.ping(); | ||
if (pong != null) { | ||
log.info("pong:" + pong + ",连接成功!"); | ||
} else { | ||
log.info("连接失败"); | ||
} | ||
|
||
} | ||
|
||
public void insert(String dbName, Point point) { | ||
influxDB.write(dbName, influxDBMetricsProperties.getRetentionPolicy(), point); | ||
} | ||
|
||
public void insert(BatchPoints batchPoints) { | ||
influxDB.write(batchPoints); | ||
} | ||
|
||
public <T> List<T> queryList(String database, String query, Class<T> clazz) { | ||
QueryResult queryResult = influxDB.query(new Query(query, database)); | ||
return resultMapper.toPOJO(queryResult, clazz); | ||
} | ||
|
||
public <T> List<T> queryList(String database, String query, Map<String, Object> paramMap, Class<T> clazz) { | ||
BoundParameterQuery.QueryBuilder queryBuilder = BoundParameterQuery.QueryBuilder.newQuery(query); | ||
queryBuilder.forDatabase(database); | ||
|
||
if (paramMap != null && !paramMap.isEmpty()) { | ||
Set<Map.Entry<String, Object>> entries = paramMap.entrySet(); | ||
for (Map.Entry<String, Object> entry : entries) { | ||
queryBuilder.bind(entry.getKey(), entry.getValue()); | ||
} | ||
} | ||
|
||
QueryResult queryResult = influxDB.query(queryBuilder.create()); | ||
return resultMapper.toPOJO(queryResult, clazz); | ||
} | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
...a/csp/sentinel/dashboard/repository/metric/influxdb/InfluxDBMetricsAutoConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.alibaba.csp.sentinel.dashboard.repository.metric.influxdb; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@ConditionalOnProperty(name = "sentinel.metrics.type", havingValue = "influxdb") | ||
@EnableConfigurationProperties({InfluxDBMetricsProperties.class, OkHttpClientProperties.class}) | ||
@Configuration(proxyBeanMethods = false) | ||
public class InfluxDBMetricsAutoConfiguration { | ||
private static final Logger log= LoggerFactory.getLogger(InfluxDBMetricsAutoConfiguration.class); | ||
|
||
public static final String AUTOWIRED_INFLUX_DB_METRICS_ENTITY_REPOSITORY = "Autowired InfluxDBMetricsEntityRepository"; | ||
|
||
private final InfluxDBMetricsProperties influxDBMetricsProperties; | ||
|
||
private final InfluxDBConnect influxDBClient; | ||
|
||
public InfluxDBMetricsAutoConfiguration(InfluxDBMetricsProperties influxDBMetricsProperties, | ||
InfluxDBConnect influxDBClient) { | ||
this.influxDBMetricsProperties=influxDBMetricsProperties; | ||
this.influxDBClient=influxDBClient; | ||
} | ||
|
||
@Bean | ||
public InfluxDBMetricsEntityRepository influxDBMetricsEntityRepository() { | ||
log.debug(AUTOWIRED_INFLUX_DB_METRICS_ENTITY_REPOSITORY); | ||
return new InfluxDBMetricsEntityRepository(influxDBMetricsProperties, influxDBClient); | ||
} | ||
} |
Oops, something went wrong.