Skip to content

Commit

Permalink
Instrument method calls using the span helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
fstien committed Nov 2, 2020
1 parent 86505c0 commit 78110ae
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/EarthquakeClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.github.fstien

import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import com.zopa.ktor.opentracing.OpenTracingClient
import com.zopa.ktor.opentracing.span
import io.ktor.client.*
import io.ktor.client.call.*
import io.ktor.client.engine.apache.*
Expand All @@ -22,7 +23,7 @@ class EarthquakeClient {
install(OpenTracingClient)
}

private suspend fun getAll(): List<Earthquake> {
private suspend fun getAll(): List<Earthquake> = span("EarthquakeClient.getAll()") {
val date = LocalDateTime.now().toLocalDate()

val call: HttpStatement = client.get("https://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&starttime=$date")
Expand All @@ -36,25 +37,37 @@ class EarthquakeClient {

val earthquakes = earthQuakeResponse.features.map { it.properties.toEarthQuake() }

setTag("count", earthquakes.size)

return earthquakes
}

suspend fun getLatest(): Earthquake {
suspend fun getLatest(): Earthquake = span("EarthquakeClient.getLatest()") {
val earthquakes = getAll()
val latest = earthquakes.first()
setTag("location", latest.location)
setTag("magnitude", latest.magnitude)
setTag("timeGMT", latest.timeGMT)

return latest
}

suspend fun getBiggest(): Earthquake {
suspend fun getBiggest(): Earthquake = span("EarthquakeClient.getBiggest()") {
val earthquakes = getAll()
val biggest = earthquakes.sortedBy { it.magnitude }.last()
setTag("location", biggest.location)
setTag("magnitude", biggest.magnitude)
setTag("timeGMT", biggest.timeGMT)

return biggest
}

suspend fun getBiggerThan(threshold: Double): List<Earthquake> {
suspend fun getBiggerThan(threshold: Double): List<Earthquake> = span("EarthquakeClient.getBiggerThan()") {
setTag("threshold", threshold)
val earthquakes = getAll()
val biggerThan = earthquakes.filter { it.magnitude > threshold }
setTag("count", biggerThan.size)

return biggerThan
}
}
Expand Down

0 comments on commit 78110ae

Please sign in to comment.