Skip to content

Commit b969e6d

Browse files
edaenabnookala
authored andcommitted
Add jaeger and logs in the CustomEndpointController (#2)
1 parent b669243 commit b969e6d

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

api/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
2323
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
2424
<java.version>1.8</java.version>
25+
<jaeger.version>0.32.0</jaeger.version>
2526
</properties>
2627

2728
<dependencies>
@@ -77,6 +78,12 @@
7778
<version>2.0.0</version>
7879
</dependency>
7980

81+
<dependency>
82+
<groupId>io.jaegertracing</groupId>
83+
<artifactId>jaeger-client</artifactId>
84+
<version>${jaeger.version}</version>
85+
</dependency>
86+
8087
</dependencies>
8188

8289
<repositories>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.microsoft.cse.reference.spring.dal;
2+
3+
import io.jaegertracing.Configuration;
4+
import io.jaegertracing.Configuration.ReporterConfiguration;
5+
import io.jaegertracing.Configuration.SamplerConfiguration;
6+
import io.jaegertracing.internal.JaegerTracer;
7+
8+
9+
/**
10+
* Helper functions for tracing and logs
11+
*/
12+
public class JaegerTracerHelper {
13+
14+
/**
15+
* Initializes a Jaeger tracer
16+
* @param service the name of the service
17+
* @return the Jaeger tracer
18+
*/
19+
public static JaegerTracer initTracer(String service) {
20+
SamplerConfiguration samplerConfig = SamplerConfiguration.fromEnv().withType("const").withParam(1);
21+
ReporterConfiguration reporterConfig = ReporterConfiguration.fromEnv().withLogSpans(true);
22+
Configuration config = new Configuration(service).withSampler(samplerConfig).withReporter(reporterConfig);
23+
return config.getTracer();
24+
}
25+
}

api/src/main/java/com/microsoft/cse/reference/spring/dal/controllers/CustomEndpointController.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.microsoft.cse.reference.spring.dal.controllers;
22

3+
import com.microsoft.cse.reference.spring.dal.JaegerTracerHelper;
34
import com.microsoft.cse.reference.spring.dal.converters.IntegerToBoolean;
45
import com.microsoft.cse.reference.spring.dal.converters.EmptyStringToNull;
56
import com.microsoft.cse.reference.spring.dal.models.PrincipalWithName;
@@ -15,6 +16,11 @@
1516
import static java.util.Arrays.asList;
1617
import static org.springframework.data.mongodb.core.aggregation.Aggregation.*;
1718

19+
import io.opentracing.Span;
20+
import io.opentracing.Tracer;
21+
22+
import com.google.common.collect.ImmutableMap;
23+
1824
/**
1925
*
2026
* Create a custom controller so that when a user hits a URL that's not formatted like the search endpoint
@@ -25,9 +31,11 @@ public class CustomEndpointController {
2531
private IntegerToBoolean integerToBoolean = new IntegerToBoolean();
2632
private EmptyStringToNull emptyStringToNull = new EmptyStringToNull();
2733
private MongoTemplate mongoTemplate;
34+
private final Tracer tracer;
2835

2936
public CustomEndpointController(MongoTemplate mongoTemplate) {
3037
this.mongoTemplate = mongoTemplate;
38+
this.tracer = JaegerTracerHelper.initTracer("custom-endpoint-controller");
3139
}
3240

3341
/**
@@ -57,6 +65,9 @@ public CustomEndpointController(MongoTemplate mongoTemplate) {
5765
*/
5866
@RequestMapping(method = RequestMethod.GET, value = "/people/{nconst}/titles")
5967
public List<Title> getAllTitles(@PathVariable String nconst) {
68+
Span span = tracer.buildSpan("get-person-titles").start();
69+
span.log(ImmutableMap.of("event", "query-person-titles", "value", nconst));
70+
6071
MatchOperation filterByNconst = match(Criteria.where("nconst").is(nconst));
6172

6273
LookupOperation titleLookup = LookupOperation.newLookup()
@@ -73,6 +84,8 @@ public List<Title> getAllTitles(@PathVariable String nconst) {
7384

7485
AggregationResults<Document> aggregationResults = mongoTemplate.aggregate(aggregation, "principals_mapping", Document.class);
7586

87+
span.finish();
88+
7689
return documentToTitleList(aggregationResults);
7790
}
7891

@@ -86,6 +99,9 @@ public List<Title> getAllTitles(@PathVariable String nconst) {
8699
*/
87100
@RequestMapping(method = RequestMethod.GET, value = "/titles/{tconst}/people")
88101
public List<PrincipalWithName> getAllPeople(@PathVariable String tconst) {
102+
Span span = tracer.buildSpan("get-people-from-title").start();
103+
span.log(ImmutableMap.of("event", "query-people-from-title", "value", tconst));
104+
89105
MatchOperation filterByNconst = match(Criteria.where("tconst").is(tconst));
90106
LookupOperation nameLookup = LookupOperation.newLookup()
91107
.from("names")
@@ -106,6 +122,8 @@ public List<PrincipalWithName> getAllPeople(@PathVariable String tconst) {
106122
p.person.remove("_id");
107123
}
108124

125+
span.finish();
126+
109127
return mappedResults;
110128
}
111129

0 commit comments

Comments
 (0)