Skip to content

Commit

Permalink
code review
Browse files Browse the repository at this point in the history
  • Loading branch information
yanns committed Nov 8, 2024
1 parent 420e78d commit e405963
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 23 deletions.
8 changes: 7 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@ ThisBuild / githubWorkflowBuildPreamble ++= List(
)
// Binary Incompatible Changes, we'll document.
ThisBuild / mimaBinaryIssueFilters ++= Seq(
ProblemFilters.exclude[DirectMissingMethodProblem]("sangria.slowlog.SlowLog.even")
ProblemFilters.exclude[DirectMissingMethodProblem]("sangria.slowlog.SlowLog.even"),
ProblemFilters.exclude[DirectMissingMethodProblem]("sangria.slowlog.DefaultMetricRenderer.renderHistogram"),
ProblemFilters.exclude[DirectMissingMethodProblem]("sangria.slowlog.DefaultMetricRenderer.renderTimeUnit"),
ProblemFilters.exclude[DirectMissingMethodProblem]("sangria.slowlog.DefaultMetricRenderer.timeUnitSuffix"),
ProblemFilters.exclude[DirectMissingMethodProblem]("sangria.slowlog.QueryMetrics.findVariableNames"),
ProblemFilters.exclude[DirectMissingMethodProblem]("sangria.slowlog.QueryMetrics.addComments"),
ProblemFilters.exclude[DirectMissingMethodProblem]("sangria.slowlog.QueryMetrics.toComments")
)

