Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
51 changes: 51 additions & 0 deletions instrumentation/thrift/thrift-0.9.1/javaagent/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
plugins {
id("otel.javaagent-instrumentation")
}

muzzle {
pass {
group.set("org.apache.thrift")
module.set("libthrift")
versions.set("[0.9.1,)")
Copy link
Contributor

Choose a reason for hiding this comment

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

usually we also add assertInverse.set(true) to verify that instrumentation does not apply on earlier versions

}
}
val thriftExecutable = "./src/test/resources/thrift"
val thriftInputFile = "$projectDir/src/test/resources/ThriftService.thrift"
val thriftOutputDir = "$projectDir/src/test/java"

var generateThrift = tasks.register<Exec>("generateThrift") {
group = "build"
description = "Generate Java code from Thrift IDL files"
commandLine(thriftExecutable, "--gen", "java", "-out", thriftOutputDir, thriftInputFile)
Copy link
Contributor

Choose a reason for hiding this comment

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

Binaries shouldn't be really added to the source code repository, there is even a scorecard check for that https://github.com/ossf/scorecard/blob/cd152cb6742c5b8f2f3d2b5193b41d9c50905198/docs/checks.md#binary-artifacts Also since the project needs to build on at least windows, linux and mac just having one binary wouldn't work. Currently code generation seems to fail https://scans.gradle.com/s/73advrdsyn2fk/console-log/task/:instrumentation:thrift:thrift-0.9.1:javaagent:generateThrift?anchor=1&page=1 I would have hoped that the compiler binaries would be available for download like for the gRPC but that doesn't seem to be the case.
I think it would be best to go back to adding the generated java files to the repo for now. We can work on automating the generation later when other issues have been sorted. One way we could do the generation would be to have the thrift compiler in a docker image and run that image during the build similarly to how semconv repo runs weaver https://github.com/open-telemetry/semantic-conventions-java/blob/36743b068e1d3bbff129b528a340a63fb544e390/build.gradle.kts#L86 An example of such image can be found from https://github.com/reddit/thrift-compiler/pkgs/container/thrift-compiler If we can't find an existing image for the version we need we can build it ourself. @open-telemetry/java-instrumentation-approvers do you have any suggestions? Would having the thrift compiler in docker image be worth the effort?

}

tasks.named<JavaCompile>("compileTestJava") {
dependsOn(generateThrift)

doFirst {
source.forEach { file ->
if (file.absolutePath.contains("$thriftOutputDir/io/opentelemetry/javaagent/instrumentation/thrift/v0_9_1/thrift/ThriftService.java")) {
options.compilerArgs.add("-nowarn")
options.compilerArgs.add("-Xlint:-unchecked")
}
}
}
}

tasks.named<Checkstyle>("checkstyleTest") {
exclude("**/thrift/ThriftService.java")
}

spotless {
java {
targetExclude("**/thrift/ThriftService.java")
}
}

dependencies {
compileOnly("org.apache.thrift:libthrift:0.9.1")
Copy link
Contributor

Choose a reason for hiding this comment

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

you should use library instead of compileOnly. library is a custom scope that is the same as compileOnly + testImplementation. Additionally when running tests with -PtestLatestDeps=true the version for library dependency is replaced with the latest available version.

implementation(project(":instrumentation:thrift:thrift-common:library"))

testImplementation("org.apache.thrift:libthrift:0.9.1")
testImplementation("javax.annotation:javax.annotation-api:1.3.2")
}
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.javaagent.instrumentation.thrift.v0_9_1;

import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.protocol.TProtocolDecorator;

