Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restlet instrumentation #3946

Merged
merged 8 commits into from
Sep 14, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions instrumentation/restlet/restlet-1.0/javaagent/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
plugins {
id("otel.javaagent-instrumentation")
}

muzzle {
pass {
group.set("org.restlet")
module.set("org.restlet")
versions.set("[1.0.0, 1.2-M1)")
mateuszrzeszutek marked this conversation as resolved.
Show resolved Hide resolved
assertInverse.set(true)
}
}

repositories {
mavenCentral()
maven("https://maven.restlet.talend.com/")
mavenLocal()
}

dependencies {
api(project(":instrumentation:restlet:restlet-1.0:library"))

library("org.restlet:org.restlet:1.1.5")
library("com.noelios.restlet:com.noelios.restlet:1.1.5")

implementation(project(":instrumentation:restlet:restlet-1.0:library"))
testImplementation(project(":instrumentation:restlet:restlet-1.0:testing"))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.restlet.v1_0;

import com.google.auto.service.AutoService;
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import java.util.Arrays;
import java.util.List;

@AutoService(InstrumentationModule.class)
public class RestletInstrumentationModule extends InstrumentationModule {

public RestletInstrumentationModule() {
super("restlet", "restlet-1.0");
}

@Override
public List<TypeInstrumentation> typeInstrumentations() {
return Arrays.asList(new ServerInstrumentation(), new RouteInstrumentation());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.restlet.v1_0;

import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
import io.opentelemetry.instrumentation.restlet.v1_0.RestletTracing;
import org.restlet.data.Request;
import org.restlet.data.Response;

public final class RestletSingletons {

private static final Instrumenter<Request, Response> INSTRUMENTER =
RestletTracing.create(GlobalOpenTelemetry.get()).getServerInstrumenter();

public static Instrumenter<Request, Response> instrumenter() {
return INSTRUMENTER;
}

private RestletSingletons() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.restlet.v1_0;

import static io.opentelemetry.instrumentation.api.servlet.ServerSpanNaming.Source.CONTROLLER;
import static io.opentelemetry.javaagent.instrumentation.api.Java8BytecodeBridge.currentContext;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;

import io.opentelemetry.instrumentation.api.servlet.ServerSpanNaming;
import io.opentelemetry.instrumentation.restlet.v1_0.internal.RestletServerSpanNaming;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
import org.restlet.Route;
import org.restlet.data.Request;

public class RouteInstrumentation implements TypeInstrumentation {
@Override
public ElementMatcher<TypeDescription> typeMatcher() {
return named("org.restlet.Route");
}

@Override
public void transform(TypeTransformer transformer) {
transformer.applyAdviceToMethod(
isMethod()
.and(named("beforeHandle"))
.and(takesArgument(0, named("org.restlet.data.Request")))
.and(takesArgument(1, named("org.restlet.data.Response"))),
this.getClass().getName() + "$RouteBeforeHandleAdvice");
}

@SuppressWarnings("unused")
public static class RouteBeforeHandleAdvice {

@Advice.OnMethodEnter(suppress = Throwable.class)
public static void getRouteInfo(@Advice.This Route route, @Advice.Argument(0) Request request) {
String pattern = route.getTemplate().getPattern();

ServerSpanNaming.updateServerSpanName(
currentContext(), CONTROLLER, RestletServerSpanNaming.SERVER_SPAN_NAME, pattern);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.restlet.v1_0;

import static io.opentelemetry.javaagent.instrumentation.api.Java8BytecodeBridge.currentContext;
import static io.opentelemetry.javaagent.instrumentation.restlet.v1_0.RestletSingletons.instrumenter;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;

import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
import org.restlet.data.Request;
import org.restlet.data.Response;

public class ServerInstrumentation implements TypeInstrumentation {
@Override
public ElementMatcher<TypeDescription> typeMatcher() {
return named("org.restlet.Server");
}

@Override
public void transform(TypeTransformer transformer) {
transformer.applyAdviceToMethod(
isMethod()
.and(named("handle"))
.and(takesArgument(0, named("org.restlet.data.Request")))
.and(takesArgument(1, named("org.restlet.data.Response"))),
this.getClass().getName() + "$ServerHandleAdvice");
}

@SuppressWarnings("unused")
public static class ServerHandleAdvice {

@Advice.OnMethodEnter(suppress = Throwable.class)
public static void beginRequest(
@Advice.Argument(0) Request request,
@Advice.Argument(1) Response response,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {

Context parentContext = currentContext();

if (!instrumenter().shouldStart(parentContext, request)) {
return;
}

context = instrumenter().start(parentContext, request);
scope = context.makeCurrent();
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void finishRequest(
@Advice.Argument(0) Request request,
@Advice.Argument(1) Response response,
@Advice.Thrown Throwable exception,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {

if (scope == null) {
return;
}

scope.close();

if (exception != null) {
instrumenter().end(context, request, response, exception);
return;
}

// Restlet suppresses exceptions and sets the throwable in status
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Throwable statusThrowable = response.getStatus().getThrowable();

instrumenter().end(context, request, response, statusThrowable);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.restlet.v1_0
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

import io.opentelemetry.instrumentation.restlet.v1_0.AbstractRestletServerTest
import io.opentelemetry.instrumentation.test.AgentTestTrait
import org.restlet.Restlet

class RestletServerTest extends AbstractRestletServerTest implements AgentTestTrait {

@Override
Restlet wrapRestlet(Restlet restlet, String path){
return restlet
}

}
20 changes: 20 additions & 0 deletions instrumentation/restlet/restlet-1.0/library/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
plugins {
id("otel.library-instrumentation")
}

repositories {
mavenCentral()
maven("https://maven.restlet.talend.com/")
mavenLocal()
}

dependencies {

compileOnly("com.google.auto.value:auto-value-annotations")
annotationProcessor("com.google.auto.value:auto-value")

library("org.restlet:org.restlet:1.1.5")
library("com.noelios.restlet:com.noelios.restlet:1.1.5")

testImplementation(project(":instrumentation:restlet:restlet-1.0:testing"))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.restlet.v1_0;

import com.google.auto.value.AutoValue;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;

@AutoValue
abstract class ContextAndScope {

static ContextAndScope create(Context context, Scope scope) {
return new AutoValue_ContextAndScope(context, scope);
}

abstract Context getContext();

abstract Scope getScope();

void close() {
getScope().close();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.restlet.v1_0;

import java.util.Locale;
import org.restlet.data.Form;
import org.restlet.data.Request;

final class HeadersAdapter {

static Form getHeaders(Request request) {
return (Form) request.getAttributes().get("org.restlet.http.headers");
}

static String getValue(Request request, String name) {
Form headers = getHeaders(request);
String value = headers.getFirstValue(name);
if (value != null) {
return value;
}
return headers.getFirstValue(name.toLowerCase(Locale.ROOT));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.restlet.v1_0;

import io.opentelemetry.context.propagation.TextMapGetter;
import org.restlet.data.Request;

final class RestletHeadersGetter implements TextMapGetter<Request> {

@Override
public Iterable<String> keys(Request carrier) {
return HeadersAdapter.getHeaders(carrier).getNames();
}

@Override
public String get(Request carrier, String key) {
return HeadersAdapter.getValue(carrier, key);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd suggest inlining the HeadersAdapter methods and deleting the HeadersAdapter class

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, I think it's not necessary to keep them separate

}
Loading