-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #49 from hygt/http
Add Thrift-over-HTTP exporter
- Loading branch information
Showing
14 changed files
with
328 additions
and
102 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
79 changes: 79 additions & 0 deletions
79
...s/jaeger-thrift-common/src/main/scala/io/janstenpickle/trace4cats/jaeger/JaegerSpan.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,79 @@ | ||
package io.janstenpickle.trace4cats.jaeger | ||
|
||
import cats.data.NonEmptyList | ||
import cats.syntax.show._ | ||
import io.jaegertracing.thriftjava._ | ||
import io.janstenpickle.trace4cats.`export`.SemanticTags | ||
import io.janstenpickle.trace4cats.model.AttributeValue._ | ||
import io.janstenpickle.trace4cats.model._ | ||
|
||
import java.nio.ByteBuffer | ||
import java.util.concurrent.TimeUnit | ||
import scala.jdk.CollectionConverters._ | ||
|
||
private[jaeger] object JaegerSpan { | ||
|
||
private val statusTags = SemanticTags.statusTags("span.") | ||
|
||
def makeTags(attributes: Map[String, AttributeValue]): java.util.List[Tag] = | ||
attributes.view | ||
.map { | ||
case (key, StringValue(value)) => | ||
new Tag(key, TagType.STRING).setVStr(value.value) | ||
case (key, DoubleValue(value)) => | ||
new Tag(key, TagType.DOUBLE).setVDouble(value.value) | ||
case (key, BooleanValue(value)) => | ||
new Tag(key, TagType.BOOL).setVBool(value.value) | ||
case (key, LongValue(value)) => | ||
new Tag(key, TagType.LONG).setVLong(value.value) | ||
case (key, value: AttributeList) => | ||
new Tag(key, TagType.STRING).setVStr(value.show) | ||
} | ||
.toList | ||
.asJava | ||
|
||
def traceIdToLongs(traceId: TraceId): (Long, Long) = { | ||
val traceIdBuffer = ByteBuffer.wrap(traceId.value) | ||
(traceIdBuffer.getLong, traceIdBuffer.getLong) | ||
} | ||
|
||
def spanIdToLong(spanId: SpanId): Long = ByteBuffer.wrap(spanId.value).getLong | ||
|
||
def references(links: Option[NonEmptyList[Link]]): java.util.List[SpanRef] = | ||
links | ||
.fold(List.empty[SpanRef])(_.map { link => | ||
val (traceIdHigh, traceIdLow) = traceIdToLongs(link.traceId) | ||
val spanId = spanIdToLong(link.spanId) | ||
new SpanRef(SpanRefType.FOLLOWS_FROM, traceIdLow, traceIdHigh, spanId) | ||
}.toList) | ||
.asJava | ||
|
||
def convert(process: TraceProcess): Process = | ||
new Process(process.serviceName).setTags(makeTags(process.attributes)) | ||
|
||
def convert(span: CompletedSpan): Span = { | ||
val (traceIdHigh, traceIdLow) = traceIdToLongs(span.context.traceId) | ||
|
||
val startMicros = TimeUnit.MILLISECONDS.toMicros(span.start.toEpochMilli) | ||
val endMicros = TimeUnit.MILLISECONDS.toMicros(span.end.toEpochMilli) | ||
|
||
val thriftSpan = new Span( | ||
traceIdLow, | ||
traceIdHigh, | ||
spanIdToLong(span.context.spanId), | ||
span.context.parent.map(parent => ByteBuffer.wrap(parent.spanId.value).getLong).getOrElse(0), | ||
span.name, | ||
span.context.traceFlags.sampled match { | ||
case SampleDecision.Include => 0 | ||
case SampleDecision.Drop => 1 | ||
}, | ||
startMicros, | ||
endMicros - startMicros | ||
) | ||
|
||
thriftSpan | ||
.setTags(makeTags(span.allAttributes ++ statusTags(span.status) ++ SemanticTags.kindTags(span.kind))) | ||
.setReferences(references(span.links)) | ||
} | ||
|
||
} |
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
40 changes: 40 additions & 0 deletions
40
...-exporter/src/main/scala/io/janstenpickle/trace4cats/jaeger/JaegerHttpSpanCompleter.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,40 @@ | ||
package io.janstenpickle.trace4cats.jaeger | ||
|
||
import cats.effect.kernel.{Async, Resource} | ||
import fs2.Chunk | ||
import io.janstenpickle.trace4cats.`export`.{CompleterConfig, QueuedSpanCompleter} | ||
import io.janstenpickle.trace4cats.kernel.SpanCompleter | ||
import io.janstenpickle.trace4cats.model.TraceProcess | ||
import org.http4s.Uri | ||
import org.http4s.client.Client | ||
import org.typelevel.log4cats.Logger | ||
import org.typelevel.log4cats.slf4j.Slf4jLogger | ||
|
||
object JaegerHttpSpanCompleter { | ||
|
||
def apply[F[_]: Async]( | ||
client: Client[F], | ||
process: TraceProcess, | ||
host: String = "localhost", | ||
port: Int = 9411, | ||
config: CompleterConfig = CompleterConfig(), | ||
protocol: String = "http" | ||
): Resource[F, SpanCompleter[F]] = | ||
Resource.eval(Slf4jLogger.create[F]).flatMap { implicit logger: Logger[F] => | ||
Resource | ||
.eval(JaegerHttpSpanExporter[F, Chunk](client, process, host, port, protocol)) | ||
.flatMap(QueuedSpanCompleter[F](process, _, config)) | ||
} | ||
|
||
def apply[F[_]: Async]( | ||
client: Client[F], | ||
process: TraceProcess, | ||
uri: Uri, | ||
config: CompleterConfig | ||
): Resource[F, SpanCompleter[F]] = | ||
Resource.eval(Slf4jLogger.create[F]).flatMap { implicit logger: Logger[F] => | ||
Resource | ||
.eval(JaegerHttpSpanExporter[F, Chunk](client, process, uri)) | ||
.flatMap(QueuedSpanCompleter[F](process, _, config)) | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
...p-exporter/src/main/scala/io/janstenpickle/trace4cats/jaeger/JaegerHttpSpanExporter.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,61 @@ | ||
package io.janstenpickle.trace4cats.jaeger | ||
|
||
import cats.effect.kernel.Async | ||
import cats.syntax.either._ | ||
import cats.syntax.flatMap._ | ||
import cats.syntax.foldable._ | ||
import cats.syntax.functor._ | ||
import cats.{ApplicativeThrow, Foldable} | ||
import fs2.{Chunk, Stream} | ||
import io.jaegertracing.thriftjava.{Batch => JaegerBatch, Process, Span} | ||
import io.janstenpickle.trace4cats.`export`.HttpSpanExporter | ||
import io.janstenpickle.trace4cats.kernel.SpanExporter | ||
import io.janstenpickle.trace4cats.model.{Batch, TraceProcess} | ||
import org.apache.thrift.TSerializer | ||
import org.http4s._ | ||
import org.http4s.client.Client | ||
import org.http4s.headers.`Content-Type` | ||
|
||
import scala.util.control.NonFatal | ||
|
||
object JaegerHttpSpanExporter { | ||
|
||
private val thriftBinary: List[Header.ToRaw] = List(`Content-Type`(MediaType.application.`vnd.apache.thrift.binary`)) | ||
|
||
private implicit def jaegerBatchEncoder[F[_]: ApplicativeThrow](implicit | ||
serializer: TSerializer | ||
): EntityEncoder[F, JaegerBatch] = | ||
new EntityEncoder[F, JaegerBatch] { | ||
def toEntity(a: JaegerBatch): Entity[F] = try { | ||
val payload = serializer.serialize(a) | ||
Entity(Stream.chunk(Chunk.array(payload)), Some(payload.length.toLong)) | ||
} catch { | ||
case NonFatal(e) => Entity(Stream.raiseError[F](e), None) | ||
} | ||
|
||
val headers: Headers = Headers(thriftBinary) | ||
} | ||
|
||
private def makeBatch[G[_]: Foldable](jaegerProcess: Process, batch: Batch[G]): JaegerBatch = { | ||
val spans = batch.spans.foldLeft(new java.util.ArrayList[Span]) { case (acc, span) => | ||
acc.add(JaegerSpan.convert(span)) | ||
acc | ||
} | ||
new JaegerBatch(jaegerProcess, spans) | ||
} | ||
|
||
def apply[F[_]: Async, G[_]: Foldable]( | ||
client: Client[F], | ||
process: TraceProcess, | ||
host: String = "localhost", | ||
port: Int = 14268, | ||
protocol: String = "http" | ||
): F[SpanExporter[F, G]] = | ||
Uri.fromString(s"$protocol://$host:$port/api/traces").liftTo[F].flatMap(uri => apply(client, process, uri)) | ||
|
||
def apply[F[_]: Async, G[_]: Foldable](client: Client[F], process: TraceProcess, uri: Uri): F[SpanExporter[F, G]] = | ||
Async[F].catchNonFatal(new TSerializer()).map { implicit serializer: TSerializer => | ||
val jprocess = JaegerSpan.convert(process) | ||
HttpSpanExporter[F, G, JaegerBatch](client, uri, (batch: Batch[G]) => makeBatch[G](jprocess, batch), thriftBinary) | ||
} | ||
} |
Oops, something went wrong.