Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lightstep histogram lower bound #24

Merged
merged 1 commit into from
Jul 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions kotlin/goodmetrics/src/main/kotlin/goodmetrics/MetricsSetups.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import goodmetrics.downstream.GrpcTrailerLoggerInterceptor
import goodmetrics.downstream.OpentelemetryClient
import goodmetrics.downstream.PrescientDimensions
import goodmetrics.downstream.SecurityMode
import goodmetrics.io.opentelemetry.proto.metrics.v1.ResourceMetrics
import goodmetrics.pipeline.AggregatedBatch
import goodmetrics.pipeline.Aggregator
import goodmetrics.pipeline.BatchSender.Companion.launchSender
Expand Down Expand Up @@ -78,6 +79,12 @@ class MetricsSetups private constructor() {
preaggregatedBatchMaxAge: Duration = 10.seconds,
onSendUnary: (List<Metrics>) -> Unit = {},
onSendPreaggregated: (List<AggregatedBatch>) -> Unit = {},
/**
* This is verbose but can be helpful when debugging lightstep data format issues.
* It shows you exactly what protocol buffers structure is sent.
* Log with caution.
*/
logRawPayload: (ResourceMetrics) -> Unit = {},
): ConfiguredMetrics {
val client = opentelemetryClient(
lightstepAccessToken,
Expand All @@ -86,7 +93,8 @@ class MetricsSetups private constructor() {
prescientDimensions,
lightstepConnectionSecurityMode,
onLightstepTrailers,
timeout
timeout,
logRawPayload,
)

val unarySink = configureBatchedUnaryLightstepSink(unaryBatchSizeMaxMetricsCount, unaryBatchMaxAge, client, logError, onSendUnary)
Expand Down Expand Up @@ -227,7 +235,8 @@ class MetricsSetups private constructor() {
prescientDimensions: PrescientDimensions,
lightstepConnectionSecurityMode: SecurityMode,
onLightstepTrailers: (Status, Metadata) -> Unit,
timeout: Duration
timeout: Duration,
logRawPayload: (ResourceMetrics) -> Unit = { },
): OpentelemetryClient {
val authHeader = Metadata()
authHeader.put(
Expand All @@ -244,7 +253,8 @@ class MetricsSetups private constructor() {
MetadataUtils.newAttachHeadersInterceptor(authHeader),
GrpcTrailerLoggerInterceptor(onLightstepTrailers),
),
timeout = timeout
timeout = timeout,
logRawPayload = logRawPayload,
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class OpentelemetryClient(
private val channel: ManagedChannel,
private val prescientDimensions: PrescientDimensions,
private val timeout: Duration,
private val logRawPayload: (ResourceMetrics) -> Unit = { }
) {
companion object {
fun connect(
Expand All @@ -79,6 +80,7 @@ class OpentelemetryClient(
*/
interceptors: List<ClientInterceptor>,
timeout: Duration = 5.seconds,
logRawPayload: (ResourceMetrics) -> Unit = { },
): OpentelemetryClient {
val channelBuilder = NettyChannelBuilder.forAddress(sillyOtlpHostname, port)
when (securityMode) {
Expand All @@ -98,7 +100,7 @@ class OpentelemetryClient(
}
}
channelBuilder.intercept(interceptors)
return OpentelemetryClient(channelBuilder.build(), prescientDimensions, timeout)
return OpentelemetryClient(channelBuilder.build(), prescientDimensions, timeout, logRawPayload)
}
}
private fun stub(): MetricsServiceGrpcKt.MetricsServiceCoroutineStub = MetricsServiceGrpcKt.MetricsServiceCoroutineStub(
Expand All @@ -109,6 +111,7 @@ class OpentelemetryClient(

suspend fun sendMetricsBatch(batch: List<Metrics>) {
val resourceMetricsBatch = asResourceMetrics(batch)
logRawPayload(resourceMetricsBatch)
stub().export(
exportMetricsServiceRequest {
resourceMetrics.add(resourceMetricsBatch)
Expand All @@ -118,6 +121,7 @@ class OpentelemetryClient(

suspend fun sendPreaggregatedBatch(batch: List<AggregatedBatch>) {
val resourceMetricsBatch = asResourceMetricsFromBatch(batch)
logRawPayload(resourceMetricsBatch)
stub().export(
exportMetricsServiceRequest {
resourceMetrics.add(resourceMetricsBatch)
Expand Down Expand Up @@ -295,17 +299,21 @@ class OpentelemetryClient(
timeUnixNano = timestampNanos
val sorted = this@asOtlpHistogram.bucketCounts.toSortedMap()

if (sorted.isNotEmpty() && 0 < sorted.firstKey()) {
// Again, a reasonable request from Lightstep to work around their lower-infinity interpretation
// of histogram data. Goodmetrics sends sparse histograms and if lightstep has further issues
// parsing these I might have to expand this to fill in 0's for "missing" buckets or delimit
// empty ranges with 0'd out buckets. They haven't asked for that yet though so I'm not doing it.
explicitBounds.add(bucketBelow(sorted.firstKey()).toDouble())
bucketCounts.add(0)
}

count = this@asOtlpHistogram.bucketCounts.values.sumOf { it.sum() }
for ((bucket, count) in sorted) {
val below = bucketBelow(bucket)
if (0 < below && !this@asOtlpHistogram.bucketCounts.containsKey(below)) {
// And THIS little humdinger is here so Lightstep can interpret the boundary for all non-zero
// buckets. Lightstep histogram implementation wants non-zero-count ranges to have lower bounds.
// Not how I've done histograms in the past but :shrug: whatever, looks like the opentelemetry
// metrics spec is at fault for this one; they refused to improve the specification from
// openmetrics, which was bastardized in turn by that root of all monitoring evil: Prometheus.
// Lightstep is a business which must adhere to de-facto standards, so I don't fault them for
// this; though I would love it if they were to also adopt a good protocol.
explicitBounds.add(below.toDouble())
bucketCounts.add(0L)
}

explicitBounds.add(bucket.toDouble())
bucketCounts.add(count.sum())
}
Expand Down