diff --git a/contrib/endpoints/include/BUILD b/contrib/endpoints/include/BUILD index 673cbbedfdfc..c0b31a061b27 100644 --- a/contrib/endpoints/include/BUILD +++ b/contrib/endpoints/include/BUILD @@ -14,7 +14,6 @@ # ################################################################################ # -exports_files(["version"]) cc_library( name = "api_manager", @@ -53,20 +52,6 @@ filegroup( "api_manager/response.h", "api_manager/service_control.h", "api_manager/utils/status.h", - "api_manager/version.h", - ], -) - -genrule( - name = "api_manager_version_header", - srcs = [ - ":version", - ], - outs = [ - "api_manager/version.h", - ], - cmd = "$(location //contrib/endpoints/tools:create_version) $(location :version) > $@", - tools = [ - "//contrib/endpoints/tools:create_version", + "api_manager/utils/version.h", ], ) diff --git a/contrib/endpoints/include/api_manager/utils/version.h b/contrib/endpoints/include/api_manager/utils/version.h new file mode 100644 index 000000000000..98e340b6f2f8 --- /dev/null +++ b/contrib/endpoints/include/api_manager/utils/version.h @@ -0,0 +1,50 @@ +/* Copyright 2016 Google Inc. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef API_MANAGER_UTILS_VERSION_H_ +#define API_MANAGER_UTILS_VERSION_H_ + +#include + +namespace google { +namespace api_manager { +namespace utils { + +// The version is used by Endpoint cloud trace and service control +// when sending service agent. +class Version final { + public: + // Fetch singleton. + static Version& instance(); + + // Get the version. + const std::string& get() const { return version_; } + + // Set the version. Default is empty. + void set(const std::string& v) { version_ = v; } + + private: + // The version. + std::string version_; + + // public construct not allowed + Version() {} +}; + +} // namespace utils +} // namespace api_manager +} // namespace google + +#endif // API_MANAGER_UTILS_VERSION_H_ diff --git a/contrib/endpoints/include/version b/contrib/endpoints/include/version deleted file mode 100644 index 9084fa2f716a..000000000000 --- a/contrib/endpoints/include/version +++ /dev/null @@ -1 +0,0 @@ -1.1.0 diff --git a/contrib/endpoints/src/api_manager/cloud_trace/cloud_trace.cc b/contrib/endpoints/src/api_manager/cloud_trace/cloud_trace.cc index 0217d2cc8b7f..af648f1b56ae 100644 --- a/contrib/endpoints/src/api_manager/cloud_trace/cloud_trace.cc +++ b/contrib/endpoints/src/api_manager/cloud_trace/cloud_trace.cc @@ -23,7 +23,7 @@ #include #include #include "contrib/endpoints/include/api_manager/utils/status.h" -#include "contrib/endpoints/include/api_manager/version.h" +#include "contrib/endpoints/include/api_manager/utils/version.h" #include "contrib/endpoints/src/api_manager/utils/marshalling.h" #include "google/protobuf/timestamp.pb.h" @@ -43,7 +43,7 @@ const char kCloudTraceService[] = "/google.devtools.cloudtrace.v1.TraceService"; // Cloud Trace agent label key const char kCloudTraceAgentKey[] = "trace.cloud.google.com/agent"; // Cloud Trace agent label value -const char kServiceAgent[] = "esp/" API_MANAGER_VERSION_STRING; +const char kServiceAgentPrefix[] = "esp/"; // Default trace options const char kDefaultTraceOptions[] = "o=1"; @@ -332,7 +332,9 @@ void GetNewTrace(std::string trace_id_str, const std::string &root_span_name, root_span->set_span_id(RandomUInt64()); root_span->set_name(root_span_name); // Agent label is defined as "/". - root_span->mutable_labels()->insert({kCloudTraceAgentKey, kServiceAgent}); + root_span->mutable_labels()->insert( + {kCloudTraceAgentKey, + kServiceAgentPrefix + utils::Version::instance().get()}); GetNow(root_span->mutable_start_time()); } diff --git a/contrib/endpoints/src/api_manager/service_control/proto.cc b/contrib/endpoints/src/api_manager/service_control/proto.cc index cf32415855c3..17b921db076f 100644 --- a/contrib/endpoints/src/api_manager/service_control/proto.cc +++ b/contrib/endpoints/src/api_manager/service_control/proto.cc @@ -22,7 +22,7 @@ #include #include "contrib/endpoints/include/api_manager/service_control.h" -#include "contrib/endpoints/include/api_manager/version.h" +#include "contrib/endpoints/include/api_manager/utils/version.h" #include "contrib/endpoints/src/api_manager/auth/lib/auth_token.h" #include "contrib/endpoints/src/api_manager/auth/lib/base64.h" #include "google/api/metric.pb.h" @@ -427,7 +427,7 @@ const char kServiceControlPlatform[] = "servicecontrol.googleapis.com/platform"; const char kUserAgent[] = "ESP"; // Service agent label value -const char kServiceAgent[] = "ESP/" API_MANAGER_VERSION_STRING; +const char kServiceAgentPrefix[] = "ESP/"; // /credential_id Status set_credential_id(const SupportedLabel& l, const ReportRequestInfo& info, @@ -569,7 +569,7 @@ Status set_platform(const SupportedLabel& l, const ReportRequestInfo& info, // servicecontrol.googleapis.com/service_agent Status set_service_agent(const SupportedLabel& l, const ReportRequestInfo& info, Map* labels) { - (*labels)[l.name] = kServiceAgent; + (*labels)[l.name] = kServiceAgentPrefix + utils::Version::instance().get(); return Status::OK; } @@ -926,7 +926,8 @@ Status Proto::FillCheckRequest(const CheckRequestInfo& info, (*labels)[kServiceControlReferer] = info.referer; } (*labels)[kServiceControlUserAgent] = kUserAgent; - (*labels)[kServiceControlServiceAgent] = kServiceAgent; + (*labels)[kServiceControlServiceAgent] = + kServiceAgentPrefix + utils::Version::instance().get(); return Status::OK; } diff --git a/contrib/endpoints/src/api_manager/service_control/proto_test.cc b/contrib/endpoints/src/api_manager/service_control/proto_test.cc index 7b0bb4f62713..609cb54abe7a 100644 --- a/contrib/endpoints/src/api_manager/service_control/proto_test.cc +++ b/contrib/endpoints/src/api_manager/service_control/proto_test.cc @@ -21,7 +21,7 @@ #include #include -#include "contrib/endpoints/include/api_manager/version.h" +#include "contrib/endpoints/include/api_manager/utils/version.h" #include "google/protobuf/struct.pb.h" #include "google/protobuf/text_format.h" @@ -55,7 +55,7 @@ std::string ReadTestBaseline(const char* input_file_name) { // Replace instances of {{service_agent_version}} with the expected service // agent version. std::string placeholder = "{{service_agent_version}}"; - std::string value = API_MANAGER_VERSION_STRING; + std::string value = utils::Version::instance().get(); size_t current = 0; while ((current = contents.find(placeholder, current)) != std::string::npos) { contents.replace(current, placeholder.length(), value); diff --git a/contrib/endpoints/src/api_manager/utils/BUILD b/contrib/endpoints/src/api_manager/utils/BUILD index 70c94b9d2be7..76d25b871dbf 100644 --- a/contrib/endpoints/src/api_manager/utils/BUILD +++ b/contrib/endpoints/src/api_manager/utils/BUILD @@ -24,6 +24,7 @@ cc_library( "marshalling.cc", "status.cc", "url_util.cc", + "version.cc", ], hdrs = [ "marshalling.h", diff --git a/contrib/endpoints/src/api_manager/utils/version.cc b/contrib/endpoints/src/api_manager/utils/version.cc new file mode 100644 index 000000000000..2fa8f3d4ba13 --- /dev/null +++ b/contrib/endpoints/src/api_manager/utils/version.cc @@ -0,0 +1,30 @@ +// Copyright 2016 Google Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// +/////////////////////////////////////////////////////////////////////////////// + +#include "contrib/endpoints/include/api_manager/utils/version.h" + +namespace google { +namespace api_manager { +namespace utils { + +Version& Version::instance() { + static Version v; + return v; +} + +} // namespace utils +} // namespace api_manager +} // namespace google diff --git a/contrib/endpoints/tools/BUILD b/contrib/endpoints/tools/BUILD deleted file mode 100644 index 31fcea7f862b..000000000000 --- a/contrib/endpoints/tools/BUILD +++ /dev/null @@ -1,23 +0,0 @@ -# Copyright 2016 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -################################################################################ -# -sh_binary( - name = "create_version", - srcs = [ - "create_version.sh", - ], - visibility = ["//visibility:public"], -) diff --git a/contrib/endpoints/tools/create_version.sh b/contrib/endpoints/tools/create_version.sh deleted file mode 100755 index 6b46de300e85..000000000000 --- a/contrib/endpoints/tools/create_version.sh +++ /dev/null @@ -1,50 +0,0 @@ -# Copyright 2016 Google Inc. All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# -################################################################################ -# - -read VERSION < "${1:-/dev/stdin}" -MAJOR="${VERSION%%\.*}" -REST="${VERSION#*\.}" -MINOR="${REST%%\.*}" -REVISION="${VERSION##*\.}" - -cat <