View TraceRestCall.scala
private def uuid = java.util.UUID.randomUUID.toString
def trace = Directive[Unit] {
innerRoute ctx {
val traceId = s"req-$uuid"
val textMap = readOnlyTextMapFromHeaders(ctx.request.headers)
val incomingSpanContext = Kamon.extract(Format.HttpHeaders, textMap)
val serverSpan = addDefaultTags(Kamon.buildSpan(ctx.request.uri.path.toString))
.asChildOf(incomingSpanContext)
.withSpanTag("myTraceId", traceId)
View TraceFuture.scala
def traceFuture[T](name: String)(f: => Future[T]): Future[T] = {
val spanBuilder = Kamon.tracer.buildSpan("${getClass().getSimpleName}.$name"))
val newSpan = addDefaultTags(spanBuilder).start()
val activatedSpan = Kamon.makeActive(newSpan)
val evaluatedFuture = f.transform(
r => { newSpan.finish(); r},
t => { newSpan.finish(); t}
)(CallingThreadExecutionContext)
activatedSpan.deactivate()
evaluatedFuture
View TraceCodeBlock.scala
def traceBlock[T](name: String, parentSpanContext: Option[SpanContext] = None)(f: => T): T = {
val spanBuilder = Kamon.tracer.buildSpan(s"${getClass().getSimpleName}.$name")
val newSpan = addDefaultTags(spanBuilder).asChildOf(parentSpanContext).start()
val activatedSpan = Kamon.makeActive(newSpan)
try {
f
} finally {
activatedSpan.deactivate()
newSpan.finish()
}
View AddDefaultTags.scala
private lazy val cachedEnv = sys.env
def addDefaultTags(builder: SpanBuilder) = {
builder.withSpanTag("class", getClass().getName)
cachedEnv.get("KUBERNETES_POD").foreach { pod =>
builder.withSpanTag("kubernetes.pod", pod)
}
cachedEnv.get("KUBERNETES_NAMESPACE").foreach { namespace =>
builder.withSpanTag("kubernetes.namespace", namespace)
View grafana.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: grafana
spec:
replicas: 1
template:
metadata:
labels:
app: grafana
View prometheus.yml
global:
scrape_interval: 15s # By default, scrape targets every 15 seconds.
# A scrape configuration containing exactly one endpoint to scrape:
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: 'statsd-exporter'
# Override the global default and scrape targets from this job every 5 seconds.
scrape_interval: 5s
static_configs:
- targets: ['statsd-exporter-svc:9102'] # pull from the exposed statsd-exporter service
View statsd-exporter.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: statsd-exporter
spec:
replicas: 1
template:
metadata:
labels:
app: statsd-exporter
View reference.conf
kamon {
...
datadog {
hostname = "statsd-exporter-svc.monitoring.svc.cluster.local"
port = 8125
...
}
default-tags {
service-name = "my-service"
View prometheus.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: prometheus
spec:
replicas: 1
template:
metadata:
labels:
app: prometheus
View Boot.scala
object Boot extends App
with HealthCheckRoute
with HealthCheckService {
val mongoClient = new MongoClient {
// insert real implementation here
def listDatabaseNames() = Future.successful(Nil)
}
override def checks: List[HealthCheck] = List(new MongoDbHealthCheck(mongoClient))