|
5 | 5 |
|
6 | 6 | package io.opentelemetry.instrumentation.ratpack.v1_7.client; |
7 | 7 |
|
8 | | -import io.opentelemetry.api.OpenTelemetry; |
9 | 8 | import io.opentelemetry.api.trace.Span; |
10 | | -import io.opentelemetry.api.trace.Tracer; |
11 | 9 | import io.opentelemetry.context.Context; |
12 | 10 | import io.opentelemetry.context.Scope; |
| 11 | +import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; |
13 | 12 | import java.net.URI; |
14 | 13 | import java.net.URISyntaxException; |
15 | | -import java.util.concurrent.CountDownLatch; |
16 | 14 | import ratpack.exec.Execution; |
17 | 15 | import ratpack.http.client.HttpClient; |
18 | 16 | import ratpack.service.Service; |
19 | 17 | import ratpack.service.StartEvent; |
20 | 18 |
|
21 | 19 | public class BarService implements Service { |
22 | 20 | private final String url; |
23 | | - private final CountDownLatch latch; |
24 | | - private final Tracer tracer; |
| 21 | + // private final CountDownLatch latch; |
| 22 | + private final InstrumentationExtension testing; |
25 | 23 |
|
26 | | - public BarService(CountDownLatch latch, String url, OpenTelemetry openTelemetry) { |
27 | | - this.latch = latch; |
| 24 | + public BarService(String url, InstrumentationExtension testing) { |
| 25 | + // this.latch = latch; |
28 | 26 | this.url = url; |
29 | | - this.tracer = openTelemetry.getTracerProvider().tracerBuilder("testing").build(); |
| 27 | + this.testing = testing; |
30 | 28 | } |
31 | 29 |
|
32 | | - @Override |
33 | | - public void onStart(StartEvent event) { |
| 30 | + protected void generateSpan(StartEvent event) { |
34 | 31 | Context parentContext = Context.current(); |
35 | | - Span span = tracer.spanBuilder("a-span").setParent(parentContext).startSpan(); |
| 32 | + testing.runWithSpan( |
| 33 | + "a-span", |
| 34 | + () -> { |
| 35 | + Span span = Span.current(); |
| 36 | + Context otelContext = parentContext.with(span); |
| 37 | + try (Scope scope = otelContext.makeCurrent()) { |
| 38 | + Execution.current().add(Context.class, otelContext); |
| 39 | + HttpClient httpClient = event.getRegistry().get(HttpClient.class); |
| 40 | + httpClient |
| 41 | + .get(new URI(url)) |
| 42 | + .flatMap(response -> httpClient.get(new URI(url))) |
| 43 | + .then( |
| 44 | + response -> { |
| 45 | + span.end(); |
| 46 | + // latch.countDown(); |
| 47 | + }); |
| 48 | + } catch (URISyntaxException e) { |
| 49 | + throw new RuntimeException(e); |
| 50 | + } |
| 51 | + }); |
| 52 | + } |
36 | 53 |
|
37 | | - Context otelContext = parentContext.with(span); |
38 | | - try (Scope scope = otelContext.makeCurrent()) { |
39 | | - Execution.current().add(Context.class, otelContext); |
40 | | - HttpClient httpClient = event.getRegistry().get(HttpClient.class); |
41 | | - httpClient |
42 | | - .get(new URI(url)) |
43 | | - .flatMap(response -> httpClient.get(new URI(url))) |
44 | | - .then( |
45 | | - response -> { |
46 | | - span.end(); |
47 | | - latch.countDown(); |
48 | | - }); |
49 | | - } catch (URISyntaxException e) { |
50 | | - throw new RuntimeException(e); |
51 | | - } |
| 54 | + @Override |
| 55 | + public void onStart(StartEvent event) { |
| 56 | + generateSpan(event); |
52 | 57 | } |
53 | 58 | } |
0 commit comments