Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE #340]Add http trace http point #527

Merged
merged 49 commits into from
Dec 16, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
dde6ab5
tracing in AbstractHTTPServer
Roc-00 Sep 15, 2021
2ed29e2
tracing in AbstractHTTPServer
Roc-00 Sep 17, 2021
2293b9c
tracing in AbstractHTTPServer
Roc-00 Sep 20, 2021
2c5cc3d
trace in AbstractHTTPServer
Roc-00 Sep 20, 2021
60e9d69
add licence
Roc-00 Sep 20, 2021
744b16c
add licence
Roc-00 Sep 20, 2021
7479fd9
the span exporter
Roc-00 Sep 20, 2021
53a3fdf
design docs
Roc-00 Sep 20, 2021
40bb928
fix the error on text
Roc-00 Sep 22, 2021
69c62bc
delete the useless dependence
Roc-00 Sep 22, 2021
da9ed30
delete the useless dependence
Roc-00 Sep 22, 2021
3ceb7ee
add license
Roc-00 Sep 22, 2021
eea9212
add license
Roc-00 Sep 22, 2021
3d7b605
remove the unused code
Roc-00 Sep 23, 2021
4a29456
fix the different spanExporter
Roc-00 Sep 24, 2021
66bba23
change the class name
Roc-00 Sep 24, 2021
ac5d236
Merge branch 'develop' into trace3
Roc-00 Sep 24, 2021
d22bb45
fix gradle -build problem
Roc-00 Sep 24, 2021
70499ad
design docs improve
Roc-00 Sep 25, 2021
affc589
Merge branch 'develop' into trace3
Roc-00 Sep 25, 2021
78e2f62
fix the gradle.build error problem
Roc-00 Sep 25, 2021
c1e38d7
fixed
Roc-00 Oct 1, 2021
1e874ca
unsure fix
Roc-00 Oct 1, 2021
5261c1a
fixed
Roc-00 Oct 1, 2021
0cd4fc4
fix the path name
Roc-00 Oct 4, 2021
7de65fc
Merge branch 'develop' into trace3
Roc-00 Oct 22, 2021
51ff1e2
fix the path name
Roc-00 Oct 22, 2021
be96e82
fix check error
Roc-00 Oct 22, 2021
0440060
fix check error
Roc-00 Oct 22, 2021
4da95e7
fix check error
Roc-00 Oct 22, 2021
e3977fa
change the max size of one line from 100 to 120
Roc-00 Oct 22, 2021
d537406
Wrap lines longer than 120 characters
Roc-00 Oct 22, 2021
9578c73
Wrap lines longer than 120 characters
Roc-00 Oct 22, 2021
31d6a61
Wrap lines longer than 120 characters
Roc-00 Oct 22, 2021
9b6e253
format code
Roc-00 Oct 23, 2021
4fca695
format code
Roc-00 Oct 23, 2021
98d7a3e
format code
Roc-00 Oct 23, 2021
037d371
format code
Roc-00 Oct 23, 2021
bb0971d
format code
Roc-00 Oct 23, 2021
6642680
add javadoc
Roc-00 Oct 23, 2021
76b42fd
checkstyle fix
Roc-00 Oct 23, 2021
1ad5545
unversioned files
Roc-00 Nov 22, 2021
71d3bbd
Merge branch 'develop' into trace3
Roc-00 Nov 22, 2021
96b54b8
put context into channel in advance
Roc-00 Nov 22, 2021
6e883b0
Merge branch 'develop' into trace3
Roc-00 Dec 10, 2021
d48de99
Merge branch 'apache:develop' into trace3
Roc-00 Dec 14, 2021
836b13c
fix error
Roc-00 Dec 14, 2021
977b942
fix error
Roc-00 Dec 15, 2021
da634ea
fix error
Roc-00 Dec 15, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,8 @@ subprojects {
dependency 'io.prometheus:simpleclient:0.8.1'
dependency 'io.prometheus:simpleclient_httpserver:0.8.1'
dependency 'io.opentelemetry:opentelemetry-exporter-logging:1.3.0'
dependency 'io.opentelemetry.instrumentation:opentelemetry-instrumentation-api:0.13.0'
dependency 'io.opentelemetry:opentelemetry-exporter-zipkin:1.6.0'


dependency "io.openmessaging:openmessaging-api:2.2.1-pubsub"

Expand Down
2 changes: 1 addition & 1 deletion eventmesh-runtime/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ dependencies {
api 'io.prometheus:simpleclient'
api 'io.prometheus:simpleclient_httpserver'
api 'io.opentelemetry:opentelemetry-exporter-logging'
api 'io.opentelemetry.instrumentation:opentelemetry-instrumentation-api'
api 'io.opentelemetry:opentelemetry-exporter-zipkin'

implementation project(":eventmesh-connector-plugin:eventmesh-connector-api")
implementation project(":eventmesh-security-plugin:eventmesh-security-api")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import javax.annotation.Nullable;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLEngine;

Expand Down Expand Up @@ -60,7 +59,6 @@
import io.opentelemetry.api.trace.StatusCode;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.ContextKey;
import io.opentelemetry.context.Scope;
import io.opentelemetry.context.propagation.TextMapGetter;
import io.opentelemetry.context.propagation.TextMapPropagator;
Expand Down Expand Up @@ -159,7 +157,10 @@ public void sendResponse(ChannelHandlerContext ctx,
//todo end server span, we should get channel here to get span in channel's context in async call.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may need to add a switch to determine whether you want to enter here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Context context = ctx.channel().attr(AttributeKeys.SERVER_CONTEXT).get();
Span span = context.get(SpanKey.SERVER_KEY);
span.end();
try (Scope ignored = context.makeCurrent()) {
span.addEvent("Send Response");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to add event about response

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

span.end();
}

ctx.writeAndFlush(response).addListener(new ChannelFutureListener() {
@Override
Expand Down Expand Up @@ -237,10 +238,6 @@ public Iterable<String> keys(HttpRequest carrier) {

@Override
public String get(HttpRequest carrier, String key) {
// if (carrier.headers().containsKey(key)) {
// return carrier.headers().get(key).get(0);
// }
// return "";
return carrier.headers().get(key);
}
});
Expand All @@ -249,8 +246,6 @@ public String get(HttpRequest carrier, String key) {
// //attach span context in server context
// ctx.channel().attr(AttributeKeys.SERVER_CONTEXT).set(span.getSpanContext());

// ctx.writeAndFlush(span.getSpanContext());
// ctx.fireChannelRead(span.getSpanContext());
Span span = null;

try {
Expand Down Expand Up @@ -306,6 +301,7 @@ public String get(HttpRequest carrier, String key) {

//todo record command opaque in span.
span = tracer.spanBuilder("HTTP"+requestCommand.httpMethod).setParent(context).setSpanKind(SpanKind.SERVER).startSpan();
span.addEvent("Span Start");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

avoid span event here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

//attach the span to the server context
context = context.with(SpanKey.SERVER_KEY,span);
//put the context in channel
Expand Down Expand Up @@ -356,6 +352,7 @@ public String get(HttpRequest carrier, String key) {
} catch (Exception ex) {
httpServerLogger.error("AbrstractHTTPServer.HTTPHandler.channelRead0 err", ex);
//todo span end with exception.
span.addEvent("End Exceptional");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should add an exception here, but we need to record the cause in event atttribute

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i am not sure whether i am doing it in a right way

span.setStatus(StatusCode.ERROR,ex.getMessage());//set this span's status to ERROR
span.recordException(ex);//record this exception
span.end();// closing the scope does not end the span, this has to be done manually
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package org.apache.eventmesh.runtime.exporter;

import io.opentelemetry.sdk.common.CompletableResultCode;
import io.opentelemetry.sdk.common.InstrumentationLibraryInfo;
import io.opentelemetry.sdk.trace.data.SpanData;
import io.opentelemetry.sdk.trace.export.SpanExporter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collection;

/**
* Because the class 'LoggingSpanExporter' in openTelemetry exported garbled code in eventMesh's startUp, I override the 'LoggingSpanExporter'.
*/
public class logExporter implements SpanExporter {
private static final Logger logger = LoggerFactory.getLogger(logExporter.class);

@Override
public CompletableResultCode export(Collection<SpanData> spans) {
// We always have 32 + 16 + name + several whitespace, 60 seems like an OK initial guess.
StringBuilder sb = new StringBuilder(60);
for (SpanData span : spans) {
sb.setLength(0);
InstrumentationLibraryInfo instrumentationLibraryInfo = span.getInstrumentationLibraryInfo();
sb.append("'")
.append(span.getName())
.append("' : ")
.append(span.getTraceId())
.append(" ")
.append(span.getSpanId())
.append(" ")
.append(span.getKind())
.append(" [tracer: ")
.append(instrumentationLibraryInfo.getName())
.append(":")
.append(
instrumentationLibraryInfo.getVersion() == null
? ""
: instrumentationLibraryInfo.getVersion())
.append("] ")
.append(span.getAttributes());
logger.info(sb.toString());
}
return CompletableResultCode.ofSuccess();
}

/**
* Flushes the data.
* (i guess it is not necessary for slf4j's log)
* @return the result of the operation
*/
@Override
public CompletableResultCode flush() {
CompletableResultCode resultCode = new CompletableResultCode();
// for (Handler handler : logger.getHandlers()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove the unused code

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

// try {
// handler.flush();
// } catch (Throwable t) {
// resultCode.fail();
// }
// }
return resultCode.succeed();
}

@Override
public CompletableResultCode shutdown() {
return flush();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.apache.eventmesh.runtime.exporter;

import io.opentelemetry.exporter.zipkin.ZipkinSpanExporter;

public class zipkinExporter {
private String ip = "localhost";

private int port = 9411;

// Zipkin API Endpoints for uploading spans
private static final String ENDPOINT_V2_SPANS = "/api/v2/spans";

private ZipkinSpanExporter zipkinExporter;

public ZipkinSpanExporter getZipkinExporter() {
String httpUrl = String.format("http://%s:%s", ip, port);
zipkinExporter =
ZipkinSpanExporter.builder().setEndpoint(httpUrl + ENDPOINT_V2_SPANS).build();
return zipkinExporter;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,24 @@
package org.apache.eventmesh.runtime.trace;

import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator;
import io.opentelemetry.context.propagation.ContextPropagators;
import io.opentelemetry.context.propagation.TextMapPropagator;
import io.opentelemetry.exporter.logging.LoggingSpanExporter;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.sdk.trace.SdkTracerProvider;
import io.opentelemetry.sdk.trace.SpanProcessor;
import io.opentelemetry.sdk.trace.export.BatchSpanProcessor;
import io.opentelemetry.sdk.trace.export.SpanExporter;
import org.apache.eventmesh.common.config.CommonConfiguration;
import org.apache.eventmesh.runtime.exporter.logExporter;
import org.apache.eventmesh.runtime.exporter.zipkinExporter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.TimeUnit;
import static io.opentelemetry.api.common.AttributeKey.stringKey;

public class OpenTelemetryTraceFactory {
private static final Logger logger = LoggerFactory.getLogger(OpenTelemetryTraceFactory.class);
Expand All @@ -41,15 +44,18 @@ public class OpenTelemetryTraceFactory {

private SpanExporter spanExporter;

private SpanExporter defaultExporter = new LoggingSpanExporter();
private SpanExporter defaultExporter = new logExporter();

private SpanProcessor spanProcessor;

// Name of the service(using the instrumentationName)
private String SERVICE_NAME;

public OpenTelemetryTraceFactory(CommonConfiguration configuration){
//different spanExporter
try {
String exporterName = configuration.eventMeshTraceExporterType;
spanExporter = (SpanExporter) Class.forName("io.opentelemetry.exporter."+exporterName+"."+exporterName+"SpanExporter").newInstance();
//todo different spanExporter
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no idea how to set different exporter.What should I do?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Different exporter may implement the SpanExporter and you can use the reflection to get different exporter instance

// spanExporter = (SpanExporter) Class.forName("org.apache.eventmesh.runtime.exporter."+exporterName+"Exporter").newInstance();
}catch (Exception ex){
logger.error("fail to set tracer's exporter,due to{}",ex.getMessage());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fail to set tracer's exporter,due to{}
add a blank before{}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

//fail to set the exporter in configuration, changing to use the default Exporter
Expand All @@ -65,8 +71,13 @@ public OpenTelemetryTraceFactory(CommonConfiguration configuration){
.setScheduleDelay(configuration.eventMeshTraceExportInterval, TimeUnit.SECONDS)// set time between two different exports
.build();

//set the trace service's name
Resource serviceNameResource =
Resource.create(Attributes.of(stringKey("service.name"), SERVICE_NAME));

SdkTracerProvider sdkTracerProvider = SdkTracerProvider.builder()
.addSpanProcessor(spanProcessor)
.setResource(Resource.getDefault().merge(serviceNameResource))
.build();

openTelemetry = OpenTelemetrySdk.builder()
Expand All @@ -79,9 +90,10 @@ public OpenTelemetryTraceFactory(CommonConfiguration configuration){

//Gets or creates a named tracer instance
public Tracer getTracer(String instrumentationName){
SERVICE_NAME = instrumentationName;
return openTelemetry.getTracer(instrumentationName);
}

//
//to inject or extract span context
public TextMapPropagator getTextMapPropagator() {return openTelemetry.getPropagators().getTextMapPropagator();}
}