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

feat: telemetry coroutine extensions #864

Merged
merged 3 commits into from
Jun 16, 2023
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
52 changes: 46 additions & 6 deletions runtime/observability/telemetry-api/api/telemetry-api.api
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
public abstract interface class aws/smithy/kotlin/runtime/telemetry/TelemetryProvider {
public static final field Companion Laws/smithy/kotlin/runtime/telemetry/TelemetryProvider$Companion;
public abstract fun getContextManager ()Laws/smithy/kotlin/runtime/telemetry/context/ContextManager;
public abstract fun getLoggerProvider ()Laws/smithy/kotlin/runtime/telemetry/logging/LoggerProvider;
public abstract fun getMeterProvider ()Laws/smithy/kotlin/runtime/telemetry/metrics/MeterProvider;
public abstract fun getTracerProvider ()Laws/smithy/kotlin/runtime/telemetry/trace/TracerProvider;
Expand All @@ -9,14 +10,42 @@ public final class aws/smithy/kotlin/runtime/telemetry/TelemetryProvider$Compani
public final fun getNone ()Laws/smithy/kotlin/runtime/telemetry/TelemetryProvider;
}

public final class aws/smithy/kotlin/runtime/telemetry/TelemetryProviderContext$Key : kotlin/coroutines/CoroutineContext$Key {
}

public final class aws/smithy/kotlin/runtime/telemetry/TelemetryProviderContextKt {
}

public abstract interface class aws/smithy/kotlin/runtime/telemetry/context/Context {
public static final field Companion Laws/smithy/kotlin/runtime/telemetry/context/Context$Companion;
public abstract fun makeCurrent ()Laws/smithy/kotlin/runtime/telemetry/context/Scope;
}

public final class aws/smithy/kotlin/runtime/telemetry/context/Context$Companion {
public final fun getNone ()Laws/smithy/kotlin/runtime/telemetry/context/Context;
}

public abstract interface class aws/smithy/kotlin/runtime/telemetry/context/ContextManager {
public static final field Companion Laws/smithy/kotlin/runtime/telemetry/context/ContextManager$Companion;
public abstract fun current ()Laws/smithy/kotlin/runtime/telemetry/context/Context;
}

public final class aws/smithy/kotlin/runtime/telemetry/context/ContextManager$Companion {
public final fun getNone ()Laws/smithy/kotlin/runtime/telemetry/context/ContextManager;
}

public abstract interface class aws/smithy/kotlin/runtime/telemetry/context/Scope : java/io/Closeable {
}

public final class aws/smithy/kotlin/runtime/telemetry/context/TelemetryContextElement$Key : kotlin/coroutines/CoroutineContext$Key {
}

public final class aws/smithy/kotlin/runtime/telemetry/context/TelemetryContextElementKt {
}

public final class aws/smithy/kotlin/runtime/telemetry/logging/CoroutineContextLogExtKt {
}

