-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
core-trace-experimental: add @withSpan
macro
#675
Closed
iRevive
wants to merge
1
commit into
typelevel:main
from
iRevive:core-trace-experimental/withSpan-macro
+933
−3
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
247 changes: 247 additions & 0 deletions
247
...race-experimental/src/main/scala-2/org/typelevel/otel4s/trace/experimental/withSpan.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,247 @@ | ||
/* | ||
* Copyright 2022 Typelevel | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.typelevel.otel4s.trace.experimental | ||
|
||
import scala.annotation.StaticAnnotation | ||
import scala.annotation.compileTimeOnly | ||
import scala.annotation.unused | ||
import scala.reflect.macros.blackbox | ||
|
||
/** Wraps the body of an annotated method or variable into the span. | ||
* | ||
* By default, the span name will be `className.methodName`, unless a name is | ||
* provided as an argument. | ||
* | ||
* {{{ | ||
* class Service[F[_]: Tracer](db: Database[F]) { | ||
* @withSpan | ||
* def findUser(@spanAttribute userId: Long): F[User] = | ||
* db.findUser(id) | ||
* } | ||
* | ||
* // expands into | ||
* | ||
* class Service[F[_]: Tracer](db: Database[F]) { | ||
* def findUser(userId: Long): F[User] = | ||
* Tracer[F] | ||
* .span("Service.findUser", Attribute("userId", userId)) | ||
* .surround(db.findUser(id)) | ||
* } | ||
* }}} | ||
* | ||
* @param name | ||
* the custom name of the span. If not specified, the span name will be | ||
* `className.methodName` | ||
* | ||
* @param debug | ||
* whether to print the generated code to the console | ||
*/ | ||
@compileTimeOnly("enable macro to expand macro annotations") | ||
class withSpan( | ||
@unused name: String = "", | ||
@unused debug: Boolean = false | ||
) extends StaticAnnotation { | ||
def macroTransform(annottees: Any*): Any = macro withSpanMacro.impl | ||
} | ||
|
||
object withSpanMacro { | ||
|
||
def impl(c: blackbox.Context)(annottees: c.Expr[Any]*): c.Expr[c.Tree] = { | ||
import c.universe._ | ||
|
||
def abort(message: String) = | ||
c.abort(c.enclosingPosition, s"@withSpan macro: $message") | ||
|
||
def ensureImplicitExist(tpe: Tree, reason: Throwable => String): c.Tree = | ||
try { | ||
val t = c.typecheck(tpe).tpe | ||
c.inferImplicitValue(t) | ||
} catch { | ||
case e: Throwable => abort(reason(e)) | ||
} | ||
|
||
def resolveEffectType(tpt: Tree): Tree = | ||
tpt match { | ||
case tq"$tpe[${_}]" => tpe | ||
case _ => abort("cannot resolve the type of the effect") | ||
} | ||
|
||
val macroName: Tree = | ||
c.prefix.tree match { | ||
case Apply(Select(New(name), _), _) => name | ||
case _ => c.abort(c.enclosingPosition, "Unexpected macro application") | ||
} | ||
|
||
val (nameParam, debug) = c.prefix.tree match { | ||
case q"new ${`macroName`}(..$args)" => | ||
( | ||
args.headOption | ||
.collect { case Literal(value) => Left(value) } | ||
.orElse( | ||
args.collectFirst { case q"name = $value" => Right(value) } | ||
), | ||
args.collectFirst { case q"debug = true" => }.isDefined | ||
) | ||
case _ => | ||
(None, false) | ||
} | ||
|
||
def spanName(definition: ValOrDefDef): Tree = | ||
nameParam match { | ||
case Some(Left(const)) => | ||
val literal = Literal(const) | ||
q"$literal" | ||
|
||
case Some(Right(tree)) => | ||
tree | ||
|
||
case None => | ||
@annotation.tailrec | ||
def resolveEnclosingName(symbol: Symbol, output: String): String = | ||
if (symbol.isClass) { | ||
val className = symbol.name.toString | ||
if (className.startsWith("$anon")) | ||
resolveEnclosingName(symbol.owner, "$anon" + "." + output) | ||
else | ||
className + "." + output | ||
} else { | ||
resolveEnclosingName(symbol.owner, output) | ||
} | ||
|
||
val prefix = resolveEnclosingName(c.internal.enclosingOwner, "") | ||
|
||
val literal = Literal( | ||
Constant(prefix + definition.name.decodedName.toString) | ||
) | ||
q"$literal" | ||
} | ||
|
||
def expandDef(defDef: DefDef): Tree = { | ||
val effectType = resolveEffectType(defDef.tpt) | ||
val name: Tree = spanName(defDef) | ||
|
||
val attributes = defDef.vparamss.flatten.flatMap { | ||
case ValDef(mods, name, tpt, _) => | ||
mods.annotations.flatMap { annotation => | ||
val typed = c.typecheck(annotation) | ||
if ( | ||
typed.tpe.typeSymbol.fullName == "org.typelevel.otel4s.trace.experimental.spanAttribute" | ||
) { | ||
val keyArg = typed match { | ||
case q"new ${_}(${keyArg})" => | ||
keyArg | ||
case _ => | ||
abort("unknown structure of the @spanAttribute annotation.") | ||
} | ||
|
||
val key = keyArg match { | ||
// the key param is not specified, use param name as a key | ||
case Select(_, TermName("$lessinit$greater$default$1")) => | ||
Literal(Constant(name.decodedName.toString)) | ||
|
||
// type of the AttributeKey must match the parameter type | ||
case q"$_[$tpe]($_)(..$_)" => | ||
val keyType = c.untypecheck(tpe) | ||
val argType = c.typecheck(tpt, c.TYPEmode) | ||
|
||
if (!keyType.equalsStructure(argType)) { | ||
abort( | ||
s"the argument [${name.toString}] type [$argType] does not match the type of the attribute [$keyType]." | ||
) | ||
} | ||
keyArg | ||
|
||
case _ => | ||
keyArg | ||
} | ||
|
||
ensureImplicitExist( | ||
q"_root_.org.typelevel.otel4s.AttributeKey.KeySelect[$tpt]", | ||
e => | ||
s"the argument [${name.decodedName}] cannot be used as an attribute. The type [$tpt] is not supported.${e.getMessage}" | ||
) | ||
|
||
List( | ||
q"_root_.org.typelevel.otel4s.Attribute($key, $name)" | ||
) | ||
} else { | ||
Nil | ||
} | ||
} | ||
|
||
case _ => | ||
Nil | ||
} | ||
|
||
val body = | ||
q""" | ||
_root_.org.typelevel.otel4s.trace.Tracer[$effectType].span($name, ..$attributes).surround { | ||
${defDef.rhs} | ||
} | ||
""" | ||
|
||
DefDef( | ||
defDef.mods, | ||
defDef.name, | ||
defDef.tparams, | ||
defDef.vparamss, | ||
defDef.tpt, | ||
body | ||
) | ||
} | ||
|
||
def expandVal(valDef: ValDef): Tree = { | ||
val effectType = resolveEffectType(valDef.tpt) | ||
val name: Tree = spanName(valDef) | ||
|
||
val body = | ||
q""" | ||
_root_.org.typelevel.otel4s.trace.Tracer[$effectType].span($name).surround { | ||
${valDef.rhs} | ||
} | ||
""" | ||
|
||
ValDef(valDef.mods, valDef.name, valDef.tpt, body) | ||
} | ||
|
||
val result = annottees.map(_.tree).toList match { | ||
case List(defDef: DefDef) => | ||
expandDef(defDef) | ||
|
||
case List(valDef: ValDef) => | ||
expandVal(valDef) | ||
|
||
case _ => | ||
abort( | ||
"unsupported definition. Only `def` and `val` with explicit result types and defined bodies are supported." | ||
) | ||
} | ||
|
||
if (debug) { | ||
val at: String = | ||
if (c.enclosingPosition == NoPosition) | ||
"" | ||
else | ||
s"at ${c.enclosingPosition.source.file.name}:${c.enclosingPosition.line} " | ||
|
||
scala.Predef.println(s"@withSpan $at- expanded into:\n${result}") | ||
} | ||
|
||
c.Expr(result) | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since KeySelect is sealed there's no way to define a
spanAttribute
for an unsupported type, normally we'd create an Attribute manually likeThis means we can't use the macro at all if we require one attribute that is unsupported. Would it be sensible to expose some way of contramapping an existing KeySelect?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Though thinking about it still won't be possible without some other function from value => key type 😢
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Valid points. The macro wouldn't work with newtypes/type aliases.
I guess we can provide some utility methods to lift arbitrary types into the Attribute.