Skip to content

Commit

Permalink
update interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Jingwei Hao committed Jul 30, 2020
1 parent 57283c7 commit 7202a8d
Show file tree
Hide file tree
Showing 15 changed files with 33 additions and 52 deletions.
6 changes: 3 additions & 3 deletions library/common/engine.cc
Expand Up @@ -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);
});
}
}
Expand Down
4 changes: 2 additions & 2 deletions library/common/engine.h
Expand Up @@ -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.
Expand Down
7 changes: 4 additions & 3 deletions library/common/jni_interface.cc
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions library/common/main_interface.cc
Expand Up @@ -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);
}
}

Expand Down
4 changes: 2 additions & 2 deletions library/common/main_interface.h
Expand Up @@ -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.
Expand Down
Expand Up @@ -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);
}
}
Expand Up @@ -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);
}
Expand Up @@ -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);
}
}
Expand Up @@ -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);
}
3 changes: 1 addition & 2 deletions library/kotlin/src/io/envoyproxy/envoymobile/BUILD
Expand Up @@ -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",
Expand All @@ -54,8 +55,6 @@ envoy_mobile_kt_library(
"ResponseTrailersBuilder.kt",
"RetryPolicy.kt",
"RetryPolicyMapper.kt",
"Stat.kt",
"StatBuilder.kt",
"StatsClient.kt",
"StatsClientImpl.kt",
"Stream.kt",
Expand Down
8 changes: 4 additions & 4 deletions library/kotlin/src/io/envoyproxy/envoymobile/Counter.kt
Expand Up @@ -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<EnvoyEngine>,
private val elements: List<String>
) {

/**
* 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)
}
}
21 changes: 0 additions & 21 deletions library/kotlin/src/io/envoyproxy/envoymobile/StatBuilder.kt

This file was deleted.

4 changes: 2 additions & 2 deletions library/kotlin/src/io/envoyproxy/envoymobile/StatsClient.kt
Expand Up @@ -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<String>): Counter
}
Expand Up @@ -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<String>): Counter = Counter(WeakReference(envoyEngine), elements)
}
Expand Up @@ -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) {}
}

0 comments on commit 7202a8d

Please sign in to comment.