Skip to content

Commit

Permalink
Added HTTP tracing option (via tracing attribute to HTTPMonitored ann…
Browse files Browse the repository at this point in the history
…otation) to microservices
  • Loading branch information
lafernando committed Mar 1, 2016
1 parent 3596c4b commit 3422d6d
Show file tree
Hide file tree
Showing 9 changed files with 118 additions and 10 deletions.
Binary file modified analytics/das-setup/capps/msf4j_http_monitoring_capp.car
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,7 @@
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
public @interface HTTPMonitored {

boolean tracing() default false;

}
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,14 @@ static synchronized void destroy() {
}

static void publishEvent(HTTPMonitoringEvent httpMonitoringEvent) {
Object[] meta =
new Object[] { httpMonitoringEvent.getTimestamp(), SERVER_HOST_ADDRESS, SERVER_HOSTNAME, MICROSERVICE };
Object[] meta = new Object[4];
meta[0] = httpMonitoringEvent.getTimestamp();
meta[1] = SERVER_HOST_ADDRESS;
meta[2] = SERVER_HOSTNAME;
meta[3] = MICROSERVICE;
Object[] correlation = new Object[2];
correlation[0] = httpMonitoringEvent.getActivityId();
correlation[1] = httpMonitoringEvent.getParentRequest();
Object[] payload = new Object[11];
payload[0] = httpMonitoringEvent.getServiceClass();
payload[1] = httpMonitoringEvent.getServiceName();
Expand All @@ -169,7 +175,8 @@ static void publishEvent(HTTPMonitoringEvent httpMonitoringEvent) {
payload[8] = httpMonitoringEvent.getReferrer();
payload[9] = httpMonitoringEvent.getResponseHttpStatusCode();
payload[10] = httpMonitoringEvent.getResponseTime();
Event event = new Event(HTTP_MONITORING_STREAM_ID, httpMonitoringEvent.getTimestamp(), meta, null, payload);
Event event = new Event(HTTP_MONITORING_STREAM_ID, httpMonitoringEvent.getTimestamp(),
meta, correlation, payload);
dataPublisher.publish(event);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public class HTTPMonitoringEvent {
private String referrer;
private long responseTime;
private long requestSizeBytes;
private String activityId;
private String parentRequest;

public long getTimestamp() {
return timestamp;
Expand Down Expand Up @@ -156,4 +158,20 @@ public void setRequestSizeBytes(long requestSizeBytes) {
this.requestSizeBytes = requestSizeBytes;
}

public String getActivityId() {
return activityId;
}

public void setActivityId(String activityId) {
this.activityId = activityId;
}

public String getParentRequest() {
return parentRequest;
}

public void setParentRequest(String parentRequest) {
this.parentRequest = parentRequest;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import java.lang.reflect.Method;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import javax.ws.rs.Path;
Expand Down Expand Up @@ -62,15 +63,29 @@ public void run() {
});
return this;
}

/**
* Returns the final annotation that is application to the given method. For example,
* the {@link HTTPMonitored} annotation can be mentioned in class level, and also in
* the target method, but the method only have tracing enabled. Then we should get the
* setting as tracing is disabled for that specific method.
*/
private HTTPMonitored extractFinalAnnotation(Method method) {
HTTPMonitored httpMon = method.getAnnotation(HTTPMonitored.class);
if (httpMon == null) {
httpMon = method.getDeclaringClass().getAnnotation(HTTPMonitored.class);
}
return httpMon;
}

@Override
public boolean preCall(HttpRequest request, HttpResponder responder, ServiceMethodInfo serviceMethodInfo) {
Method method = serviceMethodInfo.getMethod();
Interceptor interceptor = map.get(method);
if (interceptor == null) {
if (method.isAnnotationPresent(HTTPMonitored.class)
|| method.getDeclaringClass().isAnnotationPresent(HTTPMonitored.class)) {
interceptor = new HTTPInterceptor();
HTTPMonitored httpMon = this.extractFinalAnnotation(method);
if (httpMon != null) {
interceptor = new HTTPInterceptor(httpMon.tracing());
map.put(method, interceptor);
}
}
Expand All @@ -93,14 +108,50 @@ public void postCall(HttpRequest request, HttpResponseStatus status, ServiceMeth

private class HTTPInterceptor implements Interceptor {

private static final String DEFAULT_TRACE_ID = "DEFAULT";

private static final String DEFAULT_PARENT_REQUEST = "DEFAULT";

private static final String MONITORING_EVENT = "MONITORING_EVENT";

private static final String ACTIVITY_ID = "activity-id";

private static final String PARENT_REQUEST = "parent-request";

private String serviceClass;
private String serviceName;
private String serviceMethod;
private String servicePath;

private boolean tracing;

private HTTPInterceptor() {
private HTTPInterceptor(boolean tracing) {
this.tracing = tracing;
}

public boolean isTracing() {
return tracing;
}

private String generateTraceId() {
return UUID.randomUUID().toString();
}

private void handleTracing(HttpRequest request, HTTPMonitoringEvent httpMonitoringEvent) {
String traceId, parentRequest;
if (this.isTracing()) {
HttpHeaders headers = request.headers();
traceId = headers.get(ACTIVITY_ID);
if (traceId == null) {
traceId = this.generateTraceId();
}
parentRequest = headers.get(PARENT_REQUEST);
} else {
traceId = DEFAULT_TRACE_ID;
parentRequest = DEFAULT_PARENT_REQUEST;
}
httpMonitoringEvent.setActivityId(traceId);
httpMonitoringEvent.setParentRequest(parentRequest);
}

@Override
Expand Down Expand Up @@ -135,6 +186,8 @@ public boolean preCall(HttpRequest request, HttpResponder responder, ServiceMeth
}
httpMonitoringEvent.setReferrer(httpHeaders.get(HttpHeaders.Names.REFERER));

this.handleTracing(request, httpMonitoringEvent);

serviceMethodInfo.setAttribute(MONITORING_EVENT, httpMonitoringEvent);

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,23 @@
<EnableScoreParam>false</EnableScoreParam>
<Type>STRING</Type>
</ColumnDefinition>

<ColumnDefinition>
<Name>correlation_activity_id</Name>
<EnableIndexing>true</EnableIndexing>
<IsPrimaryKey>false</IsPrimaryKey>
<EnableScoreParam>false</EnableScoreParam>
<Type>FACET</Type>
</ColumnDefinition>

<ColumnDefinition>
<Name>correlation_parent_request</Name>
<EnableIndexing>true</EnableIndexing>
<IsPrimaryKey>false</IsPrimaryKey>
<EnableScoreParam>false</EnableScoreParam>
<Type>STRING</Type>
</ColumnDefinition>

<ColumnDefinition>
<Name>service_class</Name>
<EnableIndexing>true</EnableIndexing>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@
"type": "STRING"
}
],
"correlationData": [
{
"name": "activity_id",
"type": "STRING"
},
{
"name": "parent_request",
"type": "STRING"
}
],
"payloadData": [
{
"name": "service_class",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
* Demonstrating the use of Metrics and HTTP Monitoring Annotations.
*/
@Path("/demo")
@HTTPMonitored
@HTTPMonitored (tracing = true)
public class DemoService {

private final Random random = new Random();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ public StudentService() {
@Path("/{nic}")
@Produces("application/json")
@Timed
@HTTPMonitored
@HTTPMonitored (tracing = true)
public Student getStudent(@PathParam("nic") String nic) {
return students.get(nic);
}

@POST
@Consumes("application/json")
@Metered
@HTTPMonitored
@HTTPMonitored (tracing = true)
public void addStudent(Student student) {
students.put(student.getNic(), student);
}
Expand Down

0 comments on commit 3422d6d

Please sign in to comment.