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
17 changes: 8 additions & 9 deletions src/api_server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ class MetadataReporter {
~MetadataReporter();

private:
using seconds = std::chrono::duration<double, std::chrono::seconds::period>;
// Metadata reporter.
void ReportMetadata();

Expand All @@ -83,7 +82,7 @@ class MetadataReporter {
Environment environment_;
OAuth2 auth_;
// The reporting period in seconds.
seconds period_;
time::seconds period_;
std::thread reporter_thread_;
};

Expand Down Expand Up @@ -166,7 +165,7 @@ void MetadataReporter::ReportMetadata() {
LOG(INFO) << "Metadata reporter started";
// Wait for the first collection to complete.
// TODO: Come up with a more robust synchronization mechanism.
std::this_thread::sleep_for(std::chrono::seconds(3));
std::this_thread::sleep_for(time::seconds(3));
// TODO: Do we need to be able to stop this?
while (true) {
if (agent_->config_.VerboseLogging()) {
Expand Down Expand Up @@ -271,8 +270,8 @@ void MetadataReporter::SendMetadata(
{"rawContentVersion", json::string(metadata.version)},
{"rawContent", std::move(metadata.metadata)},
{"state", json::string(metadata.is_deleted ? "DELETED" : "ACTIVE")},
{"createTime", json::string(rfc3339::ToString(metadata.created_at))},
{"collectTime", json::string(rfc3339::ToString(metadata.collected_at))},
{"createTime", json::string(time::rfc3339::ToString(metadata.created_at))},
{"collectTime", json::string(time::rfc3339::ToString(metadata.collected_at))},
});
// TODO: This is probably all kinds of inefficient...
const int size = metadata_entry->ToString().size();
Expand Down Expand Up @@ -322,8 +321,8 @@ void MetadataAgent::UpdateMetadata(const MonitoredResource& resource,
LOG(INFO) << "Updating metadata map " << resource << "->{"
<< "version: " << entry.version << ", "
<< "is_deleted: " << entry.is_deleted << ", "
<< "created_at: " << rfc3339::ToString(entry.created_at) << ", "
<< "collected_at: " << rfc3339::ToString(entry.collected_at)
<< "created_at: " << time::rfc3339::ToString(entry.created_at) << ", "
<< "collected_at: " << time::rfc3339::ToString(entry.collected_at)
<< ", "
<< "metadata: " << *entry.metadata << ", "
<< "ignore: " << entry.ignore
Expand Down Expand Up @@ -359,9 +358,9 @@ void MetadataAgent::PurgeDeletedEntries() {
LOG(INFO) << "Purging metadata entry " << resource << "->{"
<< "version: " << entry.version << ", "
<< "is_deleted: " << entry.is_deleted << ", "
<< "created_at: " << rfc3339::ToString(entry.created_at)
<< "created_at: " << time::rfc3339::ToString(entry.created_at)
<< ", "
<< "collected_at: " << rfc3339::ToString(entry.collected_at)
<< "collected_at: " << time::rfc3339::ToString(entry.collected_at)
<< ", "
<< "metadata: " << *entry.metadata << ", "
<< "ignore: " << entry.ignore
Expand Down
3 changes: 2 additions & 1 deletion src/api_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "configuration.h"
#include "json.h"
#include "resource.h"
#include "time.h"

namespace google {

Expand All @@ -37,7 +38,7 @@ class MetadataApiServer;
class MetadataReporter;

// A timestamp type.
using Timestamp = std::chrono::time_point<std::chrono::system_clock>;
using Timestamp = time_point;

// Stores the metadata mapping and runs the metadata tasks.
class MetadataAgent {
Expand Down
2 changes: 1 addition & 1 deletion src/docker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ MetadataUpdater::ResourceMetadata DockerReader::GetContainerMetadata(

const std::string created_str =
container_desc->Get<json::String>("Created");
Timestamp created_at = rfc3339::FromString(created_str);
Timestamp created_at = time::rfc3339::FromString(created_str);

const json::Object* state = container_desc->Get<json::Object>("State");
bool is_deleted = state->Get<json::Boolean>("Dead");
Expand Down
8 changes: 4 additions & 4 deletions src/kubernetes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ MetadataUpdater::ResourceMetadata KubernetesReader::GetNodeMetadata(
const std::string node_name = metadata->Get<json::String>("name");
const std::string created_str =
metadata->Get<json::String>("creationTimestamp");
Timestamp created_at = rfc3339::FromString(created_str);
Timestamp created_at = time::rfc3339::FromString(created_str);

const MonitoredResource k8s_node("k8s_node", {
{"cluster_name", cluster_name},
Expand Down Expand Up @@ -203,11 +203,11 @@ MetadataUpdater::ResourceMetadata KubernetesReader::GetPodMetadata(
const std::string pod_id = metadata->Get<json::String>("uid");
const std::string created_str =
metadata->Get<json::String>("creationTimestamp");
Timestamp created_at = rfc3339::FromString(created_str);
Timestamp created_at = time::rfc3339::FromString(created_str);

const json::Object* status = pod->Get<json::Object>("status");
const std::string started_str = status->Get<json::String>("startTime");
Timestamp started_at = rfc3339::FromString(started_str);
Timestamp started_at = time::rfc3339::FromString(started_str);

const MonitoredResource k8s_pod("k8s_pod", {
{"cluster_name", cluster_name},
Expand Down Expand Up @@ -261,7 +261,7 @@ MetadataUpdater::ResourceMetadata KubernetesReader::GetContainerMetadata(
const std::string pod_id = metadata->Get<json::String>("uid");
const std::string created_str =
metadata->Get<json::String>("creationTimestamp");
Timestamp created_at = rfc3339::FromString(created_str);
Timestamp created_at = time::rfc3339::FromString(created_str);
const json::Object* labels;
if (!metadata->Has("labels")) {
labels = nullptr;
Expand Down
2 changes: 1 addition & 1 deletion src/logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Logger::Logger(const char* file, int line, Severity severity, LogStream* stream)
{
const std::time_t now_c =
std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
std::tm local_time = safe_localtime(&now_c);
std::tm local_time = time::safe_localtime(&now_c);
// GCC 4.x does not implement std::put_time. Sigh.
char time_val[20];
std::strftime(time_val, sizeof(time_val), "%m%d %H:%M:%S ", &local_time);
Expand Down
12 changes: 3 additions & 9 deletions src/oauth2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "http_common.h"
#include "json.h"
#include "logging.h"
#include "time.h"

namespace http = boost::network::http;

Expand Down Expand Up @@ -138,13 +139,6 @@ std::string Sign(const std::string& data, const PKey& pkey) {
return std::string(reinterpret_cast<char*>(result.get()), actual_result_size);
}

// TODO: Move this to the time library?
double SecondsSinceEpoch(
const std::chrono::time_point<std::chrono::system_clock>& t) {
return std::chrono::duration_cast<std::chrono::seconds>(
t.time_since_epoch()).count();
}

}

json::value OAuth2::ComputeTokenFromCredentials() const {
Expand Down Expand Up @@ -209,8 +203,8 @@ json::value OAuth2::ComputeTokenFromCredentials() const {
{"iss", json::string(service_account_email)},
{"scope", json::string("https://www.googleapis.com/auth/monitoring")},
{"aud", json::string("https://www.googleapis.com/oauth2/v3/token")},
{"iat", json::number(SecondsSinceEpoch(now))},
{"exp", json::number(SecondsSinceEpoch(exp))},
{"iat", json::number(time::SecondsSinceEpoch(now))},
{"exp", json::number(time::SecondsSinceEpoch(exp))},
});
if (environment_.config().VerboseLogging()) {
LOG(INFO) << "claim_set = " << claim_set_object->ToString();
Expand Down
4 changes: 2 additions & 2 deletions src/oauth2.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
#define OAUTH2_H_

#include <boost/network/message/directives/header.hpp>
#include <chrono>
#include <memory>
#include <string>

#include "environment.h"
#include "json.h"
#include "time.h"

namespace google {

Expand All @@ -41,7 +41,7 @@ class OAuth2 {

const Environment& environment_;
std::string auth_header_value_;
std::chrono::time_point<std::chrono::system_clock> token_expiration_;
time_point token_expiration_;
};

template<class Request>
Expand Down
29 changes: 16 additions & 13 deletions src/time.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

namespace google {

namespace time {

namespace rfc3339 {

namespace {
Expand All @@ -44,48 +46,49 @@ const int kUtcOffset = UTCOffset();

}

std::string ToString(const std::chrono::system_clock::time_point& t) {
std::string ToString(const time_point& t) {
std::stringstream out;
const std::time_t time = std::chrono::system_clock::to_time_t(t);
const std::chrono::system_clock::time_point t_sec =
std::chrono::time_point_cast<std::chrono::system_clock::duration>(t);
const std::time_t time = std::chrono::system_clock::to_time_t(t_sec);
std::tm utc_time = safe_gmtime(&time);
// GCC 4.x does not implement std::put_time. Sigh.
char dt[64];
std::strftime(dt, sizeof(dt), "%Y-%m-%dT%H:%M:%S", &utc_time);
std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds>
t_ns = std::chrono::time_point_cast<std::chrono::nanoseconds>(t);
time_point t_ns = std::chrono::time_point_cast<std::chrono::nanoseconds>(t);
std::chrono::time_point<std::chrono::system_clock, std::chrono::seconds>
t_s = std::chrono::time_point_cast<std::chrono::seconds>(t_ns);
const std::chrono::nanoseconds ns = t_ns - t_s;
out << dt << "." << std::setw(9) << std::setfill('0') << ns.count() << "Z";
return out.str();
}

std::chrono::system_clock::time_point FromString(const std::string& s) {
time_point FromString(const std::string& s) {
std::tm tm;
char* const end = strptime(s.c_str(), "%Y-%m-%dT%H:%M:%S", &tm);
if (end == nullptr || !std::isdigit(*(end - 2))) {
// TODO: Invalid time format.
return std::chrono::system_clock::time_point();
return time_point();
}
char* zone;
if (*end == '.') {
(void)std::strtol(end + 1, &zone, 10);
if (zone <= end + 1) {
// TODO: Missing nanoseconds.
return std::chrono::system_clock::time_point();
return time_point();
}
} else {
zone = end;
}
if (*zone != 'Z' || *(zone+1) != '\0') {
// TODO: Invalid timezone.
return std::chrono::system_clock::time_point();
return time_point();
}
char* d_end;
double seconds = std::strtod(end - 2, &d_end);
if (d_end != zone) {
// TODO: Internal error.
return std::chrono::system_clock::time_point();
return time_point();
}
static_assert(sizeof(long) == 8, "long is too small");
// Truncate to 9 digits by rounding to 10 and discarding the last one.
Expand All @@ -94,10 +97,8 @@ std::chrono::system_clock::time_point FromString(const std::string& s) {
tm.tm_isdst = 0;
const std::time_t local_time = std::mktime(&tm);
const std::time_t utc_time = local_time + kUtcOffset;
std::chrono::system_clock::time_point sec =
std::chrono::system_clock::from_time_t(utc_time);
return std::chrono::time_point_cast<std::chrono::system_clock::duration>(
sec + std::chrono::nanoseconds(ns));
time_point sec = std::chrono::system_clock::from_time_t(utc_time);
return sec + std::chrono::nanoseconds(ns);
}

}
Expand All @@ -120,3 +121,5 @@ std::tm safe_gmtime(const std::time_t* t) {
}

}

}
18 changes: 16 additions & 2 deletions src/time.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,23 @@

namespace google {

using time_point = std::chrono::time_point<std::chrono::system_clock,
std::chrono::nanoseconds>;

namespace time {

using seconds = std::chrono::duration<double, std::chrono::seconds::period>;

inline double SecondsSinceEpoch(const time_point& t) {
return std::chrono::duration_cast<std::chrono::seconds>(
t.time_since_epoch()).count();
}

namespace rfc3339 {

// Time conversions.
std::string ToString(const std::chrono::system_clock::time_point& t);
std::chrono::system_clock::time_point FromString(const std::string& s);
std::string ToString(const time_point& t);
time_point FromString(const std::string& s);

}

Expand All @@ -36,4 +48,6 @@ std::tm safe_gmtime(const std::time_t* t);

}

}

#endif // TIME_H_
6 changes: 3 additions & 3 deletions src/updater.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ PollingMetadataUpdater::~PollingMetadataUpdater() {
}

bool PollingMetadataUpdater::ValidateConfiguration() const {
return period_ >= seconds::zero();
return period_ >= time::seconds::zero();
}

void PollingMetadataUpdater::StartUpdater() {
timer_.lock();
if (config().VerboseLogging()) {
LOG(INFO) << "Timer locked";
}
if (period_ > seconds::zero()) {
if (period_ > time::seconds::zero()) {
reporter_thread_ =
std::thread(&PollingMetadataUpdater::PollForMetadata, this);
}
Expand Down Expand Up @@ -100,7 +100,7 @@ void PollingMetadataUpdater::PollForMetadata() {
}
if (config().VerboseLogging()) {
LOG(INFO) << " Timer unlock timed out after "
<< std::chrono::duration_cast<seconds>(now - start).count()
<< std::chrono::duration_cast<time::seconds>(now - start).count()
<< "s (good)";
}
start = now;
Expand Down
5 changes: 2 additions & 3 deletions src/updater.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

//#include "config.h"

#include <chrono>
#include <functional>
#include <mutex>
#include <string>
Expand All @@ -27,6 +26,7 @@

#include "api_server.h"
#include "resource.h"
#include "time.h"

namespace google {

Expand Down Expand Up @@ -107,12 +107,11 @@ class PollingMetadataUpdater : public MetadataUpdater {
void StopUpdater();

private:
using seconds = std::chrono::duration<double, std::chrono::seconds::period>;
// Metadata poller.
void PollForMetadata();

// The polling period in seconds.
seconds period_;
time::seconds period_;

// The function to actually query for metadata.
std::function<std::vector<ResourceMetadata>()> query_metadata_;
Expand Down
Loading