-
Notifications
You must be signed in to change notification settings - Fork 147
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
21 changed files
with
750 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
This module instruments `TraceOps#txn` call in the newrelic zio 2 scala api. | ||
This is used instead of the `@Trace` annotation which eagerly starts the Transcaction. This instrumentation takes | ||
care of the lazy ZIO structure created in ZIO |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
apply plugin: 'scala' | ||
|
||
isScalaProjectEnabled(project, "scala-2.13") | ||
|
||
dependencies { | ||
implementation(project(":agent-bridge")) | ||
implementation(project(":newrelic-weaver-api")) | ||
implementation(project(":newrelic-weaver-scala-api")) | ||
implementation(project(":newrelic-scala-zio2-api")) | ||
implementation("org.scala-lang:scala-library:2.13.3") | ||
implementation("dev.zio:zio_2.13:2.0.0") | ||
} | ||
|
||
jar { | ||
manifest { attributes 'Implementation-Title': 'com.newrelic.instrumentation.newrelic-scala-zio2-api', | ||
'Implementation-Title-Alias': 'newrelic-scala-zio2-api_instrumentation' } | ||
} | ||
|
||
verifyInstrumentation { | ||
verifyClasspath = false //can't verify, newrelic-scala-zio2-api is a sub project | ||
} |
24 changes: 24 additions & 0 deletions
24
instrumentation/newrelic-scala-zio2-api/src/main/scala/com/newrelic/zio2/api/Util.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.newrelic.zio2.api | ||
|
||
import com.newrelic.agent.bridge.AgentBridge | ||
import zio._ | ||
|
||
object Util { | ||
def wrapTrace[R, E, A](body: ZIO[R, E, A]): ZIO[R, E, A] = { | ||
ZIO.attempt(AgentBridge.instrumentation.createScalaTxnTracer) | ||
.foldZIO(_ => body, | ||
tracer => if (tracer == null) { | ||
body | ||
} else { | ||
body.mapBoth( | ||
error => { | ||
tracer.finish(new Throwable("ZIO txn body fail")) | ||
error | ||
}, | ||
success => { | ||
tracer.finish(172, null) | ||
success | ||
}) | ||
}) | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...lic-scala-zio2-api/src/main/scala/com/newrelic/zio2/api/ZIOTraceOps_Instrumentation.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.newrelic.zio2.api | ||
|
||
import com.newrelic.api.agent.weaver.Weaver | ||
import com.newrelic.api.agent.weaver.scala.{ScalaMatchType, ScalaWeave} | ||
import zio.ZIO | ||
|
||
|
||
@ScalaWeave(`type` = ScalaMatchType.Object, `originalName` = "com.newrelic.zio2.api.TraceOps") | ||
class ZIOTraceOps_Instrumentation { | ||
def txn[R, E, A](body: ZIO[R, E, A]): ZIO[R, E, A] = Util.wrapTrace(Weaver.callOriginal) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
apply plugin: 'scala' | ||
|
||
isScalaProjectEnabled(project, "scala-2.13") | ||
|
||
dependencies { | ||
implementation("dev.zio:zio_2.13:2.0.13") | ||
implementation(project(":agent-bridge")) | ||
implementation(project(":newrelic-weaver-scala-api")) | ||
implementation(project(":newrelic-weaver-api")) | ||
implementation('org.scala-lang:scala-library:2.13.10') | ||
} | ||
|
||
jar { | ||
manifest { attributes 'Implementation-Title': 'com.newrelic.instrumentation.zio', | ||
'Implementation-Title-Alias': 'zio_instrumentation' } | ||
This comment has been minimized.
Sorry, something went wrong. |
||
} | ||
|
||
verifyInstrumentation { | ||
passes 'dev.zio:zio_2.13:[2.0.0,)' | ||
} | ||
|
||
site { | ||
title 'Scala' | ||
type 'Other' | ||
} |
42 changes: 42 additions & 0 deletions
42
instrumentation/zio-2/src/main/scala/zio/TokenAwareRunnable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* | ||
* * Copyright 2024 New Relic Corporation. All rights reserved. | ||
* * SPDX-License-Identifier: Apache-2.0 | ||
* | ||
*/ | ||
|
||
package zio; | ||
|
||
import com.newrelic.agent.bridge.AgentBridge; | ||
|
||
import static zio.Utils.getThreadTokenAndRefCount; | ||
import static zio.Utils.clearThreadTokenAndRefCountAndTxn; | ||
import static zio.Utils.setThreadTokenAndRefCount; | ||
import static zio.Utils.logTokenInfo; | ||
|
||
|
||
public final class TokenAwareRunnable implements Runnable { | ||
private final Runnable delegate; | ||
private AgentBridge.TokenAndRefCount tokenAndRefCount; | ||
|
||
public TokenAwareRunnable(Runnable delegate) { | ||
this.delegate = delegate; | ||
//get token state from calling Thread | ||
this.tokenAndRefCount = getThreadTokenAndRefCount(); | ||
logTokenInfo( tokenAndRefCount, "TokenAwareRunnable token info set"); | ||
} | ||
|
||
@Override | ||
public void run() { | ||
try { | ||
if (delegate != null) { | ||
logTokenInfo(tokenAndRefCount, "Token info set in thread"); | ||
setThreadTokenAndRefCount(tokenAndRefCount); | ||
delegate.run(); | ||
} | ||
} finally { | ||
logTokenInfo(tokenAndRefCount, "Clearing token info from thread "); | ||
clearThreadTokenAndRefCountAndTxn(tokenAndRefCount); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* | ||
* * Copyright 2024 New Relic Corporation. All rights reserved. | ||
* * SPDX-License-Identifier: Apache-2.0 | ||
* | ||
*/ | ||
|
||
package zio; | ||
|
||
import java.text.MessageFormat; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
import java.util.logging.Level; | ||
|
||
import com.newrelic.agent.bridge.AgentBridge; | ||
import com.newrelic.agent.bridge.Transaction; | ||
|
||
public class Utils { | ||
|
||
public static AgentBridge.TokenAndRefCount getThreadTokenAndRefCount() { | ||
AgentBridge.TokenAndRefCount tokenAndRefCount = AgentBridge.activeToken.get(); | ||
// Used to be that if the tokenAndRefCount is not null, we increment the counter and then return the tokenAndRefCount | ||
if (tokenAndRefCount == null) { | ||
Transaction tx = AgentBridge.getAgent().getTransaction(false); | ||
if (tx != null) { | ||
tokenAndRefCount = new AgentBridge.TokenAndRefCount(tx.getToken(), | ||
AgentBridge.getAgent().getTracedMethod(), new AtomicInteger(1)); | ||
} | ||
} else { | ||
tokenAndRefCount.refCount.incrementAndGet(); | ||
} | ||
|
||
return tokenAndRefCount; | ||
} | ||
|
||
public static void setThreadTokenAndRefCount(AgentBridge.TokenAndRefCount tokenAndRefCount) { | ||
if (tokenAndRefCount != null) { | ||
AgentBridge.activeToken.set(tokenAndRefCount); | ||
tokenAndRefCount.token.link(); | ||
} | ||
} | ||
|
||
public static void clearThreadTokenAndRefCountAndTxn(AgentBridge.TokenAndRefCount tokenAndRefCount) { | ||
AgentBridge.activeToken.remove(); | ||
if (tokenAndRefCount != null && tokenAndRefCount.refCount.decrementAndGet() <= 0) { | ||
tokenAndRefCount.token.expire(); | ||
tokenAndRefCount.token = null; | ||
} | ||
} | ||
|
||
public static void logTokenInfo(AgentBridge.TokenAndRefCount tokenAndRefCount, String msg) { | ||
if (AgentBridge.getAgent().getLogger().isLoggable(Level.FINEST)) { | ||
String tokenMsg = (tokenAndRefCount != null && tokenAndRefCount.token != null) | ||
? String.format("[%s:%s:%d]", tokenAndRefCount.token, tokenAndRefCount.token.getTransaction(), | ||
tokenAndRefCount.refCount.get()) | ||
: "[Empty token]"; | ||
AgentBridge.getAgent().getLogger().log(Level.FINEST, MessageFormat.format("{0}: token info {1}", tokenMsg, msg)); | ||
} | ||
} | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
instrumentation/zio-2/src/main/scala/zio/ZIOExecutor_Instrumentation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* | ||
* * Copyright 2024 New Relic Corporation. All rights reserved. | ||
* * SPDX-License-Identifier: Apache-2.0 | ||
* | ||
*/ | ||
|
||
package zio; | ||
|
||
import com.newrelic.api.agent.weaver.MatchType; | ||
import com.newrelic.api.agent.weaver.Weave; | ||
import com.newrelic.api.agent.weaver.Weaver; | ||
|
||
@Weave(originalName = "zio.Executor", type = MatchType.BaseClass) | ||
public class ZIOExecutor_Instrumentation { | ||
|
||
public boolean submit(Runnable runnable, Unsafe unsafe) { | ||
runnable = new TokenAwareRunnable(runnable); | ||
return Weaver.callOriginal(); | ||
} | ||
} |
Oops, something went wrong.
A new PR is added to change this to
com.newrelic.instrumentation.zio-2