public final class aws/smithy/kotlin/runtime/telemetry/logging/LogLevel : java/lang/Enum {
public static final field Debug Laws/smithy/kotlin/runtime/telemetry/logging/LogLevel;
public static final field Error Laws/smithy/kotlin/runtime/telemetry/logging/LogLevel;
Expand All @@ -30,17 +59,18 @@ public final class aws/smithy/kotlin/runtime/telemetry/logging/LogLevel : java/l

public abstract interface class aws/smithy/kotlin/runtime/telemetry/logging/LogRecordBuilder {
public abstract fun emit ()V
public abstract fun setAllAttributes (Laws/smithy/kotlin/runtime/util/Attributes;)V
public abstract fun mergeAttributes (Laws/smithy/kotlin/runtime/util/Attributes;)V
public abstract fun setAttribute (Ljava/lang/String;Ljava/lang/Object;)V
public abstract fun setCause (Ljava/lang/Throwable;)V
public abstract fun setContext (Laws/smithy/kotlin/runtime/telemetry/context/Context;)V
public abstract fun setLevel (Laws/smithy/kotlin/runtime/telemetry/logging/LogLevel;)V
public abstract fun setMessage (Ljava/lang/String;)V
public abstract fun setMessage (Lkotlin/jvm/functions/Function0;)V
public abstract fun setTimestamp (Laws/smithy/kotlin/runtime/time/Instant;)V
}

public final class aws/smithy/kotlin/runtime/telemetry/logging/LogRecordBuilder$DefaultImpls {
public static fun setAllAttributes (Laws/smithy/kotlin/runtime/telemetry/logging/LogRecordBuilder;Laws/smithy/kotlin/runtime/util/Attributes;)V
public static fun mergeAttributes (Laws/smithy/kotlin/runtime/telemetry/logging/LogRecordBuilder;Laws/smithy/kotlin/runtime/util/Attributes;)V
}

public abstract interface class aws/smithy/kotlin/runtime/telemetry/logging/Logger {
Expand Down Expand Up @@ -145,6 +175,9 @@ public final class aws/smithy/kotlin/runtime/telemetry/metrics/UpDownCounter$Def
public static synthetic fun add$default (Laws/smithy/kotlin/runtime/telemetry/metrics/UpDownCounter;JLaws/smithy/kotlin/runtime/util/Attributes;Laws/smithy/kotlin/runtime/telemetry/context/Context;ILjava/lang/Object;)V
}

public final class aws/smithy/kotlin/runtime/telemetry/trace/CoroutineContextTraceExtKt {
}

public final class aws/smithy/kotlin/runtime/telemetry/trace/SpanKind : java/lang/Enum {
public static final field CLIENT Laws/smithy/kotlin/runtime/telemetry/trace/SpanKind;
public static final field INTERNAL Laws/smithy/kotlin/runtime/telemetry/trace/SpanKind;
Expand All @@ -161,10 +194,9 @@ public final class aws/smithy/kotlin/runtime/telemetry/trace/SpanStatus : java/l
public static fun values ()[Laws/smithy/kotlin/runtime/telemetry/trace/SpanStatus;
}

public abstract interface class aws/smithy/kotlin/runtime/telemetry/trace/TraceSpan : java/io/Closeable {
public abstract interface class aws/smithy/kotlin/runtime/telemetry/trace/TraceSpan : aws/smithy/kotlin/runtime/telemetry/context/Scope {
public abstract fun close ()V
public abstract fun emitEvent (Ljava/lang/String;Laws/smithy/kotlin/runtime/util/Attributes;)V
public abstract fun getContext ()Laws/smithy/kotlin/runtime/telemetry/context/Context;
public abstract fun getName ()Ljava/lang/String;
public abstract fun mergeAttributes (Laws/smithy/kotlin/runtime/util/Attributes;)V
public abstract fun set (Laws/smithy/kotlin/runtime/util/AttributeKey;Ljava/lang/Object;)V
Expand All @@ -175,12 +207,20 @@ public final class aws/smithy/kotlin/runtime/telemetry/trace/TraceSpan$DefaultIm
public static synthetic fun emitEvent$default (Laws/smithy/kotlin/runtime/telemetry/trace/TraceSpan;Ljava/lang/String;Laws/smithy/kotlin/runtime/util/Attributes;ILjava/lang/Object;)V
}

public final class aws/smithy/kotlin/runtime/telemetry/trace/TraceSpanContext$Key : kotlin/coroutines/CoroutineContext$Key {
}

public final class aws/smithy/kotlin/runtime/telemetry/trace/TraceSpanExtKt {
public static final fun recordException (Laws/smithy/kotlin/runtime/telemetry/trace/TraceSpan;Ljava/lang/Throwable;Z)V
public static final fun setAttribute (Laws/smithy/kotlin/runtime/telemetry/trace/TraceSpan;Ljava/lang/String;Ljava/lang/Object;)V
}

public abstract interface class aws/smithy/kotlin/runtime/telemetry/trace/Tracer {
public abstract fun createSpan (Ljava/lang/String;Laws/smithy/kotlin/runtime/telemetry/context/Context;Laws/smithy/kotlin/runtime/util/Attributes;Laws/smithy/kotlin/runtime/telemetry/trace/SpanKind;)Laws/smithy/kotlin/runtime/telemetry/trace/TraceSpan;
public abstract fun createSpan (Ljava/lang/String;Laws/smithy/kotlin/runtime/util/Attributes;Laws/smithy/kotlin/runtime/telemetry/trace/SpanKind;Laws/smithy/kotlin/runtime/telemetry/context/Context;)Laws/smithy/kotlin/runtime/telemetry/trace/TraceSpan;
}

public final class aws/smithy/kotlin/runtime/telemetry/trace/Tracer$DefaultImpls {
public static synthetic fun createSpan$default (Laws/smithy/kotlin/runtime/telemetry/trace/Tracer;Ljava/lang/String;Laws/smithy/kotlin/runtime/telemetry/context/Context;Laws/smithy/kotlin/runtime/util/Attributes;Laws/smithy/kotlin/runtime/telemetry/trace/SpanKind;ILjava/lang/Object;)Laws/smithy/kotlin/runtime/telemetry/trace/TraceSpan;
public static synthetic fun createSpan$default (Laws/smithy/kotlin/runtime/telemetry/trace/Tracer;Ljava/lang/String;Laws/smithy/kotlin/runtime/util/Attributes;Laws/smithy/kotlin/runtime/telemetry/trace/SpanKind;Laws/smithy/kotlin/runtime/telemetry/context/Context;ILjava/lang/Object;)Laws/smithy/kotlin/runtime/telemetry/trace/TraceSpan;
}

public abstract interface class aws/smithy/kotlin/runtime/telemetry/trace/TracerProvider {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

package aws.smithy.kotlin.runtime.telemetry

import aws.smithy.kotlin.runtime.telemetry.context.Context
import aws.smithy.kotlin.runtime.telemetry.context.ContextManager
import aws.smithy.kotlin.runtime.telemetry.logging.LoggerProvider
import aws.smithy.kotlin.runtime.telemetry.metrics.MeterProvider
import aws.smithy.kotlin.runtime.telemetry.trace.TracerProvider
Expand Down Expand Up @@ -34,10 +36,16 @@ public interface TelemetryProvider {
* Get the [LoggerProvider] used to create new [aws.smithy.kotlin.runtime.telemetry.logging.Logger] instances
*/
public val loggerProvider: LoggerProvider

/**
* Get the [ContextManager] used to get the current [Context]
*/
public val contextManager: ContextManager
}

private object NoOpTelemetryProvider : TelemetryProvider {
override val meterProvider: MeterProvider = MeterProvider.None
override val tracerProvider: TracerProvider = TracerProvider.None
override val loggerProvider: LoggerProvider = LoggerProvider.None
override val contextManager: ContextManager = ContextManager.None
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

package aws.smithy.kotlin.runtime.telemetry

import aws.smithy.kotlin.runtime.InternalApi
import kotlin.coroutines.AbstractCoroutineContextElement
import kotlin.coroutines.CoroutineContext

/**
* Coroutine scoped telemetry context used for carrying telemetry provider configuration
* @param provider The telemetry provider to instrument with
*/
@InternalApi
public data class TelemetryProviderContext(
val provider: TelemetryProvider,
) : AbstractCoroutineContextElement(TelemetryProviderContext) {
public companion object Key : CoroutineContext.Key<TelemetryProviderContext>
override fun toString(): String = "TelemetryContext($provider)"
}

/**
* Get the active [TelemetryProvider] from this [CoroutineContext]. If none exists
* a no-op provider will be returned.
*/
@InternalApi
public val CoroutineContext.telemetryProvider: TelemetryProvider
get() = get(TelemetryProviderContext)?.provider ?: TelemetryProvider.None
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,22 @@ public interface Context {
/**
* A no-op [Context]
*/
public val None: Context = object : Context {}
public val None: Context = NoOpContext
}

/**
* Make this the currently active context
*
* @return handle to a [Scope] that MUST be disposed of (via [Scope.close]) when the current context
* is considered no longer active (returning the current active context to whatever it was previously if set).
*/
public fun makeCurrent(): Scope
}

private object NoOpContext : Context {
override fun makeCurrent(): Scope = NoOpScope
}

private object NoOpScope : Scope {
override fun close() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

package aws.smithy.kotlin.runtime.telemetry.context

/**
* Responsible for managing the current context with callers current execution unit. For example, some implementations
* use Thread Local storage for managing the current context.
*/
public interface ContextManager {
public companion object {
public val None: ContextManager = NoOpContextManager
}

/**
* Return the current [Context]
*/
public fun current(): Context
}

private object NoOpContextManager : ContextManager {
override fun current(): Context = Context.None
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

package aws.smithy.kotlin.runtime.telemetry.context

import aws.smithy.kotlin.runtime.io.Closeable

/**
* Delineates a logical scope that has a beginning and end (e.g. a function)
*/
public interface Scope : Closeable
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

package aws.smithy.kotlin.runtime.telemetry.context

import aws.smithy.kotlin.runtime.InternalApi
import kotlin.coroutines.CoroutineContext

/**
* A [CoroutineContext] element that carries a telemetry [Context].
* @param context The active context
*/
@InternalApi
public expect class TelemetryContextElement(context: Context) : CoroutineContext.Element {
public companion object Key : CoroutineContext.Key<TelemetryContextElement>

public val context: Context
}

/**
* Extract the current telemetry [Context] from the coroutine context if available.
*/
@InternalApi
public val CoroutineContext.telemetryContext: Context?
get() = get(TelemetryContextElement)?.context
Loading