Skip to content

Commit

Permalink
Refactor ratpack client packages
Browse files Browse the repository at this point in the history
  • Loading branch information
jsalinaspolo committed Jan 15, 2022
1 parent 19e1b1a commit ff0b013
Show file tree
Hide file tree
Showing 14 changed files with 67 additions and 156 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.ratpack.client;
package io.opentelemetry.instrumentation.ratpack;

import io.opentelemetry.instrumentation.ratpack.client.internal.ContextHolder;
import io.opentelemetry.instrumentation.ratpack.internal.ContextHolder;
import ratpack.exec.ExecInitializer;
import ratpack.exec.Execution;

public final class OpenTelemetryExecInitializer implements ExecInitializer {
final class OpenTelemetryExecInitializer implements ExecInitializer {
public static final ExecInitializer INSTANCE = new OpenTelemetryExecInitializer();

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.ratpack.client;
package io.opentelemetry.instrumentation.ratpack;

import io.opentelemetry.api.trace.Span;
import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
import io.opentelemetry.instrumentation.ratpack.client.internal.ContextHolder;
import io.opentelemetry.instrumentation.ratpack.internal.ContextHolder;
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
import ratpack.exec.Execution;
import ratpack.http.client.HttpClient;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.ratpack.client;
package io.opentelemetry.instrumentation.ratpack;

import io.opentelemetry.instrumentation.api.instrumenter.http.CapturedHttpHeaders;
import io.opentelemetry.instrumentation.api.instrumenter.http.HttpClientAttributesExtractor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@

import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
import ratpack.exec.ExecInitializer;
import ratpack.exec.ExecInterceptor;
import ratpack.handling.HandlerDecorator;
import ratpack.http.Request;
import ratpack.http.Response;
import ratpack.http.client.HttpClient;
import ratpack.http.client.HttpResponse;
import ratpack.http.client.RequestSpec;
import ratpack.registry.RegistrySpec;

/**
Expand Down Expand Up @@ -42,9 +46,13 @@ public static RatpackTracingBuilder builder(OpenTelemetry openTelemetry) {
}

private final OpenTelemetryServerHandler serverHandler;
private final OpenTelemetryHttpClient httpClientInstrumenter;

RatpackTracing(Instrumenter<Request, Response> serverInstrumenter) {
RatpackTracing(
Instrumenter<Request, Response> serverInstrumenter,
Instrumenter<RequestSpec, HttpResponse> clientInstrumenter) {
serverHandler = new OpenTelemetryServerHandler(serverInstrumenter);
httpClientInstrumenter = new OpenTelemetryHttpClient(clientInstrumenter);
}

/** Returns instance of {@link OpenTelemetryServerHandler} to support Ratpack Registry binding. */
Expand All @@ -57,9 +65,20 @@ public ExecInterceptor getOpenTelemetryExecInterceptor() {
return OpenTelemetryExecInterceptor.INSTANCE;
}

/** Returns instance of {@link ExecInitializer} to support Ratpack Registry binding. */
public ExecInitializer getOpenTelemetryExecInitializer() {
return OpenTelemetryExecInitializer.INSTANCE;
}

/** Configures the {@link RegistrySpec} with OpenTelemetry. */
public void configureServerRegistry(RegistrySpec registry) {
registry.add(HandlerDecorator.prepend(serverHandler));
registry.add(OpenTelemetryExecInterceptor.INSTANCE);
registry.add(OpenTelemetryExecInitializer.INSTANCE);
}

/** Returns instrumented instance of {@link HttpClient} with OpenTelemetry. */
public HttpClient instrumentedHttpClient(HttpClient httpClient) throws Exception {
return httpClientInstrumenter.instrument(httpClient);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@
import io.opentelemetry.instrumentation.api.instrumenter.http.HttpServerMetrics;
import io.opentelemetry.instrumentation.api.instrumenter.http.HttpSpanNameExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.http.HttpSpanStatusExtractor;
import io.opentelemetry.instrumentation.ratpack.internal.RatpackHttpNetAttributesExtractor;
import io.opentelemetry.instrumentation.ratpack.internal.RatpackNetAttributesExtractor;
import java.util.ArrayList;
import java.util.List;
import ratpack.http.Request;
import ratpack.http.Response;
import ratpack.http.client.HttpResponse;
import ratpack.http.client.RequestSpec;

/** A builder for {@link RatpackTracing}. */
public final class RatpackTracingBuilder {
Expand All @@ -30,6 +33,9 @@ public final class RatpackTracingBuilder {
new ArrayList<>();
private CapturedHttpHeaders capturedHttpHeaders = CapturedHttpHeaders.server(Config.get());

private final List<AttributesExtractor<? super RequestSpec, ? super HttpResponse>>
additionalHttpClientExtractors = new ArrayList<>();

RatpackTracingBuilder(OpenTelemetry openTelemetry) {
this.openTelemetry = openTelemetry;
}
Expand All @@ -44,6 +50,12 @@ public RatpackTracingBuilder addAttributeExtractor(
return this;
}

public RatpackTracingBuilder addClientAttributeExtractor(
AttributesExtractor<? super RequestSpec, ? super HttpResponse> attributesExtractor) {
additionalHttpClientExtractors.add(attributesExtractor);
return this;
}

/**
* Configure the instrumentation to capture chosen HTTP request and response headers as span
* attributes.
Expand Down Expand Up @@ -72,6 +84,21 @@ public RatpackTracing build() {
.addRequestMetrics(HttpServerMetrics.get())
.newServerInstrumenter(RatpackGetter.INSTANCE);

return new RatpackTracing(instrumenter);
return new RatpackTracing(instrumenter, httpClientInstrumenter());
}

private Instrumenter<RequestSpec, HttpResponse> httpClientInstrumenter() {
RatpackHttpNetAttributesExtractor netAttributes = new RatpackHttpNetAttributesExtractor();
RatpackHttpClientAttributesExtractor httpAttributes =
new RatpackHttpClientAttributesExtractor(capturedHttpHeaders);

return Instrumenter.<RequestSpec, HttpResponse>builder(
openTelemetry, INSTRUMENTATION_NAME, HttpSpanNameExtractor.create(httpAttributes))
.setSpanStatusExtractor(HttpSpanStatusExtractor.create(httpAttributes))
.addAttributesExtractor(netAttributes)
.addAttributesExtractor(httpAttributes)
.addAttributesExtractors(additionalHttpClientExtractors)
.addRequestMetrics(HttpServerMetrics.get())
.newClientInstrumenter(RequestHeaderSetter.INSTANCE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.ratpack.client;
package io.opentelemetry.instrumentation.ratpack;

import io.opentelemetry.context.propagation.TextMapSetter;
import ratpack.api.Nullable;
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.ratpack.client.internal;
package io.opentelemetry.instrumentation.ratpack.internal;

import io.opentelemetry.context.Context;
import ratpack.http.client.RequestSpec;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.ratpack.client.internal;
package io.opentelemetry.instrumentation.ratpack.internal;

import io.opentelemetry.instrumentation.api.instrumenter.net.NetClientAttributesExtractor;
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ class InstrumentedHttpClientTest extends Specification {
.setPropagators(ContextPropagators.create(W3CTraceContextPropagator.getInstance()))
.setTracerProvider(tracerProvider).build()

def ratpackTracing = RatpackTracing.create(openTelemetry)
def ratpackHttpTracing = RatpackHttpTracing.create(openTelemetry)
RatpackTracing ratpackTracing = RatpackTracing.create(openTelemetry)

def cleanup() {
spanExporter.reset()
Expand All @@ -66,8 +65,7 @@ class InstrumentedHttpClientTest extends Specification {
spec.registry(
Guice.registry { bindings ->
ratpackTracing.configureServerRegistry(bindings)
bindings.bindInstance(ratpackHttpTracing.getOpenTelemetryExecInitializer())
bindings.bindInstance(HttpClient, ratpackHttpTracing.instrumentedHttpClient(HttpClient.of(Action.noop())))
bindings.bindInstance(HttpClient, ratpackTracing.instrumentedHttpClient(HttpClient.of(Action.noop())))
}
)

Expand Down Expand Up @@ -128,8 +126,7 @@ class InstrumentedHttpClientTest extends Specification {
spec.registry(
Guice.registry { bindings ->
ratpackTracing.configureServerRegistry(bindings)
bindings.bindInstance(ratpackHttpTracing.getOpenTelemetryExecInitializer())
bindings.bindInstance(HttpClient, ratpackHttpTracing.instrumentedHttpClient(HttpClient.of(Action.noop())))
bindings.bindInstance(HttpClient, ratpackTracing.instrumentedHttpClient(HttpClient.of(Action.noop())))
}
)

Expand Down Expand Up @@ -194,8 +191,7 @@ class InstrumentedHttpClientTest extends Specification {
spec.registry(
Guice.registry { bindings ->
ratpackTracing.configureServerRegistry(bindings)
bindings.bindInstance(ratpackHttpTracing.getOpenTelemetryExecInitializer())
bindings.bindInstance(HttpClient, ratpackHttpTracing.instrumentedHttpClient(
bindings.bindInstance(HttpClient, ratpackTracing.instrumentedHttpClient(
HttpClient.of { s -> s.readTimeout(Duration.ofMillis(10)) })
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import io.opentelemetry.api.trace.SpanKind
import io.opentelemetry.instrumentation.ratpack.OpenTelemetryServerHandler
import io.opentelemetry.instrumentation.ratpack.RatpackFunctionalTest
import io.opentelemetry.instrumentation.ratpack.RatpackTracing
import io.opentelemetry.instrumentation.ratpack.client.RatpackHttpTracing
import io.opentelemetry.sdk.OpenTelemetrySdk
import io.opentelemetry.sdk.testing.exporter.InMemorySpanExporter
import io.opentelemetry.sdk.trace.SdkTracerProvider
Expand Down Expand Up @@ -126,20 +125,14 @@ class OpenTelemetryModule extends AbstractModule {

@Singleton
@Provides
RatpackHttpTracing ratpackHttpTracing(OpenTelemetry openTelemetry) {
return RatpackHttpTracing.create(openTelemetry)
HttpClient instrumentedHttpClient(RatpackTracing ratpackTracing) {
return ratpackTracing.instrumentedHttpClient(HttpClient.of {})
}

@Singleton
@Provides
HttpClient instrumentedHttpClient(RatpackHttpTracing ratpackHttpTracing) {
return ratpackHttpTracing.instrumentedHttpClient(HttpClient.of {})
}

@Singleton
@Provides
ExecInitializer ratpackExecInitializer(RatpackHttpTracing ratpackHttpTracing) {
return ratpackHttpTracing.getOpenTelemetryExecInitializer()
ExecInitializer ratpackExecInitializer(RatpackTracing ratpackTracing) {
return ratpackTracing.getOpenTelemetryExecInitializer()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import com.google.common.collect.ImmutableList;
import io.netty.channel.ConnectTimeoutException;
import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.ratpack.client.OpenTelemetryExecInitializer;
import io.opentelemetry.instrumentation.testing.junit.http.AbstractHttpClientTest;
import io.opentelemetry.instrumentation.testing.junit.http.HttpClientTestOptions;
import java.net.URI;
Expand Down
Loading

0 comments on commit ff0b013

Please sign in to comment.