[tracing] Support opentelemtry collector. (#10864)

* [tracing] Support opentelemtry collector.

1. support for exporting traces to multiple distributed tracing system via collector;
2. support using collector to process traces.
This commit is contained in:
luozenglin
2022-07-29 16:49:40 +08:00
committed by GitHub
parent 934fe77c06
commit d3c88471ad
10 changed files with 358 additions and 37 deletions

View File

@ -1631,9 +1631,27 @@ public class Config extends ConfigBase {
@ConfField(mutable = true, masterOnly = true)
public static int cbo_default_sample_percentage = 10;
/**
* If this configuration is enabled, you should also specify the trace_export_url.
*/
@ConfField(mutable = false, masterOnly = false)
public static boolean enable_tracing = false;
/**
* Current support for exporting traces:
* zipkin: Export traces directly to zipkin, which is used to enable the tracing feature quickly.
* collector: The collector can be used to receive and process traces and support export to a variety of
* third-party systems.
* If this configuration is enabled, you should also specify the enable_tracing=true and trace_export_url.
*/
@ConfField(mutable = false, masterOnly = false)
public static String trace_exporter = "zipkin";
/**
* The endpoint to export spans to.
* export to zipkin like: http://127.0.0.1:9411/api/v2/spans
* export to collector like: http://127.0.0.1:4318/v1/traces
*/
@ConfField(mutable = false, masterOnly = false)
public static String trace_export_url = "http://127.0.0.1:9411/api/v2/spans";

View File

@ -27,7 +27,7 @@ import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator;
import io.opentelemetry.context.propagation.ContextPropagators;
import io.opentelemetry.exporter.otlp.trace.OtlpGrpcSpanExporter;
import io.opentelemetry.exporter.otlp.http.trace.OtlpHttpSpanExporter;
import io.opentelemetry.exporter.zipkin.ZipkinSpanExporter;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.resources.Resource;
@ -47,20 +47,28 @@ public class Telemetry {
private static OpenTelemetry openTelemetry = OpenTelemetry.noop();
public enum DorisTraceExporter {
zipkin, collector
}
/**
* Initialize {@link OpenTelemetry} with {@link SdkTracerProvider}, {@link BatchSpanProcessor},
* {@link ZipkinSpanExporter} and {@link W3CTraceContextPropagator}.
*/
public static void initOpenTelemetry() {
public static void initOpenTelemetry() throws Exception {
if (!Config.enable_tracing) {
return;
}
// todo: It may be possible to use oltp exporter to export telemetry data to otel collector,
// which in turn processes and sends telemetry data to multiple back-ends (e.g. zipkin, Prometheus,
// Fluent Bit, etc.) to improve scalability.
String httpUrl = Config.trace_export_url;
SpanExporter spanExporter = zipkinExporter(httpUrl);
String traceExportUrl = Config.trace_export_url;
SpanExporter spanExporter;
if (DorisTraceExporter.collector.name().equalsIgnoreCase(Config.trace_exporter)) {
spanExporter = oltpExporter(traceExportUrl);
} else if (DorisTraceExporter.zipkin.name().equalsIgnoreCase(Config.trace_exporter)) {
spanExporter = zipkinExporter(traceExportUrl);
} else {
throw new Exception("unknown value " + Config.trace_exporter + " of trace_exporter in fe.conf");
}
String serviceName = "FRONTEND:" + Env.getCurrentEnv().getSelfNode().first;
Resource serviceNameResource = Resource.create(
@ -84,7 +92,7 @@ public class Telemetry {
}
private static SpanExporter oltpExporter(String httpUrl) {
return OtlpGrpcSpanExporter.builder().setEndpoint(httpUrl).build();
return OtlpHttpSpanExporter.builder().setEndpoint(httpUrl).build();
}
public static OpenTelemetry getOpenTelemetry() {