Skip to content
This repository was archived by the owner on Aug 19, 2019. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all 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
20 changes: 9 additions & 11 deletions src/api_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,22 @@ void MetadataApiServer::Handler::operator()(const HttpServer::request& request,
<< " body: " << request.body;
}
if (request.method == "GET" && request.destination.find(kPrefix) == 0) {
std::string id = request.destination.substr(kPrefix.size());
std::lock_guard<std::mutex> lock(store_.resource_mu_);
const auto result = store_.resource_map_.find(id);
if (result == store_.resource_map_.end()) {
const std::string id = request.destination.substr(kPrefix.size());
try {
const MonitoredResource& resource = store_.LookupResource(id);
if (config_.VerboseLogging()) {
LOG(INFO) << "Found resource for " << id << ": " << resource;
}
conn->set_status(HttpServer::connection::ok);
conn->write(resource.ToJSON()->ToString());
} catch (const std::out_of_range& e) {
// TODO: This could be considered log spam.
// As we add more resource mappings, these will become less and less
// frequent, and could be promoted to ERROR.
if (config_.VerboseLogging()) {
LOG(WARNING) << "No matching resource for " << id;
}
conn->set_status(HttpServer::connection::not_found);
} else {
const MonitoredResource& resource = result->second;
if (config_.VerboseLogging()) {
LOG(INFO) << "Found resource for " << id << ": " << resource;
}
conn->set_status(HttpServer::connection::ok);
conn->write(resource.ToJSON()->ToString());
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ MetadataStore::Metadata MetadataStore::Metadata::IGNORED() {
MetadataStore::MetadataStore(const MetadataAgentConfiguration& config)
: config_(config) {}

const MonitoredResource& MetadataStore::LookupResource(
const std::string& resource_id) const throw(std::out_of_range) {
std::lock_guard<std::mutex> lock(resource_mu_);
return resource_map_.at(resource_id);
}

void MetadataStore::UpdateResource(const std::vector<std::string>& resource_ids,
const MonitoredResource& resource) {
std::lock_guard<std::mutex> lock(resource_mu_);
Expand Down
7 changes: 6 additions & 1 deletion src/store.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include <map>
#include <mutex>
#include <stdexcept>
#include <string>

#include "json.h"
Expand Down Expand Up @@ -75,6 +76,11 @@ class MetadataStore {

MetadataStore(const MetadataAgentConfiguration& config);

// Looks up the local resource map entry for a given resource id.
// Throws an exception if the resource is not found.
const MonitoredResource& LookupResource(const std::string& resource_id) const
throw(std::out_of_range);

// Updates the local resource map entry for a given resource.
// Each local id in `resource_ids` is effectively an alias for `resource`.
// Adds a resource mapping from each of the `resource_ids` to the `resource`.
Expand All @@ -87,7 +93,6 @@ class MetadataStore {
Metadata&& entry);

private:
friend class MetadataApiServer;
friend class MetadataReporter;

std::map<MonitoredResource, Metadata> GetMetadataMap() const;
Expand Down