Skip to content

Commit

Permalink
added support for 1.7
Browse files Browse the repository at this point in the history
  • Loading branch information
dhilpipre committed May 15, 2024
1 parent 1918e1d commit 2e68dd5
Show file tree
Hide file tree
Showing 27 changed files with 1,373 additions and 0 deletions.
37 changes: 37 additions & 0 deletions Kotlin-Coroutines_1.7/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

// Build.gradle generated for instrumentation module Kotlin-Coroutines_1.2

apply plugin: 'java'

targetCompatibility = JavaVersion.VERSION_1_9

dependencies {
implementation group: 'org.jetbrains.kotlinx', name: 'kotlinx-coroutines-core', version: '1.7.0'

// New Relic Java Agent dependencies
implementation 'com.newrelic.agent.java:newrelic-agent:6.0.0'
implementation 'com.newrelic.agent.java:newrelic-api:6.0.0'
implementation fileTree(include: ['*.jar'], dir: '../libs')
}

jar {
manifest {
attributes 'Implementation-Title': 'com.newrelic.instrumentation.labs.Kotlin-Coroutines_1.7'
attributes 'Implementation-Vendor': 'New Relic Labs'
attributes 'Implementation-Vendor-Id': 'com.newrelic.labs'
attributes 'Implementation-Version': 1.0
}
}

verifyInstrumentation {
passes 'org.jetbrains.kotlinx:kotlinx-coroutines-core:[1.7.0,)'
passes 'org.jetbrains.kotlinx:kotlinx-coroutines-core-jvm:[1.7.0,)'
excludeRegex '.*SNAPSHOT'
excludeRegex '.*alpha'
excludeRegex '.*-eap-.*'
excludeRegex '.*-native-.*'
excludeRegex '.*-M[0-9]'
excludeRegex '.*-rc'
excludeRegex '.*-RC'
excludeRegex '.*-Beta'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.newrelic.instrumentation.kotlin.coroutines;

import com.newrelic.agent.bridge.AgentBridge;
import com.newrelic.api.agent.NewRelic;
import com.newrelic.api.agent.Token;
import com.newrelic.api.agent.Trace;

import kotlin.coroutines.Continuation;
import kotlin.coroutines.CoroutineContext;

public class NRContinuationWrapper<T> implements Continuation<T> {

private Continuation<T> delegate = null;
private String name = null;
private static boolean isTransformed = false;

public NRContinuationWrapper(Continuation<T> d, String n) {
delegate = d;
name = n;
if(!isTransformed) {
AgentBridge.instrumentation.retransformUninstrumentedClass(getClass());
isTransformed = true;
}
}

@Override
public CoroutineContext getContext() {
return delegate.getContext();
}

@Override
@Trace(async=true)
public void resumeWith(Object p0) {

NewRelic.getAgent().getTracedMethod().setMetricName("Custom","ContinuationWrapper","resumeWith",name != null ? name : Utils.getCoroutineName(getContext(), delegate.getClass()));
Token token = Utils.getToken(getContext());
if(token != null) {
token.link();
}
delegate.resumeWith(p0);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.newrelic.instrumentation.kotlin.coroutines;

import com.newrelic.api.agent.Token;

import kotlin.coroutines.AbstractCoroutineContextElement;
import kotlin.coroutines.CoroutineContext;

public class NRCoroutineToken extends AbstractCoroutineContextElement
{
public static Key key = new Key();

public NRCoroutineToken(Token t) {
super(key);
token = t;
}

private Token token = null;

public static final class Key implements CoroutineContext.Key<NRCoroutineToken> {
private Key() {}
}

public Token getToken() {
return token;
}

@Override
public int hashCode() {
return token.hashCode();
}

@Override
public boolean equals(Object obj) {
if(this != obj ) {
if(obj instanceof NRCoroutineToken) {
NRCoroutineToken t = (NRCoroutineToken)obj;
return t.token == token;
}
} else {
return true;
}
return false;
}

@Override
public String toString() {
return "NRCoroutineToken";
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.newrelic.instrumentation.kotlin.coroutines;

import com.newrelic.agent.bridge.AgentBridge;
import com.newrelic.api.agent.NewRelic;
import com.newrelic.api.agent.Trace;

import kotlin.jvm.functions.Function1;

public class NRFunction1Wrapper<P1,R> implements Function1<P1, R> {

private Function1<P1, R> delegate = null;
private String name = null;
private static boolean isTransformed = false;

public NRFunction1Wrapper(Function1<P1, R> d, String n) {
delegate = d;
name = n;
if(!isTransformed) {
isTransformed = true;
AgentBridge.instrumentation.retransformUninstrumentedClass(getClass());
}
}

@Override
@Trace(dispatcher=true)
public R invoke(P1 p1) {
if(name != null) NewRelic.getAgent().getTracedMethod().setMetricName("Custom","WrappedSuspend",name);
if(delegate != null) {
return delegate.invoke(p1);
}
return null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.newrelic.instrumentation.kotlin.coroutines;

import com.newrelic.agent.bridge.AgentBridge;
import com.newrelic.api.agent.NewRelic;
import com.newrelic.api.agent.Token;
import com.newrelic.api.agent.Trace;

import kotlin.coroutines.Continuation;
import kotlin.jvm.functions.Function2;

public class NRFunction2Wrapper<P1, P2, R> implements Function2<P1, P2, R> {

private Function2<P1, P2, R> delegate = null;
private String name = null;
private static boolean isTransformed = false;
public Token token = null;

public NRFunction2Wrapper(Function2<P1, P2, R> d,String n) {
delegate = d;
name = n;
if(!isTransformed) {
isTransformed = true;
AgentBridge.instrumentation.retransformUninstrumentedClass(getClass());
}
}

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
@Trace(async=true)
public R invoke(P1 p1, P2 p2) {
if(token != null) {
token.linkAndExpire();
token = null;
}
String nameStr = null;
if(p2 instanceof Continuation) {
Continuation continuation = (Continuation)p2;

if (!Utils.ignoreContinuation(continuation.getClass(), continuation.getContext())) {
NRContinuationWrapper wrapper = new NRContinuationWrapper(continuation, name);
p2 = (P2) wrapper;
}
}
if(nameStr == null) {
nameStr = name;
}
if(nameStr != null) {
NewRelic.getAgent().getTracedMethod().setMetricName("Custom","WrappedSuspend",nameStr);
}
if(delegate != null) {
return delegate.invoke(p1, p2);
}
return null;
}

}
Loading

0 comments on commit 2e68dd5

Please sign in to comment.