Skip to content

Commit

Permalink
fix metric log bug (#181)
Browse files Browse the repository at this point in the history
* fix metric-log bug
  • Loading branch information
Oseenix authored Jan 10, 2022
1 parent 6e98b4e commit bf5be73
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 36 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ dependency-reduced-pom.xml
*.swp
**/*.versionsBackup

.patch
.classpath
.factorypath
.project
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ public class AgentLoggerFactory<T extends AgentLogger> {
private final Function<Logger, T> loggerSupplier;
private final Mdc mdc;

private AgentLoggerFactory(@Nonnull ClassLoader classLoader, @Nonnull Object factory, @Nonnull Method method, @Nonnull Function<Logger, T> loggerSupplier, @Nonnull Mdc mdc) {
private AgentLoggerFactory(@Nonnull ClassLoader classLoader,
@Nonnull Object factory, @Nonnull Method method,
@Nonnull Function<Logger, T> loggerSupplier, @Nonnull Mdc mdc) {
this.classLoader = classLoader;
this.factory = factory;
this.method = method;
Expand All @@ -47,15 +49,18 @@ private AgentLoggerFactory(@Nonnull ClassLoader classLoader, @Nonnull Object fac
this.agentLogger = this.getLogger(AgentLoggerFactory.class.getName());
}

public static <T extends AgentLogger> Builder<T> builder(ClassloaderSupplier classLoaderSupplier, Function<Logger, T> loggerSupplier, Class<T> tClass) {
public static <T extends AgentLogger> Builder<T> builder(ClassloaderSupplier classLoaderSupplier,
Function<Logger, T> loggerSupplier,
Class<T> tClass) {
ClassLoader classLoader = Objects.requireNonNull(classLoaderSupplier.get(), "classLoader must not be null.");
return new Builder<>(classLoader, loggerSupplier, tClass);
}

public <N extends AgentLogger> AgentLoggerFactory<N> newFactory(Function<Logger, N> loggerSupplier, Class<N> tClass) {
try {
return new Builder<N>(classLoader, loggerSupplier, tClass).build();
} catch (ClassNotFoundException | NoSuchMethodException | NoSuchFieldException | InstantiationException | InvocationTargetException | IllegalAccessException e) {
} catch (ClassNotFoundException | NoSuchMethodException
| NoSuchFieldException | InstantiationException | InvocationTargetException | IllegalAccessException e) {
agentLogger.error("new factory fail: {}", e);
}
return null;
Expand Down Expand Up @@ -92,7 +97,9 @@ public Builder(@Nonnull ClassLoader classLoader, @Nonnull Function<Logger, T> lo
this.tClass = tClass;
}

public AgentLoggerFactory<T> build() throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException {
public AgentLoggerFactory<T> build() throws ClassNotFoundException, NoSuchMethodException,
IllegalAccessException, InvocationTargetException, InstantiationException, NoSuchFieldException {

ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(classLoader);
Expand Down Expand Up @@ -123,5 +130,4 @@ private Mdc buildMdc() throws ClassNotFoundException, NoSuchFieldException, Ille
return new Mdc(put, remove, get);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public static Builder forRegistry(MetricRegistry registry) {
@SuppressWarnings("rawtypes")
public void report(SortedMap<String, Gauge> gauges, SortedMap<String, Counter> counters, SortedMap<String, Histogram> histograms, SortedMap<String, Meter> meters, SortedMap<String, com.codahale.metrics.Timer> timers) {
Boolean e = this.enabled.get();
if (e != null && e.booleanValue()) {
if (e != null && !e.booleanValue()) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,4 @@ default Boolean getBoolean(String property, Boolean defaultValue) {
Set<String> keySet();

void addChangeListener(PluginConfigChangeListener listener);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright (c) 2021, MegaEase
* All rights reserved.
*
* 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 com.megaease.easeagent.report;

public interface OutputChange {
void onOutPutChange(OutputProperties outputProperties);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import com.megaease.easeagent.config.ConfigUtils;
import com.megaease.easeagent.config.Configs;

import java.util.Map;

import static com.megaease.easeagent.plugin.api.config.ConfigConst.Observability.*;

public interface OutputProperties {
Expand All @@ -44,6 +46,7 @@ public interface OutputProperties {

String getEndpointAlgorithm();

void updateConfig(Map<String, String> changed);

static OutputProperties newDefault(Configs configs) {
return new Default(configs);
Expand Down Expand Up @@ -75,6 +78,22 @@ public Default(Configs configs) {
ConfigUtils.bindProp(OUTPUT_ENDPOINT_IDENTIFICATION_ALGORITHM, configs, Config::getString, v -> this.endpointAlgorithm = v);
}

@Override
public void updateConfig(Map<String, String> changed) {
Configs configs = new Configs(changed);

ConfigUtils.bindProp(OUTPUT_SERVERS, configs, Config::getString, v -> { if (v != null) this.servers = v; });
ConfigUtils.bindProp(OUTPUT_TIMEOUT, configs, Config::getString, v -> { if (v != null) this.timeout = v; });
ConfigUtils.bindProp(OUTPUT_ENABLED, configs, Config::getBoolean, v -> { if (v != null) this.enabled = v; });
ConfigUtils.bindProp(OUTPUT_SECURITY_PROTOCOL, configs, Config::getString, v -> { if (v != null) this.protocol = v; });
ConfigUtils.bindProp(OUTPUT_SSL_KEYSTORE_TYPE, configs, Config::getString, v -> { if (v != null) this.sslKeyStoreType = v; });
ConfigUtils.bindProp(OUTPUT_KEY, configs, Config::getString, v -> { if (v != null) this.sslKey = v; });
ConfigUtils.bindProp(OUTPUT_CERT, configs, Config::getString, v -> { if (v != null) this.certificate = v; });
ConfigUtils.bindProp(OUTPUT_TRUST_CERT, configs, Config::getString, v -> { if (v != null) this.trustCertificate = v; });
ConfigUtils.bindProp(OUTPUT_TRUST_CERT_TYPE, configs, Config::getString, v -> { if (v != null) this.trustCertificateType = v; });
ConfigUtils.bindProp(OUTPUT_ENDPOINT_IDENTIFICATION_ALGORITHM, configs, Config::getString, v -> { if (v != null) this.endpointAlgorithm = v; });
}

@Override
public String getServers() {
return this.servers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@
import org.apache.logging.log4j.core.Logger;

public class KeySender {
private static final String CONSOLE_APPEND = "console";
private final String key;
private final AppenderManager appenderManager;
private final MetricProps metricProps;
private Logger logger;
private org.slf4j.Logger consoleLogger;
private boolean isConsole = false;

public KeySender(String key, AppenderManager appenderManager, MetricProps metricProps) {
this.key = key;
Expand All @@ -36,12 +39,23 @@ public KeySender(String key, AppenderManager appenderManager, MetricProps metric

public void send(String content) {
this.lazyInitLogger();
this.logger.info(content);
if (this.isConsole) {
this.consoleLogger.info(content);
} else {
this.logger.info(content);
}
}

private void lazyInitLogger() {
if (logger == null) {
String loggerName = prepareAppenderAndLogger();
if (logger != null) {
return;
}

String loggerName = prepareAppenderAndLogger();
if (metricProps.getAppendType().equals(CONSOLE_APPEND)) {
this.isConsole = true;
this.consoleLogger = org.slf4j.LoggerFactory.getLogger(loggerName);
} else {
logger = LoggerFactory.getLoggerContext().getLogger(loggerName);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,53 +17,51 @@

package com.megaease.easeagent.report.metric;

import com.megaease.easeagent.config.Config;
import com.megaease.easeagent.config.ConfigUtils;
import com.megaease.easeagent.config.Configs;
import com.megaease.easeagent.plugin.Const;
import com.megaease.easeagent.plugin.api.config.IPluginConfig;
import com.megaease.easeagent.plugin.utils.NoNull;

import static com.megaease.easeagent.plugin.api.config.ConfigConst.Observability.*;
import static com.megaease.easeagent.plugin.api.config.ConfigConst.SERVICE_ID_ENABLED_KEY;
import static com.megaease.easeagent.plugin.api.config.ConfigConst.join;
import static com.megaease.easeagent.plugin.api.config.ConfigConst.Observability.KEY_COMM_APPEND_TYPE;
import static com.megaease.easeagent.plugin.api.config.ConfigConst.Observability.KEY_COMM_TOPIC;

public interface MetricProps {
String getName();

String getAppendType();

String getTopic();

boolean isEnabled();

static MetricProps newDefault(Configs configs, String key) {
return new Default(configs, key);
}
void changeAppendType(String type);

static MetricProps newDefault(IPluginConfig config) {
return new Default(
config.namespace(),
config.enabled(),
NoNull.of(config.getString(KEY_COMM_APPEND_TYPE), Const.METRIC_DEFAULT_APPEND_TYPE),
NoNull.of(config.getString(KEY_COMM_TOPIC), Const.METRIC_DEFAULT_TOPIC)
);
}

class Default implements MetricProps {
private volatile boolean enabled = false;
private volatile String appendType;
private volatile String topic;
private final boolean enabled;
private final String topic;
private final String name;

public Default(Configs configs, String key) {
ConfigUtils.bindProp(join(METRICS, key, SERVICE_ID_ENABLED_KEY), configs, Config::getBoolean, v -> this.enabled = v);
ConfigUtils.bindProp(join(METRICS, key, KEY_COMM_APPEND_TYPE), configs, Config::getString, v -> this.appendType = v);
ConfigUtils.bindProp(join(METRICS, key, KEY_COMM_TOPIC), configs, Config::getString, v -> this.topic = v);
}

public Default(boolean enabled, String appendType, String topic) {
public Default(String name, boolean enabled, String appendType, String topic) {
this.name = name;
this.enabled = enabled;
this.appendType = appendType;
this.topic = topic;
}

@Override
public String getName() {
return this.name;
}

@Override
public String getAppendType() {
return this.appendType;
Expand All @@ -78,5 +76,10 @@ public String getTopic() {
public boolean isEnabled() {
return this.enabled;
}

@Override
public void changeAppendType(String type) {
this.appendType = type;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
package com.megaease.easeagent.report.metric;

import com.megaease.easeagent.config.Configs;
import com.megaease.easeagent.plugin.api.Reporter;
import com.megaease.easeagent.plugin.api.config.ChangeItem;
import com.megaease.easeagent.plugin.api.config.IPluginConfig;
import com.megaease.easeagent.plugin.api.config.PluginConfigChangeListener;
import com.megaease.easeagent.report.OutputChange;
import com.megaease.easeagent.report.OutputProperties;
import com.megaease.easeagent.report.metric.log4j.AppenderManager;
import com.megaease.easeagent.report.util.Utils;
Expand All @@ -29,12 +31,14 @@
import java.util.concurrent.ConcurrentHashMap;

public class MetricReporterImpl implements MetricReporter {
private final ConcurrentHashMap<String, Reporter> reporters;
private final ConcurrentHashMap<String, DefaultMetricReporter> reporters;
private final AppenderManager appenderManager;
private final OutputProperties outputProperties;
private static final String CONSOLE_APPEND = "console";

public MetricReporterImpl(Configs configs) {
this.reporters = new ConcurrentHashMap<>();
OutputProperties outputProperties = Utils.extractOutputProperties(configs);
outputProperties = Utils.extractOutputProperties(configs);
this.appenderManager = AppenderManager.create(outputProperties);
configs.addChangeListener(this);
}
Expand All @@ -44,8 +48,8 @@ public static MetricReporter create(Configs config) {
}

@Override
public com.megaease.easeagent.plugin.api.Reporter reporter(IPluginConfig config) {
Reporter reporter = reporters.get(config.namespace());
public Reporter reporter(IPluginConfig config) {
DefaultMetricReporter reporter = reporters.get(config.namespace());
if (reporter != null) {
return reporter;
}
Expand All @@ -54,7 +58,7 @@ public com.megaease.easeagent.plugin.api.Reporter reporter(IPluginConfig config)
if (reporter != null) {
return reporter;
}
reporter = new Reporter(config);
reporter = new DefaultMetricReporter(config);
reporters.put(config.namespace(), reporter);
return reporter;
}
Expand All @@ -64,17 +68,24 @@ public com.megaease.easeagent.plugin.api.Reporter reporter(IPluginConfig config)
public void onChange(List<ChangeItem> list) {
if (Utils.isOutputPropertiesChange(list)) {
appenderManager.refresh();
Utils.updateOutputPropertiesChange(this.outputProperties, list);
reporters.forEachValue(1, reporter -> reporter.onOutPutChange(this.outputProperties));
}
}

public class Reporter implements com.megaease.easeagent.plugin.api.Reporter, PluginConfigChangeListener {
public class DefaultMetricReporter implements Reporter, OutputChange, PluginConfigChangeListener {
private final String namespace;
private MetricProps metricProps;
private KeySender sender;
private String originalAppendType;

public Reporter(IPluginConfig config) {
public DefaultMetricReporter(IPluginConfig config) {
this.namespace = config.namespace();
this.metricProps = Utils.extractMetricProps(config);
this.originalAppendType = metricProps.getAppendType();

updateAppendType(metricProps, MetricReporterImpl.this.outputProperties, originalAppendType);

this.sender = newKeyLogger();
config.addChangeListener(this);
}
Expand All @@ -90,11 +101,36 @@ private KeySender newKeyLogger() {
@Override
public void onChange(IPluginConfig oldConfig, IPluginConfig newConfig) {
MetricProps newProps = Utils.extractMetricProps(newConfig);
if (metricProps.getTopic().equals(newProps.getTopic()) && metricProps.getAppendType().equals(newProps.getAppendType())) {

this.originalAppendType = updateAppendType(newProps,
MetricReporterImpl.this.outputProperties, newProps.getAppendType());

if (metricProps.getTopic().equals(newProps.getTopic())
&& metricProps.getAppendType().equals(newProps.getAppendType())) {
return;
}
this.metricProps = newProps;
this.sender = newKeyLogger();
}

@Override
public void onOutPutChange(OutputProperties outputProperties) {
String oldAppendType = updateAppendType(this.metricProps, outputProperties, this.originalAppendType);
if (oldAppendType.equals(this.metricProps.getAppendType())) {
return;
}
this.sender = newKeyLogger();
}

private String updateAppendType(MetricProps props, OutputProperties output, String originalAppendType) {
String currentAppendType = props.getAppendType();
if (output.getServers() == null || output.getServers().isEmpty()) {
// override the configuration
props.changeAppendType(CONSOLE_APPEND);
} else {
props.changeAppendType(originalAppendType);
}
return currentAppendType;
}
}
}
Loading

0 comments on commit bf5be73

Please sign in to comment.