Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into update/sangria-json4s…
Browse files Browse the repository at this point in the history
…-native-1.0.2
  • Loading branch information
yanns committed Nov 25, 2021
2 parents 1702b82 + 734e06b commit 3fbe91b
Show file tree
Hide file tree
Showing 17 changed files with 167 additions and 153 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
scala: [2.12.15, 2.13.6]
scala: [2.12.15, 2.13.7]
java: [[email protected]]
runs-on: ${{ matrix.os }}
steps:
Expand Down Expand Up @@ -77,7 +77,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest]
scala: [2.13.6]
scala: [2.13.7]
java: [[email protected]]
runs-on: ${{ matrix.os }}
steps:
Expand Down Expand Up @@ -113,12 +113,12 @@ jobs:
tar xf targets.tar
rm targets.tar
- name: Download target directories (2.13.6)
- name: Download target directories (2.13.7)
uses: actions/download-artifact@v2
with:
name: target-${{ matrix.os }}-2.13.6-${{ matrix.java }}
name: target-${{ matrix.os }}-2.13.7-${{ matrix.java }}

- name: Inflate target directories (2.13.6)
- name: Inflate target directories (2.13.7)
run: |
tar xf targets.tar
rm targets.tar
Expand Down
4 changes: 2 additions & 2 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
version = 3.0.4
version = 3.1.2

style = default
runner.dialect = scala213

maxColumn = 100

Expand Down
12 changes: 9 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import com.typesafe.tools.mima.core.{DirectMissingMethodProblem, ProblemFilters}

name := "sangria-slowlog"
organization := "org.sangria-graphql"

Expand All @@ -9,22 +11,26 @@ licenses := Seq(
"Apache License, ASL Version 2.0" -> url("https://www.apache.org/licenses/LICENSE-2.0"))

// sbt-github-actions needs configuration in `ThisBuild`
ThisBuild / crossScalaVersions := Seq("2.12.15", "2.13.6")
ThisBuild / crossScalaVersions := Seq("2.12.15", "2.13.7")
ThisBuild / scalaVersion := crossScalaVersions.value.last
ThisBuild / githubWorkflowPublishTargetBranches := List()
ThisBuild / githubWorkflowBuildPreamble ++= List(
WorkflowStep.Sbt(List("mimaReportBinaryIssues"), name = Some("Check binary compatibility")),
WorkflowStep.Sbt(List("scalafmtCheckAll"), name = Some("Check formatting"))
)
// Binary Incompatible Changes, we'll document.
ThisBuild / mimaBinaryIssueFilters ++= Seq(
ProblemFilters.exclude[DirectMissingMethodProblem]("sangria.slowlog.SlowLog.even")
)

scalacOptions += "-target:jvm-1.8"
javacOptions ++= Seq("-source", "8", "-target", "8")

scalacOptions ++= Seq("-deprecation", "-feature")

libraryDependencies ++= Seq(
"org.sangria-graphql" %% "sangria" % "2.1.3",
"io.dropwizard.metrics" % "metrics-core" % "4.2.3",
"org.sangria-graphql" %% "sangria" % "2.1.6",
"io.dropwizard.metrics" % "metrics-core" % "4.2.4",
"org.slf4j" % "slf4j-api" % "1.7.32",
"io.opentracing.contrib" %% "opentracing-scala-concurrent" % "0.0.6",
"io.opentracing" % "opentracing-mock" % "0.33.0" % Test,
Expand Down
8 changes: 4 additions & 4 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
addSbtPlugin("com.typesafe" % "sbt-mima-plugin" % "1.0.0")
addSbtPlugin("org.scoverage" % "sbt-scoverage" % "1.9.0")
addSbtPlugin("com.typesafe" % "sbt-mima-plugin" % "1.0.1")
addSbtPlugin("org.scoverage" % "sbt-scoverage" % "1.9.2")
addSbtPlugin("org.scoverage" % "sbt-coveralls" % "1.3.1")
addSbtPlugin("com.codecommit" % "sbt-github-actions" % "0.13.0")
addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.4.3")
addSbtPlugin("com.geirsson" % "sbt-ci-release" % "1.5.7")
addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.4.4")
addSbtPlugin("com.github.sbt" % "sbt-ci-release" % "1.5.10")
10 changes: 5 additions & 5 deletions src/main/scala/sangria/slowlog/ApolloTracingExtension.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,23 @@ object ApolloTracingExtension
type QueryVal = QueryTrace
type FieldVal = Long

def beforeQuery(context: MiddlewareQueryContext[Any, _, _]) =
def beforeQuery(context: MiddlewareQueryContext[Any, _, _]): QueryTrace =
QueryTrace(Instant.now(), System.nanoTime(), new ConcurrentLinkedQueue)

def afterQuery(queryVal: QueryVal, context: MiddlewareQueryContext[Any, _, _]) = ()
def afterQuery(queryVal: QueryVal, context: MiddlewareQueryContext[Any, _, _]): Unit = ()

def beforeField(
queryVal: QueryVal,
mctx: MiddlewareQueryContext[Any, _, _],
ctx: Context[Any, _]) =
ctx: Context[Any, _]): BeforeFieldResult[Any, FieldVal] =
continue(System.nanoTime())

