Skip to content

Commit

Permalink
report an extra field: system (#75)
Browse files Browse the repository at this point in the history
* report an extra field: system

* Access-log support send field "system"
MD5 info support send field "system"

Co-authored-by: yuanwei <[email protected]>
  • Loading branch information
landyking and chihuopub authored May 31, 2021
1 parent f98f918 commit 0797ac9
Show file tree
Hide file tree
Showing 16 changed files with 93 additions and 41 deletions.
1 change: 1 addition & 0 deletions build/src/main/resources/agent.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
name=unknown-service
system=unknown-system

### http server
# When the enabled value = false, agent will not start the http server
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public class MD5DictionaryItem {

private String service;

private String system;

private String type;

private String tags;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
public interface ConfigConst {
String DELIMITER = ".";
String SERVICE_NAME = "name";
String SYSTEM_NAME = "system";
String OBSERVABILITY = "observability";
String GLOBAL_CANARY_LABELS = "globalCanaryHeaders";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ private static HashMap<String, String> extractPropsMap(InputStream in) throws IO
}

private static void validConfigs(Configs configs) {
//validate serviceName
//validate serviceName and systemName
ValidateUtils.validate(configs, ConfigConst.SERVICE_NAME, HasText);
ValidateUtils.validate(configs, ConfigConst.SYSTEM_NAME, HasText);
//validate output
ValidateUtils.validate(configs, ConfigConst.Observability.OUTPUT_ENABLED, HasText, Bool);
ValidateUtils.validate(configs, ConfigConst.Observability.OUTPUT_SERVERS, HasText);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ public static void setEndTime(Map<Object, Object> context) {
}

public static Long getEndTime(Map<Object, Object> context) {
return (Long) context.get(END_TIME);
Long endTime = (Long) context.get(END_TIME);
if (endTime == null) {
setEndTime(context);
endTime = (Long) context.get(END_TIME);
}
return endTime;
}

public static long getDuration(Map<Object, Object> context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,17 @@
public class MetricsAdditionalAttributes implements Supplier<Map<String, Object>> {

private volatile Map<String, Object> additionalAttributes;
private volatile String serviceName = "";
private volatile String systemName = "";

public MetricsAdditionalAttributes(Config config) {
ConfigUtils.bindProp(ConfigConst.SERVICE_NAME, config, Config::getString, v -> {
this.additionalAttributes = new AdditionalAttributes(v).getAdditionalAttributes();
this.serviceName = v;
this.additionalAttributes = new AdditionalAttributes(this.serviceName, this.systemName).getAdditionalAttributes();
});
ConfigUtils.bindProp(ConfigConst.SYSTEM_NAME, config, Config::getString, v -> {
this.systemName = v;
this.additionalAttributes = new AdditionalAttributes(this.serviceName, this.systemName).getAdditionalAttributes();
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.megaease.easeagent.report.util.Utils;
import zipkin2.Span;
import zipkin2.codec.Encoding;
import zipkin2.internal.GlobalExtrasSupplier;
import zipkin2.reporter.AsyncReporter;
import zipkin2.reporter.SDKAsyncReporter;
import zipkin2.reporter.Sender;
Expand Down Expand Up @@ -56,14 +57,27 @@ private RefreshableReporter<Span> initSpanRefreshableReporter(Configs configs) {
.build());
}

AutoRefreshConfigItem<String> serviceName = new AutoRefreshConfigItem<>(configs, ConfigConst.SERVICE_NAME, Config::getString);
GlobalExtrasSupplier extrasSupplier = new GlobalExtrasSupplier() {
final AutoRefreshConfigItem<String> serviceName = new AutoRefreshConfigItem<>(configs, ConfigConst.SERVICE_NAME, Config::getString);
final AutoRefreshConfigItem<String> systemName = new AutoRefreshConfigItem<>(configs, ConfigConst.SYSTEM_NAME, Config::getString);

@Override
public String service() {
return serviceName.getValue();
}

@Override
public String system() {
return systemName.getValue();
}
};
SDKAsyncReporter reporter = SDKAsyncReporter.
builderSDKAsyncReporter(AsyncReporter.builder(sender)
.queuedMaxSpans(traceProperties.getOutput().getQueuedMaxSpans())
.messageTimeout(traceProperties.getOutput().getMessageTimeout(), TimeUnit.MILLISECONDS)
.queuedMaxBytes(traceProperties.getOutput().getQueuedMaxSize()),
traceProperties,
serviceName::getValue);
extrasSupplier);
reporter.startFlushThread();
spanRefreshableReporter = new RefreshableReporter<Span>(reporter, traceProperties, outputProperties);
return spanRefreshableReporter;
Expand Down
24 changes: 17 additions & 7 deletions report/src/main/java/zipkin2/internal/AgentV2SpanGlobalWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,21 @@
import zipkin2.Span;

import java.util.Optional;
import java.util.function.Supplier;

public class AgentV2SpanGlobalWriter implements WriteBuffer.Writer<Span> {

final String type;
final Supplier<String> service;//= ApplicationUtils.getBean(Environment.class).getProperty(MetricNameBuilder
final GlobalExtrasSupplier extras;//= ApplicationUtils.getBean(Environment.class).getProperty(MetricNameBuilder
// .SPRING_APPLICATION_NAME, "");
final TraceProps traceProperties;//= ApplicationUtils.getBean(TraceProperties.class);

final String typeFieldName = ",\"type\":\"";
final String serviceFieldName = ",\"service\":\"";
final String systemFieldName = ",\"system\":\"";

public AgentV2SpanGlobalWriter(String type, Supplier<String> service, TraceProps tp) {
public AgentV2SpanGlobalWriter(String type, GlobalExtrasSupplier extras, TraceProps tp) {
this.type = type;
this.service = service;
this.extras = extras;
this.traceProperties = tp;
}

Expand All @@ -50,12 +50,17 @@ public int sizeInBytes(Span value) {
mutableInt.add(JsonEscaper.jsonEscapedSizeInBytes(type));
}

String tmpService = this.service.get();
String tmpService = this.extras.system();
if (TextUtils.hasText(tmpService)) {
mutableInt.add(serviceFieldName.length() + 1);
mutableInt.add(JsonEscaper.jsonEscapedSizeInBytes(tmpService));
}

String tmpSystem = this.extras.system();
if (TextUtils.hasText(tmpSystem)) {
mutableInt.add(systemFieldName.length() + 1);
mutableInt.add(JsonEscaper.jsonEscapedSizeInBytes(tmpSystem));
}
});
return mutableInt.intValue();
}
Expand All @@ -68,13 +73,18 @@ public void write(Span value, WriteBuffer buffer) {
buffer.writeUtf8(JsonEscaper.jsonEscape(type));
buffer.writeByte(34);
}
String tmpService = this.service.get();
String tmpService = this.extras.system();
if (TextUtils.hasText(tmpService)) {
buffer.writeAscii(serviceFieldName);
buffer.writeUtf8(JsonEscaper.jsonEscape(tmpService));
buffer.writeByte(34);
}

String tmpSystem = this.extras.system();
if (TextUtils.hasText(tmpSystem)) {
buffer.writeAscii(systemFieldName);
buffer.writeUtf8(JsonEscaper.jsonEscape(tmpSystem));
buffer.writeByte(34);
}
});
}
}
17 changes: 13 additions & 4 deletions report/src/main/java/zipkin2/internal/AgentV2SpanWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,25 +23,34 @@
import zipkin2.Span;

import java.util.Collection;
import java.util.function.Supplier;

public class AgentV2SpanWriter implements WriteBuffer.Writer<Span> {

public final Collection<WriteBuffer.Writer<Span>> writerList;

@Deprecated
public AgentV2SpanWriter() {
this(()->"", null);
this(new GlobalExtrasSupplier() {
@Override
public String service() {
return "";
}

@Override
public String system() {
return "";
}
}, null);
}

public AgentV2SpanWriter(Supplier<String> service, TraceProps properties) {
public AgentV2SpanWriter(GlobalExtrasSupplier extrasSupplier, TraceProps properties) {
writerList = ImmutableList.<WriteBuffer.Writer<Span>>builder()
.add(new AgentV2SpanBaseWriter())
.add(new AgentV2SpanLocalEndpointWriter())
.add(new AgentV2SpanRemoteEndpointWriter())
.add(new AgentV2SpanAnnotationsWriter())
.add(new AgentV2SpanTagsWriter())
.add(new AgentV2SpanGlobalWriter("log-tracing", service, properties))
.add(new AgentV2SpanGlobalWriter("log-tracing", extrasSupplier, properties))
.build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package zipkin2.internal;

public interface GlobalExtrasSupplier {
String service();

String system();
}
18 changes: 9 additions & 9 deletions report/src/main/java/zipkin2/reporter/SDKAsyncReporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import zipkin2.codec.Encoding;
import zipkin2.codec.SpanBytesEncoder;
import zipkin2.internal.AgentV2SpanWriter;
import zipkin2.internal.GlobalExtrasSupplier;
import zipkin2.internal.JsonCodec;
import zipkin2.reporter.kafka11.SDKSender;

Expand All @@ -37,7 +38,6 @@
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Supplier;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand Down Expand Up @@ -82,10 +82,10 @@ public class SDKAsyncReporter<S> extends AsyncReporter<S> {

public static SDKAsyncReporter<Span> builderSDKAsyncReporter(AsyncReporter.Builder builder,
TraceProps traceProperties,
Supplier<String> service) {
GlobalExtrasSupplier extrasSupplier) {
final SDKAsyncReporter<Span> reporter = new Builder(builder
.messageMaxBytes(traceProperties.getOutput().getMessageMaxBytes())) //设置队列的最大count和最大的size
.build(traceProperties, service);
.build(traceProperties, extrasSupplier);
reporter.setTraceProperties(traceProperties);
return reporter;
}
Expand Down Expand Up @@ -378,11 +378,11 @@ public AsyncReporter.Builder queuedMaxBytes(int queuedMaxBytes) {
/**
* Builds an async reporter that encodes zipkin spans as they are reported.
*/
public SDKAsyncReporter<Span> build(TraceProps traceProperties, Supplier<String> service) {
public SDKAsyncReporter<Span> build(TraceProps traceProperties, GlobalExtrasSupplier extrasSupplier) {
this.traceProperties = traceProperties;
switch (builder.sender.encoding()) {
case JSON:
return build(getAgentEncoder(traceProperties, service));
return build(getAgentEncoder(traceProperties, extrasSupplier));
case PROTO3:
return build(SpanBytesEncoder.PROTO3);
case THRIFT:
Expand All @@ -392,8 +392,8 @@ public SDKAsyncReporter<Span> build(TraceProps traceProperties, Supplier<String>
}
}

private BytesEncoder<Span> getAgentEncoder(TraceProps tp, Supplier<String> service) {
return new AgentJSONByteEncoder(service, tp);
private BytesEncoder<Span> getAgentEncoder(TraceProps tp, GlobalExtrasSupplier extrasSupplier) {
return new AgentJSONByteEncoder( extrasSupplier, tp);
}

/**
Expand Down Expand Up @@ -444,8 +444,8 @@ private static class AgentJSONByteEncoder implements BytesEncoder<Span> {

final AgentV2SpanWriter writer;

AgentJSONByteEncoder(Supplier<String> service, TraceProps traceProperties) {
writer = new AgentV2SpanWriter(service, traceProperties);
AgentJSONByteEncoder(GlobalExtrasSupplier extrasSupplier, TraceProps traceProperties) {
writer = new AgentV2SpanWriter( extrasSupplier, traceProperties);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ public Supplier<AgentInterceptorChain.Builder> supplier4Filter() {
.addInterceptor(new HTTPHeaderExtractInterceptor(new CrossThreadPropagationConfig(this.config)))
.addInterceptor(new HttpFilterMetricsInterceptor(servletMetric, config))
.addInterceptor(new HttpFilterTracingInterceptor(this.tracing, config))
.addInterceptor(new ServletHttpLogInterceptor(serviceName, config, s -> agentReport.report(new MetricItem(ConfigConst.Observability.KEY_METRICS_ACCESS, s))))
.addInterceptor(new ServletHttpLogInterceptor(config, s -> agentReport.report(new MetricItem(ConfigConst.Observability.KEY_METRICS_ACCESS, s))))
;
};
}
Expand Down Expand Up @@ -287,7 +287,7 @@ public Supplier<AgentInterceptorChain.Builder> supplier4Gateway() {
AgentInterceptorChain.Builder headersFilterChainBuilder = ChainBuilderFactory.DEFAULT.createBuilder()
.addInterceptor(gatewayMetricsInterceptor)
.addInterceptor(new SpringGatewayServerTracingInterceptor(tracing, config))
.addInterceptor(new SpringGatewayLogInterceptor(this.serviceName, config, s -> agentReport.report(new MetricItem(ConfigConst.Observability.KEY_METRICS_ACCESS, s))));
.addInterceptor(new SpringGatewayLogInterceptor(config, s -> agentReport.report(new MetricItem(ConfigConst.Observability.KEY_METRICS_ACCESS, s))));
return ChainBuilderFactory.DEFAULT.createBuilder()
.addInterceptor(new SpringGatewayInitGlobalFilterInterceptor(headersFilterChainBuilder, chainInvoker));
};
Expand Down Expand Up @@ -522,6 +522,7 @@ public void accept(Map<String, String> map) {
.hostName(HostAddress.localhost())
.hostIpv4(HostAddress.getHostIpv4())
.gid("")
.system(config.getString("system"))
.service(serviceName.getValue())
.tags("")
.type("md5-dictionary")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@
@Slf4j
public class HttpLog {

public RequestInfo prepare(String serviceName, Long beginTime, Span span, AccessLogServerInfo serverInfo) {
public RequestInfo prepare(String system, String serviceName, Long beginTime, Span span, AccessLogServerInfo serverInfo) {
RequestInfo requestInfo = new RequestInfo();
requestInfo.setSystem(system);
requestInfo.setService(serviceName);
requestInfo.setHostName(HostAddress.localhost());
requestInfo.setHostIpv4(HostAddress.getHostIpv4());
Expand Down Expand Up @@ -59,7 +60,6 @@ public String getLogString(RequestInfo requestInfo, boolean success, Long beginT
List<RequestInfo> list = new ArrayList<>(1);
list.add(requestInfo);
String logString = JsonUtil.toJson(list);
// log.info("access-log: {}", logString);
return logString;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public class RequestInfo {

private String service;

private String system;

@JsonProperty("client_ip")
private String clientIP = "-";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import com.megaease.easeagent.common.ContextCons;
import com.megaease.easeagent.common.config.SwitchUtil;
import com.megaease.easeagent.common.http.HttpServletInterceptor;
import com.megaease.easeagent.config.AutoRefreshConfigItem;
import com.megaease.easeagent.config.Config;
import com.megaease.easeagent.core.interceptor.MethodInfo;
import com.megaease.easeagent.core.utils.ContextUtils;
Expand All @@ -43,14 +42,11 @@ public class ServletHttpLogInterceptor extends HttpServletInterceptor {

private final Consumer<String> reportConsumer;

private final AutoRefreshConfigItem<String> serviceName;

private final static String PROCESSED_BEFORE_KEY = ServletHttpLogInterceptor.class.getName() + ".processedBefore";

private final static String PROCESSED_AFTER_KEY = ServletHttpLogInterceptor.class.getName() + ".processedAfter";

public ServletHttpLogInterceptor(AutoRefreshConfigItem<String> serviceName, Config config, Consumer<String> reportConsumer) {
this.serviceName = serviceName;
public ServletHttpLogInterceptor(Config config, Consumer<String> reportConsumer) {
this.reportConsumer = reportConsumer;
this.config = config;
}
Expand All @@ -73,7 +69,7 @@ public void internalBefore(MethodInfo methodInfo, Map<Object, Object> context, H
Long beginTime = ContextUtils.getBeginTime(context);
Span span = (Span) context.get(ContextCons.SPAN);
AccessLogServerInfo serverInfo = this.serverInfo(httpServletRequest, httpServletResponse);
RequestInfo requestInfo = this.httpLog.prepare(this.serviceName.getValue(), beginTime, span, serverInfo);
RequestInfo requestInfo = this.httpLog.prepare(config.getString("system"), config.getString("name"), beginTime, span, serverInfo);
httpServletRequest.setAttribute(RequestInfo.class.getName(), requestInfo);
}

Expand Down
Loading

0 comments on commit 0797ac9

Please sign in to comment.