-
Notifications
You must be signed in to change notification settings - Fork 293
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add FeatureObserver to metrics (#2167)
- Loading branch information
Showing
7 changed files
with
156 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Copyright (c) 2017-2022 Cloudflare, Inc. | ||
# Licensed under the Apache 2.0 license found in the LICENSE file or at: | ||
# https://opensource.org/licenses/Apache-2.0 | ||
|
||
@0x8b3d4aaa36221ec9; | ||
|
||
using Cxx = import "/capnp/c++.capnp"; | ||
$Cxx.namespace("workerd"); | ||
$Cxx.allowCancellation; | ||
|
||
enum Features { | ||
test @0; | ||
# A test feature that should never be used in production code. | ||
|
||
# Due to a number of practical limitations on the metrics collection, | ||
# we do not really want the list of features to grow unbounded over | ||
# time. At any given point in time we shouldn't be trying to track | ||
# more than 50 features at a time. | ||
# | ||
# Features we are no longer needing to track can and should be removed, | ||
# just be careful to adjust the index ordinals of the remaining features | ||
# correctly. In code, be sure to never rely on the ordinal value and | ||
# instead always use the features enum to ensure that things won't break. | ||
|
||
# We want to determine how users typically read the data from a Blob. | ||
# The reason is so that we can determine how best to optimize the Blob | ||
# implementation. | ||
blobAsArrayBuffer @1; | ||
blobAsText @2; | ||
blobAsStream @3; | ||
blobGetData @4; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#include "observer.h" | ||
#include "worker-interface.h" | ||
#include <kj/test.h> | ||
|
||
namespace workerd { | ||
namespace { | ||
|
||
KJ_TEST("FeatureObserver") { | ||
FeatureObserver::init(FeatureObserver::createDefault()); | ||
|
||
auto& observer = KJ_ASSERT_NONNULL(FeatureObserver::get()); | ||
|
||
observer.use(FeatureObserver::Feature::TEST); | ||
observer.use(FeatureObserver::Feature::TEST); | ||
observer.use(FeatureObserver::Feature::TEST); | ||
|
||
uint64_t count = 0; | ||
observer.collect([&](FeatureObserver::Feature feature, const uint64_t value) { | ||
KJ_ASSERT(feature == FeatureObserver::Feature::TEST); | ||
count = value; | ||
}); | ||
KJ_ASSERT(count == 3); | ||
} | ||
|
||
} // namespace | ||
} // namespace workerd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#include "observer.h" | ||
#include "worker-interface.h" | ||
#include <kj/common.h> | ||
#include <kj/map.h> | ||
#include <kj/mutex.h> | ||
|
||
namespace workerd { | ||
|
||
namespace { | ||
kj::Maybe<kj::Own<FeatureObserver>> featureObserver; | ||
|
||
class FeatureObserverImpl final: public FeatureObserver { | ||
public: | ||
void use(Feature feature) const override { | ||
auto lock = counts.lockExclusive(); | ||
lock->upsert(feature, 1, [](uint64_t& count, uint64_t value) { | ||
count += value; | ||
}); | ||
} | ||
|
||
void collect(CollectCallback&& callback) const override { | ||
auto lock = counts.lockShared(); | ||
for (auto& entry: *lock) { | ||
callback(entry.key, entry.value); | ||
} | ||
} | ||
|
||
private: | ||
kj::MutexGuarded<kj::HashMap<Feature, uint64_t>> counts; | ||
}; | ||
|
||
} // namespace | ||
|
||
kj::Own<FeatureObserver> FeatureObserver::createDefault() { | ||
return kj::heap<FeatureObserverImpl>(); | ||
} | ||
|
||
void FeatureObserver::init(kj::Own<FeatureObserver> instance) { | ||
KJ_ASSERT(featureObserver == kj::none); | ||
featureObserver = kj::mv(instance); | ||
} | ||
|
||
kj::Maybe<FeatureObserver&> FeatureObserver::get() { | ||
KJ_IF_SOME(impl, featureObserver) { | ||
return *impl; | ||
} | ||
return kj::none; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters