-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add mixer interface in api manager. #25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,42 @@ | ||
| # 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. | ||
| # | ||
| ################################################################################ | ||
| # | ||
| package(default_visibility = ["//contrib/endpoints/src/api_manager:__subpackages__"]) | ||
|
|
||
| cc_library( | ||
| name = "mixer", | ||
| srcs = [ | ||
| "mixer.cc", | ||
| ], | ||
| hdrs = [ | ||
| "mixer.h", | ||
| ], | ||
| linkopts = select({ | ||
| "//:darwin": [], | ||
| "//conditions:default": [ | ||
| "-lm", | ||
| "-luuid", | ||
| ], | ||
| }), | ||
| deps = [ | ||
| "//external:grpc++", | ||
| "//external:mixer_api_cc_proto", | ||
| "//external:protobuf", | ||
| "//contrib/endpoints/src/api_manager:impl_headers", | ||
| "//contrib/endpoints/src/api_manager/service_control", | ||
| "//contrib/endpoints/src/api_manager/utils", | ||
| ], | ||
| ) |
This file contains hidden or 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,225 @@ | ||
| /* 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/src/api_manager/mixer/mixer.h" | ||
|
|
||
| #include <sstream> | ||
| #include "mixer/api/v1/service.pb.h" | ||
|
|
||
| using ::google::api_manager::utils::Status; | ||
| using ::google::protobuf::util::error::Code; | ||
| using ::google::protobuf::Map; | ||
|
|
||
| namespace google { | ||
| namespace api_manager { | ||
| namespace mixer { | ||
| namespace { | ||
|
|
||
| enum AttributeIndex { | ||
| ATTR_SERVICE_NAME = 0, | ||
| ATTR_PEER_ID, | ||
| ATTR_OPERATION_NAME, | ||
| ATTR_API_KEY, | ||
| ATTR_RESPONSE_CODE, | ||
| ATTR_URL, | ||
| ATTR_LOCATION, | ||
| ATTR_API_NAME, | ||
| ATTR_API_VERSION, | ||
| ATTR_API_METHOD, | ||
| ATTR_REQUEST_SIZE, | ||
| ATTR_RESPONSE_SIZE, | ||
| ATTR_LOG_MESSAGE, | ||
| }; | ||
|
|
||
| struct AttributeDict { | ||
| int index; | ||
| std::string name; | ||
| } kAttributeNames[] = { | ||
| { | ||
| ATTR_SERVICE_NAME, "serviceName", | ||
| }, | ||
| { | ||
| ATTR_PEER_ID, "peerId", | ||
| }, | ||
| { | ||
| ATTR_OPERATION_NAME, "operationName", | ||
| }, | ||
| { | ||
| ATTR_API_KEY, "apiKey", | ||
| }, | ||
| { | ||
| ATTR_RESPONSE_CODE, "responseCode", | ||
| }, | ||
| { | ||
| ATTR_URL, "URL", | ||
| }, | ||
| { | ||
| ATTR_LOCATION, "location", | ||
| }, | ||
| { | ||
| ATTR_API_NAME, "apiName", | ||
| }, | ||
| { | ||
| ATTR_API_VERSION, "apiVersion", | ||
| }, | ||
| { | ||
| ATTR_API_METHOD, "apiMethod", | ||
| }, | ||
| { | ||
| ATTR_REQUEST_SIZE, "requestSize", | ||
| }, | ||
| { | ||
| ATTR_RESPONSE_SIZE, "responesSize", | ||
| }, | ||
| { | ||
| ATTR_LOG_MESSAGE, "logMessage", | ||
| }, | ||
| }; | ||
|
|
||
| void SetAttributeDict(Map<int32_t, std::string>* dict) { | ||
| for (auto attr : kAttributeNames) { | ||
| (*dict)[attr.index] = attr.name; | ||
| } | ||
| } | ||
|
|
||
| void CovertToPb(const service_control::CheckRequestInfo& info, | ||
| ::istio::mixer::v1::Attributes* attr) { | ||
| SetAttributeDict(attr->mutable_dictionary()); | ||
|
|
||
| auto* str_attrs = attr->mutable_string_attributes(); | ||
| (*str_attrs)[ATTR_SERVICE_NAME] = info.service_name; | ||
| (*str_attrs)[ATTR_PEER_ID] = "Proxy"; | ||
| (*str_attrs)[ATTR_OPERATION_NAME] = info.operation_name; | ||
| (*str_attrs)[ATTR_API_KEY] = info.api_key; | ||
| } | ||
|
|
||
| void CovertToPb(const service_control::ReportRequestInfo& info, | ||
| ::istio::mixer::v1::Attributes* attr) { | ||
| SetAttributeDict(attr->mutable_dictionary()); | ||
|
|
||
| auto* str_attrs = attr->mutable_string_attributes(); | ||
| (*str_attrs)[ATTR_SERVICE_NAME] = info.service_name; | ||
| (*str_attrs)[ATTR_PEER_ID] = "Proxy"; | ||
| (*str_attrs)[ATTR_OPERATION_NAME] = info.operation_name; | ||
| (*str_attrs)[ATTR_API_KEY] = info.api_key; | ||
|
|
||
| (*str_attrs)[ATTR_URL] = info.url; | ||
| (*str_attrs)[ATTR_LOCATION] = info.location; | ||
|
|
||
| (*str_attrs)[ATTR_API_NAME] = info.api_name; | ||
| (*str_attrs)[ATTR_API_VERSION] = info.api_version; | ||
| (*str_attrs)[ATTR_API_METHOD] = info.api_method; | ||
|
|
||
| (*str_attrs)[ATTR_LOG_MESSAGE] = info.log_message; | ||
|
|
||
| auto* int_attrs = attr->mutable_int64_attributes(); | ||
| (*int_attrs)[ATTR_RESPONSE_CODE] = info.response_code; | ||
| (*int_attrs)[ATTR_REQUEST_SIZE] = info.request_size; | ||
| (*int_attrs)[ATTR_RESPONSE_SIZE] = info.response_size; | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| Mixer::Mixer(ApiManagerEnvInterface* env) : env_(env), request_index_(0) {} | ||
|
|
||
| Mixer::~Mixer() {} | ||
|
|
||
| Status Mixer::Init() { return Status::OK; } | ||
|
|
||
| Status Mixer::Close() { return Status::OK; } | ||
|
|
||
| Status Mixer::Report(const service_control::ReportRequestInfo& info) { | ||
| std::unique_ptr<GRPCRequest> grpc_request(new GRPCRequest([this]( | ||
| Status status, std::string&& body) { | ||
| if (status.ok()) { | ||
| // Handle 200 response | ||
| ::istio::mixer::v1::ReportResponse response; | ||
| if (!response.ParseFromString(body)) { | ||
| status = | ||
| Status(Code::INVALID_ARGUMENT, std::string("Invalid response")); | ||
| env_->LogError(std::string("Failed parse report response: ") + body); | ||
| } | ||
| env_->LogInfo(std::string("Report response: ") + response.DebugString()); | ||
| } else { | ||
| env_->LogError(std::string("Failed to call Mixer::report, Error: ") + | ||
| status.ToString()); | ||
| } | ||
| })); | ||
|
|
||
| ::istio::mixer::v1::ReportRequest request; | ||
| request.set_request_index(++request_index_); | ||
| CovertToPb(info, request.mutable_attribute_update()); | ||
| env_->LogInfo(std::string("Send Report: ") + request.DebugString()); | ||
|
|
||
| std::string request_body; | ||
| request.SerializeToString(&request_body); | ||
|
|
||
| grpc_request->set_service("istio.mixer.v1.Mixer") | ||
| .set_method("Report") | ||
| .set_body(request_body); | ||
|
|
||
| env_->RunGRPCRequest(std::move(grpc_request)); | ||
| return Status::OK; | ||
| } | ||
|
|
||
| void Mixer::Check( | ||
| const service_control::CheckRequestInfo& info, | ||
| cloud_trace::CloudTraceSpan* parent_span, | ||
| std::function<void(Status, const service_control::CheckResponseInfo&)> | ||
| on_done) { | ||
| std::unique_ptr<GRPCRequest> grpc_request(new GRPCRequest([this, on_done]( | ||
| Status status, std::string&& body) { | ||
| if (status.ok()) { | ||
| // Handle 200 response | ||
| ::istio::mixer::v1::CheckResponse response; | ||
| if (!response.ParseFromString(body)) { | ||
| status = | ||
| Status(Code::INVALID_ARGUMENT, std::string("Invalid response")); | ||
| env_->LogError(std::string("Failed parse check response: ") + body); | ||
| } | ||
| env_->LogInfo(std::string("Check response: ") + response.DebugString()); | ||
| } else { | ||
| env_->LogError(std::string("Failed to call Mixer::check, Error: ") + | ||
| status.ToString()); | ||
| } | ||
| service_control::CheckResponseInfo info; | ||
| on_done(status, info); | ||
| })); | ||
|
|
||
| ::istio::mixer::v1::CheckRequest request; | ||
| request.set_request_index(++request_index_); | ||
| CovertToPb(info, request.mutable_attribute_update()); | ||
| env_->LogInfo(std::string("Send Check: ") + request.DebugString()); | ||
|
|
||
| std::string request_body; | ||
| request.SerializeToString(&request_body); | ||
|
|
||
| grpc_request->set_service("istio.mixer.v1.Mixer") | ||
| .set_method("Check") | ||
| .set_body(request_body); | ||
|
|
||
| env_->RunGRPCRequest(std::move(grpc_request)); | ||
| } | ||
|
|
||
| Status Mixer::GetStatistics(service_control::Statistics* esp_stat) const { | ||
| return Status::OK; | ||
| } | ||
|
|
||
| service_control::Interface* Mixer::Create(ApiManagerEnvInterface* env) { | ||
| return new Mixer(env); | ||
| } | ||
|
|
||
| } // namespace mixer | ||
| } // namespace api_manager | ||
| } // namespace google | ||
This file contains hidden or 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,59 @@ | ||
| /* 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_MIXER_MIXER_H_ | ||
| #define API_MANAGER_MIXER_MIXER_H_ | ||
|
|
||
| #include "contrib/endpoints/include/api_manager/env_interface.h" | ||
| #include "contrib/endpoints/src/api_manager/service_control/interface.h" | ||
|
|
||
| namespace google { | ||
| namespace api_manager { | ||
| namespace mixer { | ||
|
|
||
| // This implementation uses service-control-client-cxx module. | ||
| class Mixer : public service_control::Interface { | ||
| public: | ||
| static service_control::Interface* Create(ApiManagerEnvInterface* env); | ||
|
|
||
| virtual ~Mixer(); | ||
|
|
||
| virtual utils::Status Report(const service_control::ReportRequestInfo& info); | ||
|
|
||
| virtual void Check( | ||
| const service_control::CheckRequestInfo& info, | ||
| cloud_trace::CloudTraceSpan* parent_span, | ||
| std::function<void(utils::Status, | ||
| const service_control::CheckResponseInfo&)> | ||
| on_done); | ||
|
|
||
| virtual utils::Status Init(); | ||
| virtual utils::Status Close(); | ||
|
|
||
| virtual utils::Status GetStatistics(service_control::Statistics* stat) const; | ||
|
|
||
| private: | ||
| // The constructor. | ||
| Mixer(ApiManagerEnvInterface* env); | ||
|
|
||
| // The Api Manager environment interface. | ||
| ApiManagerEnvInterface* env_; | ||
| int64_t request_index_; | ||
| }; | ||
|
|
||
| } // namespace mixer | ||
| } // namespace api_manager | ||
| } // namespace google | ||
|
|
||
| #endif // API_MANAGER_MIXER_MIXER_H_ |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
call set_server("mixer_server").