diff --git a/library/common/engine.cc b/library/common/engine.cc index b607eb2690..37fbc35509 100644 --- a/library/common/engine.cc +++ b/library/common/engine.cc @@ -93,14 +93,14 @@ Engine::~Engine() { main_thread_.join(); } -void Engine::incCounter(std::string elemenets) { +void Engine::recordCounter(std::string elemenets, uint64_t count) { if (server_) { - server_->dispatcher().post([this, elemenets]() -> void { + server_->dispatcher().post([this, elemenets, count]() -> void { std::string client_stats = "client_stats"; absl::string_view prefix{client_stats}; absl::string_view dynamic_elements{elemenets}; Stats::Utility::counterFromElements( - server_->serverFactoryContext().scope(), {prefix, dynamic_elements}).inc(); + server_->serverFactoryContext().scope(), {prefix, dynamic_elements}).add(count); }); } } diff --git a/library/common/engine.h b/library/common/engine.h index 2bb2ec574b..db0b220404 100644 --- a/library/common/engine.h +++ b/library/common/engine.h @@ -36,9 +36,9 @@ class Engine { Http::Dispatcher& httpDispatcher(); /** - * Increment a counter with a given string of elements. + * Increment a counter with a given string of elements and by the given count. */ - void incCounter(std::string elemenets); + void recordCounter(std::string elemenets, uint64_t count); /** * Flush the stats sinks outside of a flushing interval. diff --git a/library/common/jni_interface.cc b/library/common/jni_interface.cc index 1059a37177..a9b3b514ca 100644 --- a/library/common/jni_interface.cc +++ b/library/common/jni_interface.cc @@ -78,10 +78,11 @@ Java_io_envoyproxy_envoymobile_engine_AndroidJniLibrary_setPreferredNetwork(JNIE } extern "C" JNIEXPORT void JNICALL -Java_io_envoyproxy_envoymobile_engine_JniLibrary_incCounter(JNIEnv* env, +Java_io_envoyproxy_envoymobile_engine_JniLibrary_recordCounter(JNIEnv* env, jclass, // class - jstring elements) { - inc_counter(env->GetStringUTFChars(elements, nullptr)); + jstring elements, + jint count) { + record_counter(env->GetStringUTFChars(elements, nullptr), count); } extern "C" JNIEXPORT void JNICALL diff --git a/library/common/main_interface.cc b/library/common/main_interface.cc index ae8ff47e02..253cfc6a34 100644 --- a/library/common/main_interface.cc +++ b/library/common/main_interface.cc @@ -69,9 +69,9 @@ envoy_status_t set_preferred_network(envoy_network_t network) { return ENVOY_SUCCESS; } -void inc_counter(const char* elements) { +void record_counter(const char* elements, uint64_t count) { if (auto e = engine_.lock()) { - e->incCounter(std::string(elements)); + e->recordCounter(std::string(elements), count); } } diff --git a/library/common/main_interface.h b/library/common/main_interface.h index 0d05719ef6..a34a5fb4cb 100644 --- a/library/common/main_interface.h +++ b/library/common/main_interface.h @@ -92,9 +92,9 @@ envoy_engine_t init_engine(); envoy_status_t set_preferred_network(envoy_network_t network); /** - * Increment a counter with the given elements. + * Increment a counter with the given elements and by the given count. */ -void inc_counter(const char* elements); +void record_counter(const char* elements, uint64_t count); /** * Flush the stats sinks outside of a flushing interval. diff --git a/library/java/src/io/envoyproxy/envoymobile/engine/AndroidEngineImpl.java b/library/java/src/io/envoyproxy/envoymobile/engine/AndroidEngineImpl.java index 66fcf0337b..201d25f043 100644 --- a/library/java/src/io/envoyproxy/envoymobile/engine/AndroidEngineImpl.java +++ b/library/java/src/io/envoyproxy/envoymobile/engine/AndroidEngineImpl.java @@ -37,7 +37,7 @@ public int runWithConfig(EnvoyConfiguration envoyConfiguration, String logLevel) } @Override - public void incCounter(String elements) { - envoyEngine.incCounter(elements); + public void recordCounter(String elements, int count) { + envoyEngine.recordCounter(elements, count); } } diff --git a/library/java/src/io/envoyproxy/envoymobile/engine/EnvoyEngine.java b/library/java/src/io/envoyproxy/envoymobile/engine/EnvoyEngine.java index 400f7e9e35..25fbd5faf9 100644 --- a/library/java/src/io/envoyproxy/envoymobile/engine/EnvoyEngine.java +++ b/library/java/src/io/envoyproxy/envoymobile/engine/EnvoyEngine.java @@ -30,9 +30,10 @@ public interface EnvoyEngine { int runWithConfig(EnvoyConfiguration envoyConfiguration, String logLevel); /** - * Increment a counter. + * Increment the counter with given count. * * @param elements Elements of the counter. + * @param count Count to increment the counter with. */ - void incCounter(String elements); + void recordCounter(String elements, int count); } diff --git a/library/java/src/io/envoyproxy/envoymobile/engine/EnvoyEngineImpl.java b/library/java/src/io/envoyproxy/envoymobile/engine/EnvoyEngineImpl.java index e39b9a9448..bc6f2071db 100644 --- a/library/java/src/io/envoyproxy/envoymobile/engine/EnvoyEngineImpl.java +++ b/library/java/src/io/envoyproxy/envoymobile/engine/EnvoyEngineImpl.java @@ -54,12 +54,13 @@ public int runWithConfig(EnvoyConfiguration envoyConfiguration, String logLevel) } /** - * Increment a counter. + * Increment the counter with given count. * * @param elements Elements of the counter. + * @param count Count to increment the counter with. */ @Override - public void incCounter(String elements) { - JniLibrary.incCounter(elements); + public void recordCounter(String elements, int count) { + JniLibrary.recordCounter(elements, count); } } diff --git a/library/java/src/io/envoyproxy/envoymobile/engine/JniLibrary.java b/library/java/src/io/envoyproxy/envoymobile/engine/JniLibrary.java index 9d4a59ce5a..7b465d78d1 100644 --- a/library/java/src/io/envoyproxy/envoymobile/engine/JniLibrary.java +++ b/library/java/src/io/envoyproxy/envoymobile/engine/JniLibrary.java @@ -143,5 +143,5 @@ private static class JavaLoader { /** * Increment a counter. */ - protected static native void incCounter(String elements); + protected static native void recordCounter(String elements, int count); } diff --git a/library/kotlin/src/io/envoyproxy/envoymobile/BUILD b/library/kotlin/src/io/envoyproxy/envoymobile/BUILD index dee0219052..a4983f95d8 100644 --- a/library/kotlin/src/io/envoyproxy/envoymobile/BUILD +++ b/library/kotlin/src/io/envoyproxy/envoymobile/BUILD @@ -36,6 +36,7 @@ kt_android_library( envoy_mobile_kt_library( name = "envoy_interfaces_lib", srcs = glob([ + "Counter.kt", "Engine.kt", "EngineBuilder.kt", "EngineImpl.kt", @@ -54,8 +55,6 @@ envoy_mobile_kt_library( "ResponseTrailersBuilder.kt", "RetryPolicy.kt", "RetryPolicyMapper.kt", - "Stat.kt", - "StatBuilder.kt", "StatsClient.kt", "StatsClientImpl.kt", "Stream.kt", diff --git a/library/kotlin/src/io/envoyproxy/envoymobile/Counter.kt b/library/kotlin/src/io/envoyproxy/envoymobile/Counter.kt index 3c8c1acc7d..ccf3f4e6dd 100644 --- a/library/kotlin/src/io/envoyproxy/envoymobile/Counter.kt +++ b/library/kotlin/src/io/envoyproxy/envoymobile/Counter.kt @@ -8,15 +8,15 @@ import java.lang.ref.WeakReference * * Current the supported stat type is counter, and it can increment. */ -class Stat internal constructor( +class Counter internal constructor( private val envoyEngine: WeakReference, private val elements: List ) { /** - * Increment the stat. + * Increment the counter by the given count. */ - fun incCounter() { - envoyEngine.get()?.incCounter(elements.joinToString(separator = ".")) + fun increment(count: Int) { + envoyEngine.get()?.recordCounter(elements.joinToString(separator = "."), count) } } \ No newline at end of file diff --git a/library/kotlin/src/io/envoyproxy/envoymobile/StatBuilder.kt b/library/kotlin/src/io/envoyproxy/envoymobile/StatBuilder.kt deleted file mode 100644 index 86ce23d49b..0000000000 --- a/library/kotlin/src/io/envoyproxy/envoymobile/StatBuilder.kt +++ /dev/null @@ -1,21 +0,0 @@ -package io.envoyproxy.envoymobile - -import java.lang.ref.WeakReference - -import io.envoyproxy.envoymobile.engine.EnvoyEngine - -/** - * A builder to build a {@link Stat} - */ -class StatBuilder constructor(private val envoyEngine: WeakReference) { - private lateinit var elements: List - - fun withElements(elements: List): StatBuilder { - this.elements = elements - return this - } - - fun build(): Stat { - return Stat(envoyEngine, elements) - } -} \ No newline at end of file diff --git a/library/kotlin/src/io/envoyproxy/envoymobile/StatsClient.kt b/library/kotlin/src/io/envoyproxy/envoymobile/StatsClient.kt index 84cd9ecbbb..e7656080e1 100644 --- a/library/kotlin/src/io/envoyproxy/envoymobile/StatsClient.kt +++ b/library/kotlin/src/io/envoyproxy/envoymobile/StatsClient.kt @@ -6,7 +6,7 @@ package io.envoyproxy.envoymobile interface StatsClient { /** - * @return a {@link StatBuilder} + * @return a counter instantiated with the given elements. */ - fun statBuilder(): StatBuilder + fun getCounter(elements: List): Counter } \ No newline at end of file diff --git a/library/kotlin/src/io/envoyproxy/envoymobile/StatsClientImpl.kt b/library/kotlin/src/io/envoyproxy/envoymobile/StatsClientImpl.kt index d567ca7b43..4b91330dfb 100644 --- a/library/kotlin/src/io/envoyproxy/envoymobile/StatsClientImpl.kt +++ b/library/kotlin/src/io/envoyproxy/envoymobile/StatsClientImpl.kt @@ -9,7 +9,7 @@ import java.lang.ref.WeakReference internal class StatsClientImpl constructor(private val envoyEngine: EnvoyEngine) : StatsClient { /** - * @return a {@link StatBuilder} + * @return a counter instantiated with the given elements. */ - override fun statBuilder(): StatBuilder = StatBuilder(WeakReference(envoyEngine)) + override fun getCounter(elements: List): Counter = Counter(WeakReference(envoyEngine), elements) } \ No newline at end of file diff --git a/library/kotlin/src/io/envoyproxy/envoymobile/mocks/MockEnvoyEngine.kt b/library/kotlin/src/io/envoyproxy/envoymobile/mocks/MockEnvoyEngine.kt index bb46b823cf..24a96ff0c8 100644 --- a/library/kotlin/src/io/envoyproxy/envoymobile/mocks/MockEnvoyEngine.kt +++ b/library/kotlin/src/io/envoyproxy/envoymobile/mocks/MockEnvoyEngine.kt @@ -15,5 +15,5 @@ internal class MockEnvoyEngine : EnvoyEngine { override fun startStream(callbacks: EnvoyHTTPCallbacks?): EnvoyHTTPStream = MockEnvoyHTTPStream(callbacks!!) - override fun incCounter(name: String) {} + override fun recordCounter(elements: String, count: Int) {} }