def afterField(
queryVal: QueryVal,
fieldVal: FieldVal,
value: Any,
mctx: MiddlewareQueryContext[Any, _, _],
ctx: Context[Any, _]) = {
ctx: Context[Any, _]): None.type = {
updateMetric(queryVal, fieldVal, ctx)
None
}
Expand All @@ -46,7 +46,7 @@ object ApolloTracingExtension
fieldVal: FieldVal,
error: Throwable,
mctx: MiddlewareQueryContext[Any, _, _],
ctx: Context[Any, _]) =
ctx: Context[Any, _]): Unit =
updateMetric(queryVal, fieldVal, ctx)

def updateMetric(queryVal: QueryVal, fieldVal: FieldVal, ctx: Context[Any, _]): Unit =
Expand Down
6 changes: 3 additions & 3 deletions src/main/scala/sangria/slowlog/FieldMetrics.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package sangria.slowlog

import com.codahale.metrics.{Counter, Histogram}
import com.codahale.metrics.{Counter, Histogram, Snapshot}

case class FieldMetrics(success: Counter, failure: Counter, histogram: Histogram) {
lazy val snapshot = histogram.getSnapshot
lazy val count = histogram.getCount
lazy val snapshot: Snapshot = histogram.getSnapshot
lazy val count: Long = histogram.getCount
}
20 changes: 10 additions & 10 deletions src/main/scala/sangria/slowlog/MetricRenderer.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package sangria.slowlog

import java.util.concurrent.TimeUnit

import com.codahale.metrics.Snapshot
import sangria.marshalling.InputUnmarshaller
import sangria.ast
import sangria.ast.ObjectValue