scalacOptions += { if (isScala3.value) "-Xtarget:8" else "-target:jvm-1.8" }
Expand Down
13 changes: 8 additions & 5 deletions src/main/scala/sangria/slowlog/MetricRenderer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class DefaultMetricRenderer(val unit: TimeUnit) extends MetricRenderer {
val histogram = metrics.snapshot
val count = metrics.count

val countStr = s"[$prefix$typeName] count: $success${if (failure > 0) "/" + failure else ""}"
val countStr = s"[$prefix$typeName] count: $success${if (failure > 0) s"/$failure" else ""}"

(countStr +: renderHistogram(count, histogram, unit).map { case (n, v) => s"$n: $v" })
.mkString(", ")
Expand All @@ -49,7 +49,10 @@ class DefaultMetricRenderer(val unit: TimeUnit) extends MetricRenderer {
def renderExecution(durationNanos: Long, validationNanos: Long, queryReducerNanos: Long) =
s"[Execution Metrics] duration: ${renderDuration(durationNanos)}, validation: ${renderDuration(validationNanos)}, reducers: ${renderDuration(queryReducerNanos)}"

def renderHistogram(count: Long, snap: Snapshot, unit: TimeUnit): Vector[(String, String)] =
private def renderHistogram(
count: Long,
snap: Snapshot,
unit: TimeUnit): Vector[(String, String)] =
if (count == 1)
Vector("time" -> renderDuration(snap.getMax))
else
Expand Down Expand Up @@ -97,10 +100,10 @@ class DefaultMetricRenderer(val unit: TimeUnit) extends MetricRenderer {
if (unit == TimeUnit.NANOSECONDS) value
else unit.convert(value, TimeUnit.NANOSECONDS)

ast.ObjectField(name + timeUnitSuffix, ast.BigIntValue(correctValue))
ast.ObjectField(s"$name$timeUnitSuffix", ast.BigIntValue(correctValue))
}

def renderTimeUnit(unit: TimeUnit): String = unit match {
private def renderTimeUnit(unit: TimeUnit): String = unit match {
case TimeUnit.DAYS => "d"
case TimeUnit.HOURS => "h"
case TimeUnit.MICROSECONDS => "μs"
Expand All @@ -110,7 +113,7 @@ class DefaultMetricRenderer(val unit: TimeUnit) extends MetricRenderer {
case TimeUnit.SECONDS => "s"
}

lazy val timeUnitSuffix: String = unit match {
private lazy val timeUnitSuffix: String = unit match {
case TimeUnit.DAYS => "Day"
case TimeUnit.HOURS => "Hour"
case TimeUnit.MICROSECONDS => "Micros"
Expand Down
8 changes: 3 additions & 5 deletions src/main/scala/sangria/slowlog/OpenTracing.scala
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,14 @@ class OpenTracing(parentSpan: Option[Span] = None, defaultOperationName: String
val spanBuilder =
queryVal
.get(parentPath)
.map { case (parentSpan, _) =>
.fold(
tracer
.buildSpan(ctx.field.name)
.withTag("type", "graphql-field")
.asChildOf(parentSpan)
}
.getOrElse {
.withTag("type", "graphql-field")) { case (parentSpan, _) =>
tracer
.buildSpan(ctx.field.name)
.withTag("type", "graphql-field")
.asChildOf(parentSpan)
}

val span = spanBuilder.start()
Expand Down
21 changes: 10 additions & 11 deletions src/main/scala/sangria/slowlog/QueryMetrics.scala
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ case class QueryMetrics(
if (varNames.nonEmpty) {
val rendered = renderer.renderVariables(variables, varNames)

if (rendered.trim.nonEmpty) toComments("\n" + rendered)
if (rendered.trim.nonEmpty) toComments(s"\n$rendered")
else Vector.empty
} else Vector.empty

Expand All @@ -103,7 +103,7 @@ case class QueryMetrics(
val paths =
originPaths.iterator.map {
case (_, hoPath) if hoPath.isEmpty => "* (query root)"
case (_, hoPath) => "* " + hoPath.mkString(".")
case (_, hoPath) => s"* ${hoPath.mkString(".")}"
}

val usages =
Expand Down Expand Up @@ -143,10 +143,7 @@ case class QueryMetrics(
metricComments(
typeInfo,
typeMetrics,
hoPath.mkString("", ".", if (hoPath.nonEmpty) "." else "") + path.mkString(
"",
".",
" "))
s"${hoPath.mkString("", ".", if (hoPath.nonEmpty) "." else "")}${path.mkString("", ".", " ")}")
case None => Vector.empty
}
}
Expand Down Expand Up @@ -203,7 +200,7 @@ case class QueryMetrics(
if (varNames.nonEmpty) {
val renderedVars = renderer.renderVariables(variables, varNames)

if (renderedVars.nonEmpty) toComments("\n" + renderedVars)
if (renderedVars.nonEmpty) toComments(s"\n$renderedVars")
else Vector.empty
} else Vector.empty
}
Expand Down Expand Up @@ -270,7 +267,7 @@ case class QueryMetrics(
builder.result()
}

def findVariableNames(node: ast.AstNode): Vector[String] = {
private def findVariableNames(node: ast.AstNode): Vector[String] = {
val names = new mutable.HashSet[String]

AstVisitor.visit(
Expand All @@ -282,11 +279,13 @@ case class QueryMetrics(
names.toVector
}

def addComments(existing: Vector[ast.Comment], added: Vector[ast.Comment]): Vector[Comment] =
private def addComments(
existing: Vector[ast.Comment],
added: Vector[ast.Comment]): Vector[Comment] =
if (existing.nonEmpty) (existing :+ ast.Comment("")) ++ added
else added

def toComments(s: String): Vector[ast.Comment] =
private def toComments(s: String): Vector[ast.Comment] =
s.split("\n").iterator.map(c => ast.Comment(c.trim)).toVector

def extension(
Expand All @@ -298,7 +297,7 @@ case class QueryMetrics(
val sortedTypes =
fieldData.iterator
.map { case (typeName, fields) =>
typeName -> fields.map { case (fieldName, metrics) =>
typeName -> fields.iterator.map { case (_, metrics) =>
metrics.snapshot.get98thPercentile()
}.max
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/sangria/slowlog/SlowLog.scala
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ object SlowLog {
addExtensions)

def print(
threshold: FiniteDuration = 0 seconds,
threshold: FiniteDuration = 0.seconds,
addExtensions: Boolean = false,
separateOp: Boolean = true)(implicit renderer: MetricRenderer): SlowLog =
new SlowLog(
Expand Down

0 comments on commit e405963

Please sign in to comment.