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: add runtime version metadata to the first Momento call #66

Merged
merged 6 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
8 changes: 7 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ jobs:

android:
# The Android emulator only has hardware acceleration on macOS.
runs-on: macos-latest
runs-on: ubuntu-latest
strategy:
matrix:
api-level: [ 23 ]
Expand All @@ -72,6 +72,12 @@ jobs:
with:
arguments: clean build -x jvmTest -x testDebugUnitTest -x testReleaseUnitTest

- name: Enable KVM
nand4011 marked this conversation as resolved.
Show resolved Hide resolved
run: |
echo 'KERNEL=="kvm", GROUP="kvm", MODE="0666", OPTIONS+="static_node=kvm"' | sudo tee /etc/udev/rules.d/99-kvm4all.rules
sudo udevadm control --reload-rules
sudo udevadm trigger --name-match=kvm

- name: run tests
uses: reactivecircus/android-emulator-runner@v2
with:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ internal class ControlGrpcStubsManager(credentialProvider: CredentialProvider) :
channel = setupConnection(credentialProvider)
futureStub = ScsControlGrpcKt.ScsControlCoroutineStub(channel)
}

val stub: ScsControlGrpcKt.ScsControlCoroutineStub
/**
* Returns a stub with appropriate deadlines.
Expand All @@ -43,7 +44,7 @@ internal class ControlGrpcStubsManager(credentialProvider: CredentialProvider) :
channelBuilder.useTransportSecurity()
channelBuilder.disableRetry()
val clientInterceptors: MutableList<ClientInterceptor> = ArrayList()
clientInterceptors.add(UserHeaderInterceptor("android", credentialProvider.apiKey))
clientInterceptors.add(UserHeaderInterceptor(credentialProvider.apiKey, "cache"))
channelBuilder.intercept(clientInterceptors)
return channelBuilder.build()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import java.util.concurrent.TimeUnit
import kotlin.time.Duration
import kotlin.time.Duration.Companion.minutes

internal class DataGrpcStubsManager(credentialProvider: CredentialProvider, configuration: GrpcConfiguration) : Closeable {
internal class DataGrpcStubsManager(credentialProvider: CredentialProvider, configuration: GrpcConfiguration) :
Closeable {
private val deadline: Duration
private val channel: ManagedChannel
private val futureStub: ScsGrpcKt.ScsCoroutineStub
Expand All @@ -21,6 +22,7 @@ internal class DataGrpcStubsManager(credentialProvider: CredentialProvider, conf
channel = setupConnection(credentialProvider)
futureStub = ScsGrpcKt.ScsCoroutineStub(channel)
}

val stub: ScsGrpcKt.ScsCoroutineStub
/**
* Returns a stub with appropriate deadlines.
Expand All @@ -47,7 +49,7 @@ internal class DataGrpcStubsManager(credentialProvider: CredentialProvider, conf
channelBuilder.useTransportSecurity()
channelBuilder.disableRetry()
val clientInterceptors: MutableList<ClientInterceptor> = ArrayList()
clientInterceptors.add(UserHeaderInterceptor("android", credentialProvider.apiKey))
clientInterceptors.add(UserHeaderInterceptor(credentialProvider.apiKey, "cache"))
channelBuilder.intercept(clientInterceptors)
return channelBuilder.build()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ internal class TopicGrpcStubsManager(credentialProvider: CredentialProvider) : C
channelBuilder.keepAliveTimeout(5, TimeUnit.SECONDS)
channelBuilder.keepAliveWithoutCalls(true)
val clientInterceptors: MutableList<ClientInterceptor> = ArrayList()
clientInterceptors.add(UserHeaderInterceptor("android", credentialProvider.apiKey))
clientInterceptors.add(UserHeaderInterceptor(credentialProvider.apiKey, "cache"))
channelBuilder.intercept(clientInterceptors)
return channelBuilder.build()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package software.momento.kotlin.sdk.internal

import android.os.Build

internal actual class PlatformInfo {
internal actual val runtimeVersion: String
get() {
val kotlinVersion = KotlinVersion.CURRENT
val androidVersion = Build.VERSION.RELEASE
val deviceModel = Build.MODEL
val manufacturer = Build.MANUFACTURER
return "Android $androidVersion, Kotlin $kotlinVersion, $manufacturer $deviceModel"
}

internal actual fun getSdkVersion(clientType: String): String {
val version = this.javaClass.getPackage()?.implementationVersion ?: "unknown"
return "kotlin-android:$clientType:$version"
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package software.momento.kotlin.sdk.internal

import kotlin.jvm.Volatile
internal class UserHeaderInterceptor(private val tokenValue: String, private val clientType: String) :
io.grpc.ClientInterceptor {
private var isUserAgentSent = false

internal class UserHeaderInterceptor(sdkType: String, private val tokenValue: String) : io.grpc.ClientInterceptor {
private val sdkVersion = String.format("kotlin-$sdkType:%s", this.javaClass.getPackage()?.implementationVersion ?: "unknown")
override fun <ReqT, RespT> interceptCall(
methodDescriptor: io.grpc.MethodDescriptor<ReqT, RespT>,
callOptions: io.grpc.CallOptions,
Expand All @@ -15,7 +15,9 @@ internal class UserHeaderInterceptor(sdkType: String, private val tokenValue: St
override fun start(listener: Listener<RespT>, metadata: io.grpc.Metadata) {
metadata.put(AUTH_HEADER_KEY, tokenValue)
if (!isUserAgentSent) {
metadata.put(SDK_AGENT_KEY, sdkVersion)
val platformInfo = PlatformInfo()
metadata.put(SDK_AGENT_KEY, platformInfo.getSdkVersion(clientType))
metadata.put(RUNTIME_VERSION_KEY, platformInfo.runtimeVersion)
isUserAgentSent = true
}
super.start(listener, metadata)
Expand All @@ -25,12 +27,16 @@ internal class UserHeaderInterceptor(sdkType: String, private val tokenValue: St

companion object {
private val AUTH_HEADER_KEY: io.grpc.Metadata.Key<String> =
io.grpc.Metadata.Key.of("Authorization", io.grpc.Metadata.ASCII_STRING_MARSHALLER)
io.grpc.Metadata.Key.of("authorization", io.grpc.Metadata.ASCII_STRING_MARSHALLER)
private val SDK_AGENT_KEY: io.grpc.Metadata.Key<String> =
io.grpc.Metadata.Key.of("Agent", io.grpc.Metadata.ASCII_STRING_MARSHALLER)

@Volatile
private var isUserAgentSent = false
io.grpc.Metadata.Key.of("agent", io.grpc.Metadata.ASCII_STRING_MARSHALLER)
private val RUNTIME_VERSION_KEY: io.grpc.Metadata.Key<String> =
io.grpc.Metadata.Key.of("runtime-version", io.grpc.Metadata.ASCII_STRING_MARSHALLER)
}
}

internal expect class PlatformInfo() {
internal val runtimeVersion: String

internal fun getSdkVersion(clientType: String): String
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package software.momento.kotlin.sdk.internal

import grpc.cache_client.ScsGrpcKt
import grpc.control_client.ScsControlGrpcKt
import io.grpc.ClientInterceptor
import io.grpc.ManagedChannel
Expand All @@ -18,6 +17,7 @@ internal class ControlGrpcStubsManager(credentialProvider: CredentialProvider) :
channel = setupConnection(credentialProvider)
futureStub = ScsControlGrpcKt.ScsControlCoroutineStub(channel)
}

val stub: ScsControlGrpcKt.ScsControlCoroutineStub
/**
* Returns a stub with appropriate deadlines.
Expand All @@ -44,7 +44,7 @@ internal class ControlGrpcStubsManager(credentialProvider: CredentialProvider) :
channelBuilder.useTransportSecurity()
channelBuilder.disableRetry()
val clientInterceptors: MutableList<ClientInterceptor> = ArrayList()
clientInterceptors.add(UserHeaderInterceptor("jvm", credentialProvider.apiKey))
clientInterceptors.add(UserHeaderInterceptor(credentialProvider.apiKey, "cache"))
channelBuilder.intercept(clientInterceptors)
return channelBuilder.build()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import software.momento.kotlin.sdk.config.GrpcConfiguration
import java.io.Closeable
import java.util.concurrent.TimeUnit
import kotlin.time.Duration
import kotlin.time.Duration.Companion.minutes

internal class DataGrpcStubsManager(credentialProvider: CredentialProvider, configuration: GrpcConfiguration) : Closeable {
internal class DataGrpcStubsManager(credentialProvider: CredentialProvider, configuration: GrpcConfiguration) :
Closeable {
private val deadline: Duration
private val channel: ManagedChannel
private val futureStub: ScsGrpcKt.ScsCoroutineStub
Expand All @@ -21,6 +21,7 @@ internal class DataGrpcStubsManager(credentialProvider: CredentialProvider, conf
channel = setupConnection(credentialProvider)
futureStub = ScsGrpcKt.ScsCoroutineStub(channel)
}

val stub: ScsGrpcKt.ScsCoroutineStub
/**
* Returns a stub with appropriate deadlines.
Expand All @@ -46,7 +47,7 @@ internal class DataGrpcStubsManager(credentialProvider: CredentialProvider, conf
channelBuilder.useTransportSecurity()
channelBuilder.disableRetry()
val clientInterceptors: MutableList<ClientInterceptor> = ArrayList()
clientInterceptors.add(UserHeaderInterceptor("jvm", credentialProvider.apiKey))
clientInterceptors.add(UserHeaderInterceptor(credentialProvider.apiKey, "cache"))
channelBuilder.intercept(clientInterceptors)
return channelBuilder.build()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ internal class TopicGrpcStubsManager(credentialProvider: CredentialProvider) : C
channelBuilder.keepAliveTimeout(5, TimeUnit.SECONDS)
channelBuilder.keepAliveWithoutCalls(true)
val clientInterceptors: MutableList<ClientInterceptor> = ArrayList()
clientInterceptors.add(UserHeaderInterceptor("jvm", credentialProvider.apiKey))
clientInterceptors.add(UserHeaderInterceptor(credentialProvider.apiKey, "topic"))
channelBuilder.intercept(clientInterceptors)
return channelBuilder.build()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package software.momento.kotlin.sdk.internal

internal actual class PlatformInfo {
internal actual val runtimeVersion: String
get() {
val javaVendor = System.getProperty("java.vendor")
val javaVersion = System.getProperty("java.version")
val kotlinVersion = KotlinVersion.CURRENT
return "$javaVendor:$javaVersion, Kotlin $kotlinVersion"
}

internal actual fun getSdkVersion(clientType: String): String {
val version = this.javaClass.getPackage()?.implementationVersion ?: "unknown"
return "kotlin-jvm:$clientType:$version"
}
}
Loading