Skip to content

Commit

Permalink
split perscient dimensions into dedicated resource dimensions and sha…
Browse files Browse the repository at this point in the history
…red dimensions (#36)

otlp receivers like chronosphere need both of these configurations
  • Loading branch information
tylerburdsall authored Mar 14, 2024
1 parent a6ba19a commit f1367b1
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 29 deletions.
30 changes: 20 additions & 10 deletions kotlin/goodmetrics/src/main/kotlin/goodmetrics/MetricsSetups.kt
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ class MetricsSetups private constructor() {
*/
fun CoroutineScope.lightstepNativeOtlp(
lightstepAccessToken: String,
prescientDimensions: PrescientDimensions,
prescientResourceDimensions: PrescientDimensions.AsResource,
prescientSharedDimensions: PrescientDimensions.AsDimensions,
aggregationWidth: Duration,
logError: (message: String, exception: Exception) -> Unit,
onLightstepTrailers: (Status, Metadata) -> Unit = { status, trailers ->
Expand Down Expand Up @@ -100,7 +101,8 @@ class MetricsSetups private constructor() {
"lightstep-access-token",
lightstepUrl,
lightstepPort,
prescientDimensions,
prescientResourceDimensions = prescientResourceDimensions,
prescientSharedDimensions = prescientSharedDimensions,
lightstepConnectionSecurityMode,
onLightstepTrailers,
timeout,
Expand Down Expand Up @@ -134,7 +136,8 @@ class MetricsSetups private constructor() {
*/
fun lightstepNativeOtlpButItSendsMetricsUponRecordingForLambda(
lightstepAccessToken: String,
prescientDimensions: PrescientDimensions,
prescientResourceDimensions: PrescientDimensions.AsResource,
prescientSharedDimensions: PrescientDimensions.AsDimensions,
logError: (message: String, exception: Exception) -> Unit,
onLightstepTrailers: (Status, Metadata) -> Unit = { status, trailers ->
println("got trailers from lightstep. Status: $status, Trailers: $trailers")
Expand All @@ -151,7 +154,8 @@ class MetricsSetups private constructor() {
"lightstep-access-token",
lightstepUrl,
lightstepPort,
prescientDimensions,
prescientResourceDimensions = prescientResourceDimensions,
prescientSharedDimensions = prescientSharedDimensions,
lightstepConnectionSecurityMode,
onLightstepTrailers,
timeout,
Expand All @@ -167,7 +171,8 @@ class MetricsSetups private constructor() {
fun CoroutineScope.rawNativeOtlp(
accessToken: String,
authHeaderName: String,
prescientDimensions: PrescientDimensions,
prescientResourceDimensions: PrescientDimensions.AsResource,
prescientSharedDimensions: PrescientDimensions.AsDimensions,
aggregationWidth: Duration,
logError: (message: String, exception: Exception) -> Unit,
onIngestTrailers: (Status, Metadata) -> Unit = { status, trailers ->
Expand Down Expand Up @@ -196,7 +201,8 @@ class MetricsSetups private constructor() {
authHeaderName,
ingestUrl,
ingestPort,
prescientDimensions,
prescientResourceDimensions = prescientResourceDimensions,
prescientSharedDimensions = prescientSharedDimensions,
connectionSecurityMode,
onIngestTrailers,
timeout,
Expand Down Expand Up @@ -231,7 +237,8 @@ class MetricsSetups private constructor() {
fun rawNativeOtlpButItSendsMetricsUponRecordingForLambda(
accessToken: String,
authHeaderName: String,
prescientDimensions: PrescientDimensions,
prescientResourceDimensions: PrescientDimensions.AsResource,
prescientSharedDimensions: PrescientDimensions.AsDimensions,
logError: (message: String, exception: Exception) -> Unit,
onIngestTrailers: (Status, Metadata) -> Unit = { status, trailers ->
println("got trailers from ingest. Status: $status, Trailers: $trailers")
Expand All @@ -248,7 +255,8 @@ class MetricsSetups private constructor() {
authHeaderName,
ingestUrl,
ingestPort,
prescientDimensions,
prescientResourceDimensions = prescientResourceDimensions,
prescientSharedDimensions = prescientSharedDimensions,
connectionSecurityMode,
onIngestTrailers,
timeout,
Expand Down Expand Up @@ -382,7 +390,8 @@ class MetricsSetups private constructor() {
authHeaderName: String,
ingestUrl: String,
ingestPort: Int,
prescientDimensions: PrescientDimensions,
prescientResourceDimensions: PrescientDimensions.AsResource,
prescientSharedDimensions: PrescientDimensions.AsDimensions,
securityMode: SecurityMode,
onIngestTrailers: (Status, Metadata) -> Unit,
timeout: Duration,
Expand All @@ -398,7 +407,8 @@ class MetricsSetups private constructor() {
return OpentelemetryClient.connect(
sillyOtlpHostname = ingestUrl,
port = ingestPort,
prescientDimensions = prescientDimensions,
prescientResourceDimensions = prescientResourceDimensions,
prescientSharedDimensions = prescientSharedDimensions,
securityMode = securityMode,
interceptors = listOf(
MetadataUtils.newAttachHeadersInterceptor(authHeader),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ sealed interface CompressionMode {
*/
class OpentelemetryClient(
private val channel: ManagedChannel,
private val prescientDimensions: PrescientDimensions,
private val prescientResourceDimensions: PrescientDimensions.AsResource,
private val prescientSharedDimensions: PrescientDimensions.AsDimensions,
private val timeout: Duration,
private val logRawPayload: (ResourceMetrics) -> Unit = { },
private val compressionMode: CompressionMode,
Expand All @@ -79,7 +80,8 @@ class OpentelemetryClient(
fun connect(
sillyOtlpHostname: String = "localhost",
port: Int = 5001,
prescientDimensions: PrescientDimensions,
prescientResourceDimensions: PrescientDimensions.AsResource,
prescientSharedDimensions: PrescientDimensions.AsDimensions,
securityMode: SecurityMode,
/**
* stuff like MetadataUtils.newAttachHeadersInterceptor()
Expand Down Expand Up @@ -107,7 +109,7 @@ class OpentelemetryClient(
}
}
channelBuilder.intercept(interceptors)
return OpentelemetryClient(channelBuilder.build(), prescientDimensions, timeout, logRawPayload, compressionMode)
return OpentelemetryClient(channelBuilder.build(), prescientResourceDimensions, prescientSharedDimensions, timeout, logRawPayload, compressionMode)
}
}
private fun stub(): MetricsServiceGrpcKt.MetricsServiceCoroutineStub {
Expand Down Expand Up @@ -144,7 +146,7 @@ class OpentelemetryClient(

private fun asResourceMetricsFromBatch(batch: List<AggregatedBatch>): ResourceMetrics {
return resourceMetrics {
prescientResource?.let { this.resource = it }
resource = prescientResource
for (aggregate in batch) {
this.scopeMetrics.add(aggregate.asOtlpScopeMetrics())
}
Expand All @@ -157,7 +159,7 @@ class OpentelemetryClient(
}

private fun asResourceMetrics(batch: List<Metrics>): ResourceMetrics = resourceMetrics {
prescientResource?.let { this.resource = it }
resource = prescientResource
this.scopeMetrics.add(asScopeMetrics(batch))
}

Expand All @@ -170,10 +172,7 @@ class OpentelemetryClient(
for ((position, measurements) in this@asGoofyOtlpMetricSequence.positions) {
// Push down our shared dimensions to each datum leaf if required. For systems that may ingest OTLP metrics
// but use a different backing system (e.g. OTLP -> Prometheus)
val otlpDimensions = position.map { it.asOtlpKeyValue() } + when (prescientDimensions) {
is PrescientDimensions.AsDimensions -> prescientDimensions.sharedDimensions.asOtlpDimensions()
is PrescientDimensions.AsResource -> emptySequence()
}
val otlpDimensions = position.map { it.asOtlpKeyValue() } + prescientSharedDimensions.sharedDimensions.asOtlpDimensions()
for ((measurementName, aggregation) in measurements) {
when (aggregation) {
is Aggregation.Histogram -> {
Expand Down Expand Up @@ -362,15 +361,8 @@ class OpentelemetryClient(
}

private val prescientResource by lazy {
when (prescientDimensions) {
is PrescientDimensions.AsDimensions -> {
null
}
is PrescientDimensions.AsResource -> {
resource {
attributes.addAll(prescientDimensions.resourceDimensions.asOtlpDimensions().asIterable())
}
}
resource {
attributes.addAll(prescientResourceDimensions.resourceDimensions.asOtlpDimensions().asIterable())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ fun main() {
runBlocking {
val configuredMetrics = lightstepNativeOtlp(
lightstepAccessToken = System.getenv("lightsteptoken") ?: "none",
prescientDimensions = PrescientDimensions.AsResource(
prescientResourceDimensions = PrescientDimensions.AsResource(
mapOf(
"service.name" to Metrics.Dimension.String("service.name", "goodmetrics_test"),
"service.version" to Metrics.Dimension.String("service.version", OpentelemetryClient::class.java.`package`.implementationVersion ?: "dev"),
"host.hostname" to Metrics.Dimension.String("host.hostname", InetAddress.getLocalHost().hostName)
)
),
prescientSharedDimensions = PrescientDimensions.AsDimensions(emptyMap()),
aggregationWidth = 10.seconds,
logError = { message, exception -> println("metrics error: $message, ex: ${exception.message}") },
onSendUnary = { println("sending unary batch size: ${it.size}") },
Expand Down

0 comments on commit f1367b1

Please sign in to comment.