• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 //
3 // Copyright 2023 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18 
19 #ifndef GRPC_SRC_CPP_EXT_CSM_METADATA_EXCHANGE_H
20 #define GRPC_SRC_CPP_EXT_CSM_METADATA_EXCHANGE_H
21 
22 #include <grpc/support/port_platform.h>
23 
24 #include <memory>
25 #include <string>
26 #include <utility>
27 #include <vector>
28 
29 #include "absl/strings/string_view.h"
30 #include "google/protobuf/struct.upb.h"
31 #include "opentelemetry/sdk/common/attribute_utils.h"
32 #include "src/core/lib/slice/slice.h"
33 #include "src/core/lib/transport/metadata_batch.h"
34 #include "src/cpp/ext/otel/otel_plugin.h"
35 #include "upb/mem/arena.hpp"
36 
37 namespace grpc {
38 namespace internal {
39 
40 class ServiceMeshLabelsInjector : public LabelsInjector {
41  public:
42   explicit ServiceMeshLabelsInjector(
43       const opentelemetry::sdk::common::AttributeMap& map);
44   // Read the incoming initial metadata to get the set of labels to be added to
45   // metrics.
46   std::unique_ptr<LabelsIterable> GetLabels(
47       grpc_metadata_batch* incoming_initial_metadata) const override;
48 
49   // Modify the outgoing initial metadata with metadata information to be sent
50   // to the peer.
51   void AddLabels(grpc_metadata_batch* outgoing_initial_metadata,
52                  LabelsIterable* labels_from_incoming_metadata) const override;
53 
54   // Add optional labels to the traced calls.
55   bool AddOptionalLabels(
56       bool is_client,
57       absl::Span<const grpc_core::RefCountedStringValue> optional_labels,
58       opentelemetry::nostd::function_ref<
59           bool(opentelemetry::nostd::string_view,
60                opentelemetry::common::AttributeValue)>
61           callback) const override;
62 
63   // Gets the size of the actual optional labels.
GetOptionalLabelsSize(bool is_client,absl::Span<const grpc_core::RefCountedStringValue>)64   size_t GetOptionalLabelsSize(
65       bool is_client,
66       absl::Span<const grpc_core::RefCountedStringValue> /*optional_labels*/)
67       const override {
68     return is_client ? 2 : 0;
69   }
70 
71   const std::vector<std::pair<absl::string_view, std::string>>&
TestOnlyLocalLabels()72   TestOnlyLocalLabels() const {
73     return local_labels_;
74   }
75 
TestOnlySerializedLabels()76   const grpc_core::Slice& TestOnlySerializedLabels() const {
77     return serialized_labels_to_send_;
78   }
79 
80  private:
81   std::vector<std::pair<absl::string_view, std::string>> local_labels_;
82   grpc_core::Slice serialized_labels_to_send_;
83 };
84 
85 // A LabelsIterable class provided by ServiceMeshLabelsInjector. EXPOSED FOR
86 // TESTING PURPOSES ONLY.
87 class MeshLabelsIterable : public LabelsIterable {
88  public:
89   enum class GcpResourceType : std::uint8_t { kGke, kGce, kUnknown };
90 
91   MeshLabelsIterable(
92       const std::vector<std::pair<absl::string_view, std::string>>&
93           local_labels,
94       grpc_core::Slice remote_metadata);
95 
96   absl::optional<std::pair<absl::string_view, absl::string_view>> Next()
97       override;
98 
99   size_t Size() const override;
100 
ResetIteratorPosition()101   void ResetIteratorPosition() override { pos_ = 0; }
102 
103   // Returns true if the peer sent a non-empty base64 encoded
104   // "x-envoy-peer-metadata" metadata.
GotRemoteLabels()105   bool GotRemoteLabels() const { return struct_pb_ != nullptr; }
106 
107  private:
108   upb::Arena arena_;
109   google_protobuf_Struct* struct_pb_ = nullptr;
110   const std::vector<std::pair<absl::string_view, std::string>>& local_labels_;
111   GcpResourceType remote_type_ = GcpResourceType::kUnknown;
112   uint32_t pos_ = 0;
113 };
114 
115 }  // namespace internal
116 }  // namespace grpc
117 
118 #endif  // GRPC_SRC_CPP_EXT_CSM_METADATA_EXCHANGE_H
119