/**
* Note that the 8888th field of record is reserved for transporting trace header. Because Thrift
* doesn't support to transport metadata.
*/
public abstract class AbstractProtocolWrapper extends TProtocolDecorator {
Copy link
Contributor

Choose a reason for hiding this comment

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

public static final String OT_MAGIC_FIELD = "OT_MAGIC_FIELD";
public static final short OT_MAGIC_FIELD_ID = 8888;

public AbstractProtocolWrapper(TProtocol protocol) {
super(protocol);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.thrift.v0_9_1;

import static io.opentelemetry.javaagent.instrumentation.thrift.v0_9_1.ThriftSingletons.clientInstrumenter;
import static io.opentelemetry.javaagent.instrumentation.thrift.v0_9_1.ThriftSingletons.serverInstrumenter;

import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.thrift.common.RequestScopeContext;
import io.opentelemetry.instrumentation.thrift.common.ThriftRequest;
import org.apache.thrift.async.AsyncMethodCallback;

public final class AsyncMethodCallbackWrapper<T> implements AsyncMethodCallback<T> {
private final AsyncMethodCallback<T> delegate;
private RequestScopeContext requestScopeContext;
private final boolean isServer;

public AsyncMethodCallbackWrapper(AsyncMethodCallback<T> methodCallback, boolean isServer) {
this.delegate = methodCallback;
this.isServer = isServer;
}

public void setRequestScopeContext(RequestScopeContext requestScopeContext) {
this.requestScopeContext = requestScopeContext;
}

@Override
public void onComplete(T t) {
try {
if (this.requestScopeContext == null) {
return;
}
this.requestScopeContext.close();
Context context = this.requestScopeContext.getContext();
ThriftRequest request = this.requestScopeContext.getRequest();
if (isServer) {
serverInstrumenter().end(context, request, 0, null);
} else {
clientInstrumenter().end(context, request, 0, null);
}
} finally {
this.delegate.onComplete(t);
}
}

@Override
public void onError(Exception e) {
try {
if (this.requestScopeContext == null) {
return;
}
this.requestScopeContext.close();
Context context = this.requestScopeContext.getContext();
ThriftRequest request = this.requestScopeContext.getRequest();
if (isServer) {
serverInstrumenter().end(context, request, 1, e);
} else {
clientInstrumenter().end(context, request, 1, e);
}
} finally {
this.delegate.onError(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.thrift.v0_9_1;

import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
import io.opentelemetry.instrumentation.thrift.common.ThriftInstrumenterFactory;
import io.opentelemetry.instrumentation.thrift.common.ThriftRequest;

public final class ThriftSingletons {
private static final String INSTRUMENTATION_NAME = "io.opentelemetry.thrift-0.9.1";

private static final Instrumenter<ThriftRequest, Integer> CLIENT_INSTRUMENTER =
ThriftInstrumenterFactory.clientInstrumenter(INSTRUMENTATION_NAME);
private static final Instrumenter<ThriftRequest, Integer> SERVER_INSTRUMENTER =
ThriftInstrumenterFactory.serverInstrumenter(INSTRUMENTATION_NAME);

public static Instrumenter<ThriftRequest, Integer> clientInstrumenter() {
return CLIENT_INSTRUMENTER;
}

public static Instrumenter<ThriftRequest, Integer> serverInstrumenter() {
return SERVER_INSTRUMENTER;
}

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

package io.opentelemetry.javaagent.instrumentation.thrift.v0_9_1.client;

import static io.opentelemetry.javaagent.instrumentation.thrift.v0_9_1.ThriftSingletons.clientInstrumenter;

import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.thrift.common.RequestScopeContext;
import io.opentelemetry.instrumentation.thrift.common.SocketAccessor;
import io.opentelemetry.instrumentation.thrift.common.ThriftRequest;
import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge;
import io.opentelemetry.javaagent.instrumentation.thrift.v0_9_1.AbstractProtocolWrapper;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TField;
import org.apache.thrift.protocol.TMap;
import org.apache.thrift.protocol.TMessage;
import org.apache.thrift.protocol.TMessageType;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.protocol.TType;
import org.apache.thrift.transport.TTransport;

@SuppressWarnings("all")
Copy link
Contributor

Choose a reason for hiding this comment

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

instead of suppressing warnings we prefer to fix the code so that it wouldn't have the warnings

public final class ClientOutProtocolWrapper extends AbstractProtocolWrapper {
public static final String ONE_WAY_METHOD_NAME_PREFIX = "recv_";
private volatile RequestScopeContext requestScopeContext;
public TTransport transport;
private boolean injected = true;
private String methodName;
private final Set<String> voidMethodNames;
private String serviceName;
private byte type = -1;
private byte originType;

public ClientOutProtocolWrapper(
TProtocol protocol, String serviceName, Set<String> voidMethodNames) {
super(protocol);
this.serviceName = serviceName;
this.voidMethodNames = voidMethodNames;
}

@Override
public void writeMessageBegin(TMessage message) throws TException {
this.injected = false;
this.methodName = message.name;
this.originType = message.type;
// Compatible with version 0.9.1 and 0.9.2 asynchronous logic
if (message.type == TMessageType.ONEWAY || this.type == -1) {
this.type = message.type;
}
if (!this.isOneway()) {
if (this.voidMethodNames != null
&& this.voidMethodNames.contains(this.methodName)
&& !this.voidMethodNames.contains(ONE_WAY_METHOD_NAME_PREFIX + this.methodName)) {
this.type = TMessageType.ONEWAY;
}
}
try {
if (this.requestScopeContext == null) {
Socket socket = SocketAccessor.getSocket(super.getTransport());
if (socket == null) {
socket = SocketAccessor.getSocket(this.transport);
}
ThriftRequest request =
ThriftRequest.create(this.serviceName, this.methodName, socket, new HashMap<>());
Context parentContext = Java8BytecodeBridge.currentContext();
if (!clientInstrumenter().shouldStart(parentContext, request)) {
return;
}
Context context = clientInstrumenter().start(parentContext, request);
this.requestScopeContext = RequestScopeContext.create(request, null, context);
}
} finally {
if (this.isOneway() && message.type != TMessageType.ONEWAY) {
TMessage onewayMessage = new TMessage(message.name, TMessageType.ONEWAY, message.seqid);
super.writeMessageBegin(onewayMessage);
} else {
super.writeMessageBegin(message);
}
Comment on lines +80 to +85
Copy link
Contributor

Choose a reason for hiding this comment

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

Usually instrumentation should not affect how library works. You need to add a comment that explains why this is needed and why it is okay to do this.

}
}

@Override
public void writeFieldStop() throws TException {
try {
if (!this.injected && this.requestScopeContext != null) {
ThriftRequest request = this.requestScopeContext.getRequest();
this.writeHeader(request.getHeader());
}
} finally {
this.injected = true;
super.writeFieldStop();
}
}

public void writeHeader(Map<String, String> header) throws TException {
super.writeFieldBegin(new TField(OT_MAGIC_FIELD, TType.MAP, OT_MAGIC_FIELD_ID));
super.writeMapBegin(new TMap(TType.STRING, TType.STRING, header.size()));

Set<Map.Entry<String, String>> entries = header.entrySet();
for (Map.Entry<String, String> entry : entries) {
super.writeString(entry.getKey());
super.writeString(entry.getValue());
}

super.writeMapEnd();
super.writeFieldEnd();
}

public boolean isOneway() {
return this.type == TMessageType.ONEWAY;
}

public boolean isChangeToOneway() {
return this.type != this.originType;
}

public void updateTransport(TTransport transport) {
this.transport = transport;
}

public RequestScopeContext getRequestScopeContext() {
return requestScopeContext;
}

public void setRequestScopeContext(RequestScopeContext requestScopeContext) {
this.requestScopeContext = requestScopeContext;
}

public void setServiceName(String serviceName) {
this.serviceName = serviceName;
}

public void setType(byte type) {
this.type = type;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.thrift.v0_9_1.client;

import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.protocol.TProtocolFactory;
import org.apache.thrift.transport.TTransport;

@SuppressWarnings({"serial"})
Copy link
Contributor

Choose a reason for hiding this comment

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

why not just add the serial version uid?

public final class ClientProtocolFactoryWrapper implements TProtocolFactory {
public TProtocolFactory delegate;
public TTransport transport;
public String serviceName;

@Override
public TProtocol getProtocol(TTransport transport) {
TProtocol protocol = this.delegate.getProtocol(transport);
if (protocol instanceof ClientOutProtocolWrapper) {
if (transport != null) {
((ClientOutProtocolWrapper) protocol).updateTransport(this.transport);
}
((ClientOutProtocolWrapper) protocol).setServiceName(this.serviceName);
return protocol;
}
protocol = new ClientOutProtocolWrapper(protocol, this.serviceName, null);
if (transport != null) {
((ClientOutProtocolWrapper) protocol).updateTransport(this.transport);
}
return protocol;
}

public ClientProtocolFactoryWrapper(
TProtocolFactory protocolFactory, TTransport transport, String serviceName) {
this.delegate = protocolFactory;
this.transport = transport;
this.serviceName = serviceName;
}
}
Loading
Loading