Skip to content

Commit

Permalink
[type:refactor] refactor shenyu-plugin-logging. (apache#3917)
Browse files Browse the repository at this point in the history
* [type:refactor] refactor shenyu-plugin-logging.

* [type:refactor] remove useless attributes.

* [type:refactor] refactor shenyu-plugin-logging.

* [type:refactor] refactor shenyu-plugin-logging.
  • Loading branch information
yunlongn authored Sep 5, 2022
1 parent cedb14a commit 83e9958
Show file tree
Hide file tree
Showing 18 changed files with 53 additions and 213 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shenyu.common.concurrent.ShenyuThreadFactory;
import org.apache.shenyu.common.utils.GsonUtils;
Expand All @@ -42,6 +41,7 @@
import org.apache.shenyu.plugin.logging.common.entity.ShenyuRequestLog;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.lang.NonNull;

import javax.annotation.Nullable;
import java.util.ArrayList;
Expand Down Expand Up @@ -74,7 +74,7 @@ public class AliyunSlsLogCollectClient extends AbstractLogConsumeClient<AliyunLo
* @param config config
*/
@Override
public void initClient0(final AliyunLogCollectConfig.AliyunSlsLogConfig config) {
public void initClient0(@NonNull final AliyunLogCollectConfig.AliyunSlsLogConfig config) {
String accessId = config.getAccessId();
String accessKey = config.getAccessKey();
String host = config.getHost();
Expand Down Expand Up @@ -107,10 +107,7 @@ public void initClient0(final AliyunLogCollectConfig.AliyunSlsLogConfig config)
* @param logs list of log
*/
@Override
public void consume0(final List<ShenyuRequestLog> logs) {
if (CollectionUtils.isEmpty(logs)) {
return;
}
public void consume0(@NonNull final List<ShenyuRequestLog> logs) {
logs.forEach(this::sendLog);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@

package org.apache.shenyu.plugin.aliyun.sls.collector;

import org.apache.shenyu.plugin.aliyun.sls.client.AliyunSlsLogCollectClient;
import org.apache.shenyu.plugin.aliyun.sls.handler.LoggingAliyunSlsPluginDataHandler;
import org.apache.shenyu.plugin.logging.common.client.LogConsumeClient;
import org.apache.shenyu.plugin.logging.common.collector.AbstractLogCollector;
import org.apache.shenyu.plugin.logging.common.collector.LogCollector;

/**
* aliyun sls log collector,depend a LogConsumeClient for consume logs.
*/
public class AliyunSlsLogCollector extends AbstractLogCollector {
public class AliyunSlsLogCollector extends AbstractLogCollector<AliyunSlsLogCollectClient> {

private static final LogCollector INSTANCE = new AliyunSlsLogCollector();

Expand All @@ -39,7 +39,7 @@ public static LogCollector getInstance() {
}

@Override
protected LogConsumeClient getLogConsumeClient() {
protected AliyunSlsLogCollectClient getLogConsumeClient() {
return LoggingAliyunSlsPluginDataHandler.getAliyunSlsLogCollectClient();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.apache.shenyu.plugin.logging.clickhouse.constant.ClickHouseLoggingConstant;
import org.apache.shenyu.plugin.logging.common.client.AbstractLogConsumeClient;
import org.apache.shenyu.plugin.logging.common.entity.ShenyuRequestLog;
import org.springframework.lang.NonNull;

import java.util.List;
import java.util.Objects;
Expand All @@ -49,7 +50,7 @@ public class ClickHouseLogCollectClient extends AbstractLogConsumeClient<ClickHo
private ClickHouseNode endpoint;

@Override
public void consume0(final List<ShenyuRequestLog> logs) throws Exception {
public void consume0(@NonNull final List<ShenyuRequestLog> logs) throws Exception {
if (CollectionUtils.isNotEmpty(logs)) {
Object[][] datas = new Object[logs.size()][];
for (int i = 0; i < logs.size(); i++) {
Expand Down Expand Up @@ -115,7 +116,7 @@ public void close0() {
* @param config properties.
*/
@Override
public void initClient0(final ClickHouseLogCollectConfig.ClickHouseLogConfig config) {
public void initClient0(@NonNull final ClickHouseLogCollectConfig.ClickHouseLogConfig config) {
final String username = config.getUsername();
final String password = config.getPassword();
endpoint = ClickHouseNode.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@

package org.apache.shenyu.plugin.logging.clickhouse.collector;

import org.apache.shenyu.plugin.logging.clickhouse.client.ClickHouseLogCollectClient;
import org.apache.shenyu.plugin.logging.clickhouse.handler.LoggingClickHousePluginDataHandler;
import org.apache.shenyu.plugin.logging.common.client.LogConsumeClient;
import org.apache.shenyu.plugin.logging.common.collector.AbstractLogCollector;
import org.apache.shenyu.plugin.logging.common.collector.LogCollector;

public class ClickHouseLogCollector extends AbstractLogCollector {
public class ClickHouseLogCollector extends AbstractLogCollector<ClickHouseLogCollectClient> {

private static final LogCollector INSTANCE = new ClickHouseLogCollector();

Expand All @@ -36,7 +36,7 @@ public static LogCollector getInstance() {
}

@Override
protected LogConsumeClient getLogConsumeClient() {
protected ClickHouseLogCollectClient getLogConsumeClient() {
return LoggingClickHousePluginDataHandler.getClickHouseLogCollectClient();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.apache.shenyu.plugin.logging.clickhouse.client.ClickHouseLogCollectClient;
import org.apache.shenyu.plugin.logging.clickhouse.collector.ClickHouseLogCollector;
import org.apache.shenyu.plugin.logging.clickhouse.config.ClickHouseLogCollectConfig;
import org.apache.shenyu.plugin.logging.common.client.LogConsumeClient;
import org.apache.shenyu.plugin.logging.common.collector.LogCollector;
import org.apache.shenyu.plugin.logging.common.config.GenericApiConfig;
import org.apache.shenyu.plugin.logging.common.handler.AbstractLogPluginDataHandler;
Expand Down Expand Up @@ -62,7 +61,7 @@ public String pluginNamed() {
*
* @return LogConsumeClient
*/
public static LogConsumeClient getClickHouseLogCollectClient() {
public static ClickHouseLogCollectClient getClickHouseLogCollectClient() {
return CLICK_HOUSE_LOG_COLLECT_CLIENT;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.shenyu.plugin.logging.common.client;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.shenyu.plugin.logging.common.config.GenericGlobalConfig;
import org.apache.shenyu.plugin.logging.common.entity.ShenyuRequestLog;
import org.slf4j.Logger;
Expand Down Expand Up @@ -52,7 +53,7 @@ public abstract class AbstractLogConsumeClient<T extends GenericGlobalConfig> im
* @param logs logs
* @throws Exception error
*/
public abstract void consume0(List<ShenyuRequestLog> logs) throws Exception;
public abstract void consume0(@NonNull List<ShenyuRequestLog> logs) throws Exception;

/**
* close0.
Expand All @@ -67,7 +68,7 @@ public void initClient(final T config) {
this.close();
}
if (ObjectUtils.isEmpty(config)) {
LOG.error("{} config is null.", this.getClass().getSimpleName());
LOG.error("{} config is null, client not init.", this.getClass().getSimpleName());
return;
}
this.initClient0(config);
Expand All @@ -93,7 +94,7 @@ public void close() {

@Override
public void consume(final List<ShenyuRequestLog> logs) throws Exception {
if (!isStarted.get()) {
if (CollectionUtils.isEmpty(logs) || !isStarted.get()) {
return;
}
this.consume0(logs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import org.apache.shenyu.common.constant.Constants;
import org.apache.shenyu.common.utils.Singleton;
import org.apache.shenyu.common.utils.ThreadUtils;
import org.apache.shenyu.plugin.logging.common.client.LogConsumeClient;
import org.apache.shenyu.plugin.logging.common.client.AbstractLogConsumeClient;
import org.apache.shenyu.plugin.logging.common.entity.ShenyuRequestLog;
import org.apache.shenyu.plugin.logging.common.utils.LogCollectConfigUtils;
import org.slf4j.Logger;
Expand All @@ -43,7 +43,7 @@
/**
* abstract log collector,Contains common methods.
*/
public abstract class AbstractLogCollector implements LogCollector {
public abstract class AbstractLogCollector<T extends AbstractLogConsumeClient<?>> implements LogCollector {

private static final Logger LOG = LoggerFactory.getLogger(AbstractLogCollector.class);

Expand Down Expand Up @@ -94,7 +94,7 @@ private void consume() {
int batchSize = 100;
if (size >= batchSize || timeDiffMs > diffTimeMSForPush) {
bufferQueue.drainTo(logs, batchSize);
LogConsumeClient logCollectClient = getLogConsumeClient();
AbstractLogConsumeClient<?> logCollectClient = getLogConsumeClient();
if (Objects.nonNull(logCollectClient)) {
logCollectClient.consume(logs);
}
Expand All @@ -114,12 +114,12 @@ private void consume() {
*
* @return log consume client
*/
protected abstract LogConsumeClient getLogConsumeClient();
protected abstract T getLogConsumeClient();

@Override
public void close() throws Exception {
started.set(false);
LogConsumeClient logCollectClient = getLogConsumeClient();
AbstractLogConsumeClient<?> logCollectClient = getLogConsumeClient();
if (logCollectClient != null) {
logCollectClient.close();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,116 +37,6 @@ public class GenericLoggingConstant {
*/
public static final String SHENYU_AGENT_TRACE_ID = "shenyu-agent-trace-id";

/**
* aliyun sls accessId.
*/
public static final String ACCESS_ID = "AccessId";

/**
* aliyun sls accessKey.
*/
public static final String ACCESS_KEY = "AccessKey";

/**
* aliyun sls project.
*/
public static final String PROJECT_NAME = "ProjectName";

/**
* aliyun sls logstore name.
*/
public static final String LOG_STORE = "LogStore";

/**
* aliyun sls ttl in day.
*/
public static final String TTL_IN_DAY = "TtlInDay";

/**
* aliyun sls shard count.
*/
public static final String SHARD_COUNT = "ShardCount";

/**
* aliyun sls topic.
*/
public static final String TOPIC = "topic";

/**
* tencent cls secretId.
*/
public static final String SECRET_ID = "secretId";

/**
* tencent cls secretKey.
*/
public static final String SECRET_KEY = "secretKey";

/**
* tencent cls endpoint.
*/
public static final String ENDPOINT = "endpoint";

/**
* tencent cls totalSizeInBytes.
*/
public static final String TOTAL_SIZE_IN_BYTES = "totalSizeInBytes";

/**
* tencent cls maxSendThreadCount.
*/
public static final String MAX_SEND_THREAD_COUNT = "maxSendThreadCount";

/**
* tencent cls maxBlockSec.
*/
public static final String MAX_BLOCK_SEC = "maxBlockSec";

/**
* tencent cls maxBatchSize.
*/
public static final String MAX_BATCH_SIZE = "maxBatchSize";

/**
* tencent cls maxBatchCount.
*/
public static final String MAX_BATCH_COUNT = "maxBatchCount";

/**
* tencent cls lingerMs.
*/
public static final String LINGER_MS = "lingerMs";

/**
* tencent cls retries.
*/
public static final String RETRIES = "retries";

/**
* tencent cls maxReservedAttempts.
*/
public static final String MAX_RESERVED_ATTEMPTS = "maxReservedAttempts";

/**
* tencent cls baseRetryBackoffMs.
*/
public static final String BASE_RETRY_BACKOFF_MS = "baseRetryBackoffMs";

/**
* tencent cls maxRetryBackoffMs.
*/
public static final String MAX_RETRY_BACKOFF_MS = "maxRetryBackoffMs";

/**
* send thread config.
*/
public static final String SEND_THREAD_COUNT = "sendThreadCount";

/**
* io thread count.
*/
public static final String IO_THREAD_COUNT = "ioThreadCount";

/**
* system default, max threads.
*/
Expand All @@ -166,35 +56,5 @@ public class GenericLoggingConstant {
* The constant INDEX.
*/
public static final String INDEX = "shenyu-access-logging";

/**
* The constant PORT.
*/
public static final String PORT = "port";

/**
* The constant NAMESERVER_ADDRESS.
*/
public static final String NAMESERVER_ADDRESS = "namesrvAddr";

/**
* The constant SERVICE_URL.
*/
public static final String SERVICE_URL = "serviceUrl";

/**
* The constant PRODUCER_GROUP.
*/
public static final String PRODUCER_GROUP = "producerGroup";

/**
* The constant USERNAME.
*/
public static final String USERNAME = "username";

/**
* The constant PASSWORD.
*/
public static final String PASSWORD = "password";

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.endpoints.BooleanResponse;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
Expand All @@ -38,6 +37,7 @@
import org.elasticsearch.client.RestClientBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.lang.NonNull;

import java.io.IOException;
import java.util.ArrayList;
Expand All @@ -63,7 +63,7 @@ public class ElasticSearchLogCollectClient extends AbstractLogConsumeClient<Elas
* @param config elasticsearch client config
*/
@Override
public void initClient0(final ElasticSearchLogCollectConfig.ElasticSearchLogConfig config) {
public void initClient0(@NonNull final ElasticSearchLogCollectConfig.ElasticSearchLogConfig config) {
RestClientBuilder builder = RestClient
.builder(new HttpHost(config.getHost(), Integer.parseInt(config.getPort())));

Expand Down Expand Up @@ -92,10 +92,7 @@ public void initClient0(final ElasticSearchLogCollectConfig.ElasticSearchLogConf
}

@Override
public void consume0(final List<ShenyuRequestLog> logs) {
if (CollectionUtils.isEmpty(logs)) {
return;
}
public void consume0(@NonNull final List<ShenyuRequestLog> logs) {
List<BulkOperation> bulkOperations = new ArrayList<>();
logs.forEach(log -> {
try {
Expand Down
Loading

0 comments on commit 83e9958

Please sign in to comment.