-
Notifications
You must be signed in to change notification settings - Fork 625
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
Changes from 1 commit
dde6ab5
2ed29e2
2293b9c
2c5cc3d
60e9d69
744b16c
7479fd9
53a3fdf
40bb928
69c62bc
da9ed30
3ceb7ee
eea9212
3d7b605
4a29456
66bba23
ac5d236
d22bb45
70499ad
affc589
78e2f62
c1e38d7
1e874ca
5261c1a
0cd4fc4
7de65fc
51ff1e2
be96e82
0440060
4da95e7
e3977fa
d537406
9578c73
31d6a61
9b6e253
4fca695
98d7a3e
037d371
bb0971d
6642680
76b42fd
1ad5545
71d3bbd
96b54b8
6e883b0
d48de99
836b13c
977b942
da634ea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
||
|
@@ -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; | ||
|
@@ -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. | ||
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"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no need to add event about response There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
span.end(); | ||
} | ||
|
||
ctx.writeAndFlush(response).addListener(new ChannelFutureListener() { | ||
@Override | ||
|
@@ -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); | ||
} | ||
}); | ||
|
@@ -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 { | ||
|
@@ -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"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. avoid span event here There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
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()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove the unused code There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
---|---|---|
|
@@ -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); | ||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Different exporter may implement the |
||
// 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()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fail to set tracer's exporter,due to{} There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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() | ||
|
@@ -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();} | ||
} |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed