Skip to content

Commit

Permalink
[DEMO] Core Tracer API adoption for service-a
Browse files Browse the repository at this point in the history
  • Loading branch information
frtu committed Nov 16, 2019
1 parent 194af08 commit 57ee4d9
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
20 changes: 19 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,25 @@ If you only need Jaeger Tracer, just add :
@ComponentScan(basePackages = {"com.github.frtu.logs.tracing.core", "..."})
```

See [sample-microservices/service-a](https://github.com/frtu/log-platform/tree/master/sample-microservices/service-a)
You can create a single Span structure :

```
Span span = tracer.buildSpan("say-hello1").start();
LOGGER.info("hello1");
span.finish();
```

OR a node from a graph using Scope :

```
try (Scope scope = tracer.buildSpan("say-hello2").startActive(true)) {
LOGGER.info("hello2");
}
```


* See [sample-microservices/service-a](https://github.com/frtu/log-platform/tree/master/sample-microservices/service-a)
* Or more at [opentracing.io - span](https://opentracing.io/docs/overview/spans/)

###### b) @ExecutionSpan AOP

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan(basePackages = {"com.github.frtu.logs.example.demo"})
@ComponentScan(basePackages = {"com.github.frtu.logs.tracing.core", "com.github.frtu.logs.example.demo"})
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package com.github.frtu.logs.example.demo;

import com.google.common.collect.ImmutableMap;
import io.opentracing.Scope;
import io.opentracing.Span;
import io.opentracing.Tracer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
Expand All @@ -15,23 +20,41 @@
public class ResourceController {
private static final Logger LOGGER = LoggerFactory.getLogger(ResourceController.class);

@Autowired
private Tracer tracer;

@RequestMapping("/")
@ResponseBody
String home(@RequestParam(value = "service", defaultValue = "ServiceA", required = false) String name) {
Span span = tracer.buildSpan("say-hello1").start();
LOGGER.info("service={}", name);
span.finish();

try (Scope scope = tracer.buildSpan("say-hello2").startActive(true)) {
scope.span().log(ImmutableMap.of("event", "string-format", "value", name));
scope.span().setTag("hello-to", name);

String formatString = formatString(name);
printHello(formatString);

return formatString;
}
}

private String formatString(String helloTo) {
try (Scope scope = tracer.buildSpan("formatString").startActive(true)) {
String helloStr = String.format("Hello, %s!", helloTo);
printHello(helloStr);

scope.span().log(ImmutableMap.of("event", "string-format", "value", helloStr));
return helloStr;
}
}

private void printHello(String helloStr) {
try (Scope scope = tracer.buildSpan("printHello").startActive(true)) {
LOGGER.info(helloStr);
scope.span().log(ImmutableMap.of("event", "println"));
}
}
}

0 comments on commit 57ee4d9

Please sign in to comment.