trait MetricRenderer {
def renderField(typeName: String, metrics: FieldMetrics, prefix: String): String
Expand All @@ -17,13 +17,13 @@ trait MetricRenderer {
}

object MetricRenderer {
implicit val default = new DefaultMetricRenderer(TimeUnit.MILLISECONDS)
implicit val default: DefaultMetricRenderer = new DefaultMetricRenderer(TimeUnit.MILLISECONDS)

def in(unit: TimeUnit) = new DefaultMetricRenderer(unit)
}

class DefaultMetricRenderer(val unit: TimeUnit) extends MetricRenderer {
def renderVariables[In: InputUnmarshaller](variables: In, names: Vector[String]) = {
def renderVariables[In: InputUnmarshaller](variables: In, names: Vector[String]): String = {
val iu = implicitly[InputUnmarshaller[In]]
val renderedVars = names.iterator.flatMap(name =>
iu.getRootMapValue(variables, name).map(v => s" $$$name = ${iu.render(v)}"))
Expand All @@ -34,7 +34,7 @@ class DefaultMetricRenderer(val unit: TimeUnit) extends MetricRenderer {
""
}

def renderField(typeName: String, metrics: FieldMetrics, prefix: String) = {
def renderField(typeName: String, metrics: FieldMetrics, prefix: String): String = {
val success = metrics.success.getCount
val failure = metrics.failure.getCount
val histogram = metrics.snapshot
Expand Down Expand Up @@ -63,14 +63,14 @@ class DefaultMetricRenderer(val unit: TimeUnit) extends MetricRenderer {
"p99" -> renderDuration(snap.get99thPercentile.toLong)
)

def renderDuration(durationNanos: Long) =
if (unit == TimeUnit.NANOSECONDS) durationNanos + renderTimeUnit(unit)
else unit.convert(durationNanos, TimeUnit.NANOSECONDS) + renderTimeUnit(unit)
def renderDuration(durationNanos: Long): String =
if (unit == TimeUnit.NANOSECONDS) s"${durationNanos}${renderTimeUnit(unit)}"
else s"${unit.convert(durationNanos, TimeUnit.NANOSECONDS)}${renderTimeUnit(unit)}"

def renderLogMessage(durationNanos: Long, enrichedQuery: String) =
s"Slow GraphQL query [${renderDuration(durationNanos)}].\n\n$enrichedQuery"

def fieldMetrics(metrics: FieldMetrics) = {
def fieldMetrics(metrics: FieldMetrics): ObjectValue = {
val durationMetrics =
Vector(
durationField("min", metrics.snapshot.getMin),
Expand Down Expand Up @@ -101,7 +101,7 @@ class DefaultMetricRenderer(val unit: TimeUnit) extends MetricRenderer {
ast.ObjectField(name + timeUnitSuffix, ast.BigIntValue(correctValue))
}

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

lazy val timeUnitSuffix = unit match {
lazy val timeUnitSuffix: String = unit match {
case TimeUnit.DAYS => "Day"
case TimeUnit.HOURS => "Hour"
case TimeUnit.MICROSECONDS => "Micros"
Expand Down
11 changes: 6 additions & 5 deletions src/main/scala/sangria/slowlog/OpenTracing.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class OpenTracing(parentSpan: Option[Span] = None, defaultOperationName: String
type QueryVal = TrieMap[Vector[Any], (Span, Scope)]
type FieldVal = Unit

def beforeQuery(context: MiddlewareQueryContext[Any, _, _]) = {
def beforeQuery(
context: MiddlewareQueryContext[Any, _, _]): TrieMap[Vector[Any], (Span, Scope)] = {
val builder = tracer
.buildSpan(context.operationName.getOrElse(defaultOperationName))
.withTag("type", "graphql-query")
Expand All @@ -32,7 +33,7 @@ class OpenTracing(parentSpan: Option[Span] = None, defaultOperationName: String
TrieMap(Vector.empty -> (span, scope))
}

def afterQuery(queryVal: QueryVal, context: MiddlewareQueryContext[Any, _, _]) =
def afterQuery(queryVal: QueryVal, context: MiddlewareQueryContext[Any, _, _]): Unit =
queryVal.get(Vector.empty).foreach { case (span, scope) =>
span.finish()
scope.close()
Expand All @@ -41,7 +42,7 @@ class OpenTracing(parentSpan: Option[Span] = None, defaultOperationName: String
def beforeField(
queryVal: QueryVal,
mctx: MiddlewareQueryContext[Any, _, _],
ctx: Context[Any, _]) = {
ctx: Context[Any, _]): BeforeFieldResult[Any, Unit] = {
val path = ctx.path.path
val parentPath = path
.dropRight(1)
Expand Down Expand Up @@ -80,7 +81,7 @@ class OpenTracing(parentSpan: Option[Span] = None, defaultOperationName: String
fieldVal: FieldVal,
value: Any,
mctx: MiddlewareQueryContext[Any, _, _],
ctx: Context[Any, _]) = {
ctx: Context[Any, _]): None.type = {
queryVal.get(ctx.path.path).foreach { case (span, scope) =>
span.finish()
scope.close()
Expand All @@ -93,7 +94,7 @@ class OpenTracing(parentSpan: Option[Span] = None, defaultOperationName: String
fieldVal: FieldVal,
error: Throwable,
mctx: MiddlewareQueryContext[Any, _, _],
ctx: Context[Any, _]) =
ctx: Context[Any, _]): Unit =
queryVal.get(ctx.path.path).foreach { case (span, scope) =>
span.finish()
scope.close()
Expand Down
11 changes: 6 additions & 5 deletions src/main/scala/sangria/slowlog/QueryMetrics.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package sangria.slowlog

import com.codahale.metrics.{Counter, ExponentiallyDecayingReservoir, Histogram}
import sangria.ast
import sangria.ast.{AstVisitor, FragmentSpread}
import sangria.ast.{AstVisitor, Comment, FragmentSpread}
import sangria.execution.Extension
import sangria.marshalling.InputUnmarshaller
import sangria.schema.{ObjectType, Schema}
Expand All @@ -11,6 +11,7 @@ import sangria.visitor.VisitorCommand
import scala.collection.concurrent.TrieMap
import scala.collection.mutable
import sangria.marshalling.queryAst._
import sangria.renderer.QueryRenderer
import sangria.validation.TypeInfo

import scala.collection.immutable.VectorBuilder
Expand All @@ -27,7 +28,7 @@ case class QueryMetrics(
fieldName: String,
success: Boolean,
startNanos: Long,
endNanos: Long) = {
endNanos: Long): Unit = {
val duration = endNanos - startNanos
val forPath = pathData.getOrElseUpdate(path, TrieMap.empty)
val m = forPath.getOrElseUpdate(
Expand Down Expand Up @@ -269,7 +270,7 @@ case class QueryMetrics(
builder.result()
}

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

AstVisitor.visit(
Expand All @@ -281,7 +282,7 @@ case class QueryMetrics(
names.toVector
}

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

Expand Down Expand Up @@ -325,7 +326,7 @@ case class QueryMetrics(
renderer.durationField("execution", durationNanos),
renderer.durationField("validation", validationNanos),
renderer.durationField("reducers", queryReducerNanos),
ast.ObjectField("query", ast.StringValue(enrichedQuery.renderPretty)),
ast.ObjectField("query", ast.StringValue(QueryRenderer.renderPretty(enrichedQuery))),
ast.ObjectField("types", ast.ObjectValue(typeMetrics))
))
)))
Expand Down
Loading

0 comments on commit 3fbe91b

Please sign in to comment.