Adds new API to retrieve statsd metadata.

This API can be used by clients to gather stats about statsd, eg.
memory usage, number of metrics/matchers, etc. This data can be used
to debug if devices are not providing expected metrics. The metadata
will be for all configurations, but will not contain the actual
collected metrics since those might have privacy implications.

Test: Tests that statsd and Android still build.
Bug: 69522276
Change-Id: I8e0fedc142f5deed7be6e6309f9444e67d8369ce
This commit is contained in:
David Chen
2017-11-22 10:56:48 -08:00
parent d796c9062e
commit 2e8f380737
5 changed files with 51 additions and 3 deletions

View File

@@ -4332,6 +4332,7 @@ package android.util {
public final class StatsManager {
method public boolean addConfiguration(java.lang.String, byte[], java.lang.String, java.lang.String);
method public byte[] getData(java.lang.String);
method public byte[] getMetadata();
method public boolean removeConfiguration(java.lang.String);
}

View File

@@ -715,6 +715,18 @@ Status StatsService::getData(const String16& key, vector<uint8_t>* output) {
}
}
Status StatsService::getMetadata(vector<uint8_t>* output) {
IPCThreadState* ipc = IPCThreadState::self();
VLOG("StatsService::getMetadata with Pid %i, Uid %i", ipc->getCallingPid(),
ipc->getCallingUid());
if (checkCallingPermission(String16(kPermissionDump))) {
StatsdStats::getInstance().dumpStats(output, false); // Don't reset the counters.
return Status::ok();
} else {
return Status::fromExceptionCode(binder::Status::EX_SECURITY);
}
}
Status StatsService::addConfiguration(const String16& key,
const vector <uint8_t>& config,
const String16& package, const String16& cls,

View File

@@ -75,6 +75,11 @@ public:
*/
virtual Status getData(const String16& key, vector<uint8_t>* output) override;
/**
* Binder call for clients to get metadata across all configs in statsd.
*/
virtual Status getMetadata(vector<uint8_t>* output) override;
/**
* Binder call to let clients send a configuration and indicate they're interested when they
* should requestData for this configuration.

View File

@@ -69,10 +69,15 @@ interface IStatsManager {
/**
* Fetches data for the specified configuration key. Returns a byte array representing proto
* wire-encoded of ConfigMetricsReport.
* wire-encoded of ConfigMetricsReportList.
*/
byte[] getData(in String key);
/**
* Fetches metadata across statsd. Returns byte array representing wire-encoded proto.
*/
byte[] getMetadata();
/**
* Sets a configuration with the specified config key and subscribes to updates for this
* configuration key. Broadcasts will be sent if this configuration needs to be collected.

View File

@@ -93,10 +93,11 @@ public final class StatsManager {
}
/**
* Clients can request data with a binder call.
* Clients can request data with a binder call. This getter is destructive and also clears
* the retrieved metrics from statsd memory.
*
* @param configKey Configuration key to retrieve data from.
* @return Serialized ConfigMetricsReport proto. Returns null on failure.
* @return Serialized ConfigMetricsReportList proto. Returns null on failure.
*/
@RequiresPermission(Manifest.permission.DUMP)
public byte[] getData(String configKey) {
@@ -115,6 +116,30 @@ public final class StatsManager {
}
}
/**
* Clients can request metadata for statsd. Will contain stats across all configurations but not
* the actual metrics themselves (metrics must be collected via {@link #getData(String)}.
* This getter is not destructive and will not reset any metrics/counters.
*
* @return Serialized StatsdStatsReport proto. Returns null on failure.
*/
@RequiresPermission(Manifest.permission.DUMP)
public byte[] getMetadata() {
synchronized (this) {
try {
IStatsManager service = getIStatsManagerLocked();
if (service == null) {
Slog.d(TAG, "Failed to find statsd when getting metadata");
return null;
}
return service.getMetadata();
} catch (RemoteException e) {
Slog.d(TAG, "Failed to connecto statsd when getting metadata");
return null;
}
}
}
private class StatsdDeathRecipient implements IBinder.DeathRecipient {
@Override
public void binderDied() {