Skip to content

Commit

Permalink
Fix 2.x integration (#3036)
Browse files Browse the repository at this point in the history
Found this while testing locally. Prior to 3.4.0 the 2.x integration
didn't require iKey to be configured on the 2.x side (only on the 3.x
side). This broke in 3.4.0, and currently the 2.x integration doesn't
emit any telemetry if there's no iKey configured on the 2.x side.
  • Loading branch information
trask authored May 2, 2023
1 parent 93f6ccb commit 3792327
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import static org.objectweb.asm.Opcodes.CHECKCAST;
import static org.objectweb.asm.Opcodes.DLOAD;
import static org.objectweb.asm.Opcodes.DUP;
import static org.objectweb.asm.Opcodes.GETFIELD;
import static org.objectweb.asm.Opcodes.GOTO;
import static org.objectweb.asm.Opcodes.ICONST_M1;
import static org.objectweb.asm.Opcodes.IFEQ;
Expand All @@ -24,6 +25,7 @@
import static org.objectweb.asm.Opcodes.INVOKESPECIAL;
import static org.objectweb.asm.Opcodes.INVOKESTATIC;
import static org.objectweb.asm.Opcodes.INVOKEVIRTUAL;
import static org.objectweb.asm.Opcodes.IRETURN;
import static org.objectweb.asm.Opcodes.ISTORE;
import static org.objectweb.asm.Opcodes.NEW;
import static org.objectweb.asm.Opcodes.RETURN;
Expand All @@ -35,6 +37,7 @@
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.FieldVisitor;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
Expand Down Expand Up @@ -75,7 +78,13 @@ public byte[] transform(
TelemetryClientClassVisitor cv = new TelemetryClientClassVisitor(cw);
ClassReader cr = new ClassReader(classfileBuffer);
cr.accept(cv, 0);
return cw.toByteArray();
if (cv.foundIsDisabledMethod && !cv.foundConfigurationField) {
logger.error(
"isDisabled() method found in TelemetryClient but configuration field was not found");
return null;
} else {
return cw.toByteArray();
}
} catch (Throwable t) {
logger.error(t.getMessage(), t);
return null;
Expand All @@ -88,11 +97,24 @@ private static class TelemetryClientClassVisitor extends ClassVisitor {

private final ClassWriter cw;

private boolean foundConfigurationField;
private boolean foundIsDisabledMethod;

private TelemetryClientClassVisitor(ClassWriter cw) {
super(ASM9, cw);
this.cw = cw;
}

@Override
public FieldVisitor visitField(
int access, String name, String descriptor, String signature, Object value) {
if (name.equals("configuration")
&& descriptor.equals("L" + unshadedPrefix + "/TelemetryConfiguration;")) {
foundConfigurationField = true;
}
return super.visitField(access, name, descriptor, signature, value);
}

@Override
@Nullable
public MethodVisitor visitMethod(
Expand All @@ -113,6 +135,10 @@ public MethodVisitor visitMethod(
&& descriptor.equals("(L" + unshadedPrefix + "/telemetry/AvailabilityTelemetry;)V")) {
overwriteTrackAvailabilityMethod(mv);
return null;
} else if (name.equals("isDisabled") && descriptor.equals("()Z")) {
foundIsDisabledMethod = true;
overwriteIsDisabledMethod(mv);
return null;
} else if (name.equals("flush") && descriptor.equals("()V")) {
overwriteFlushMethod(mv);
return null;
Expand Down Expand Up @@ -405,6 +431,29 @@ private void overwriteTrackMetricMethod(MethodVisitor mv) {
mv.visitEnd();
}

private void overwriteIsDisabledMethod(MethodVisitor mv) {
mv.visitCode();
Label label0 = new Label();
mv.visitLabel(label0);
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(
GETFIELD,
unshadedPrefix + "/TelemetryClient",
"configuration",
"L" + unshadedPrefix + "/TelemetryConfiguration;");
mv.visitMethodInsn(
INVOKEVIRTUAL,
unshadedPrefix + "/TelemetryConfiguration",
"isTrackingDisabled",
"()Z",
false);
mv.visitInsn(IRETURN);
Label label1 = new Label();
mv.visitLabel(label1);
mv.visitMaxs(1, 1);
mv.visitEnd();
}

private static void overwriteFlushMethod(MethodVisitor mv) {
mv.visitCode();
mv.visitMethodInsn(INVOKESTATIC, BYTECODE_UTIL_INTERNAL_NAME, "flush", "()V", false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

<!-- cannot use ConnectionString here since testing CoreAndFilter against very old SDK
prior to 2.5.1 when when ConnectionString support was introduced -->
<InstrumentationKey>BAD1BAD1-BAD1-BAD1-BAD1-BAD1BAD1BAD1</InstrumentationKey>
<!-- testing to ensure 3.x integration still works even with no InstrumentationKey configured -->
<!--InstrumentationKey>BAD1BAD1-BAD1-BAD1-BAD1-BAD1BAD1BAD1</InstrumentationKey-->

<SDKLogger type="CONSOLE">
<enabled>true</enabled>
Expand Down

0 comments on commit 3792327

Please sign in to comment.