-
Notifications
You must be signed in to change notification settings - Fork 327
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7bf8705
commit a6b1344
Showing
4 changed files
with
145 additions
and
0 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
15 changes: 15 additions & 0 deletions
15
instrumentation/kamon-redis/src/main/resources/reference.conf
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,15 @@ | ||
kanela.modules { | ||
redis { | ||
name = "Redis Instrumentation" | ||
# Does it provide metrics? No. | ||
description = "Provides tracing and metrics for the Jedis library" | ||
|
||
instrumentations = [ | ||
"kamon.instrumentation.jedis.JedisInstrumentation", | ||
] | ||
|
||
within = [ | ||
"redis.clients.jedis..*", | ||
] | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
...ntation/kamon-redis/src/main/scala/kamon/instrumentation/jedis/JedisInstrumentation.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,59 @@ | ||
package kamon.instrumentation.jedis | ||
|
||
import kamon.Kamon | ||
import kamon.context.Storage.Scope | ||
import kamon.trace.Span | ||
import kamon.trace.Trace.SamplingDecision | ||
import redis.clients.jedis.commands.ProtocolCommand | ||
import kanela.agent.api.instrumentation.InstrumentationBuilder | ||
import kanela.agent.libs.net.bytebuddy.asm.Advice | ||
|
||
class JedisInstrumentation extends InstrumentationBuilder { | ||
onType("redis.clients.jedis.Protocol") | ||
.advise(method("sendCommand").and(withArgument(1, classOf[ProtocolCommand])), classOf[SendCommandAdvice]) | ||
|
||
onType("redis.clients.jedis.Jedis") | ||
// we're gonna have a looot of these | ||
.advise(method("set"), classOf[JedisCommandAdvice]) | ||
.advise(method("get"), classOf[JedisCommandAdvice]) | ||
} | ||
|
||
class JedisCommandAdvice | ||
object JedisCommandAdvice { | ||
@Advice.OnMethodEnter() | ||
def enter = { | ||
// operation name default should be in conf | ||
val span = Kamon.clientSpanBuilder("redis.command.unknown", "redis.client.jedis") | ||
.samplingDecision(SamplingDecision.Unknown) | ||
.start() | ||
|
||
(Kamon.storeContext(Kamon.currentContext().withEntry(Span.Key, span)), span) | ||
} | ||
|
||
@Advice.OnMethodExit(onThrowable = classOf[Throwable]) | ||
def exit(@Advice.Enter enter: (Scope, Span), | ||
@Advice.Thrown t: Throwable) = { | ||
val (scope, span) = enter | ||
// when should I fail the span? | ||
// is this throwable thing ok? | ||
// how do I even test that it's good? | ||
if (t != null) { | ||
span.fail(t) | ||
} else { | ||
span.finish() | ||
} | ||
scope.close() | ||
} | ||
} | ||
|
||
class SendCommandAdvice | ||
object SendCommandAdvice { | ||
@Advice.OnMethodEnter() | ||
def enter(@Advice.Argument(1) command: ProtocolCommand) = { | ||
val span = Kamon.currentSpan() | ||
if (span.operationName() == "redis.command.unknown") { | ||
span.name(s"redis.command.${command}") | ||
.takeSamplingDecision() | ||
} | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
...ion/kamon-redis/src/test/scala/kamon/instrumentation/jedis/JedisInstrumentationSpec.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,56 @@ | ||
package kamon.instrumentation.jedis | ||
|
||
import kamon.metric.MeasurementUnit.time.seconds | ||
import kamon.testkit.{MetricInspection, TestSpanReporter} | ||
import kamon.trace.Span.Kind | ||
import org.scalatest.{BeforeAndAfterAll, Matchers, OptionValues, WordSpec} | ||
import org.scalatest.concurrent.{Eventually, ScalaFutures} | ||
import org.testcontainers.containers.GenericContainer | ||
import org.testcontainers.utility.DockerImageName | ||
import redis.clients.jedis.Jedis | ||
|
||
import scala.collection.convert.ImplicitConversions.`collection AsScalaIterable` | ||
import scala.concurrent.duration.DurationInt | ||
|
||
|
||
class JedisInstrumentationSpec extends WordSpec | ||
with Matchers | ||
with ScalaFutures | ||
with Eventually | ||
with BeforeAndAfterAll | ||
with MetricInspection.Syntax | ||
with OptionValues | ||
with TestSpanReporter { | ||
|
||
var container: GenericContainer[Nothing] = _ | ||
override def beforeAll = { | ||
val REDIS_IMAGE = DockerImageName.parse("redis") | ||
container = new GenericContainer(REDIS_IMAGE).withExposedPorts(6379) | ||
|
||
container.start() | ||
} | ||
|
||
override def afterAll= { | ||
container.stop() | ||
} | ||
|
||
"the Jedis instrumentation" should { | ||
"generate a client span for get and set commands" in { | ||
val jedis = new Jedis(container.getHost, container.getFirstMappedPort) | ||
jedis.set("foo", "bar") | ||
|
||
eventually(timeout(10.seconds)) { | ||
val span = testSpanReporter().nextSpan().get | ||
span.operationName shouldBe "redis.command.SET" | ||
span.kind shouldBe Kind.Client | ||
} | ||
|
||
jedis.get("foo") | ||
eventually(timeout(10.seconds)) { | ||
val span = testSpanReporter().nextSpan().get | ||
span.operationName shouldBe "redis.command.GET" | ||
span.kind shouldBe Kind.Client | ||
} | ||
} | ||
} | ||
} |