1
#pragma once
2

            
3
#include "envoy/http/header_map.h"
4
#include "envoy/tracing/trace_context.h"
5

            
6
namespace Envoy {
7
namespace Tracing {
8

            
9
using InlineHandle = Http::CustomInlineHeaderRegistry::Handle<
10
    Http::CustomInlineHeaderRegistry::Type::RequestHeaders>;
11

            
12
/**
13
 * A handler class for trace context. This class could be used to set, get and erase the key/value
14
 * pair in the trace context. This handler is optimized for HTTP to support reference semantics and
15
 * inline header. If HTTP header is used as the trace context and the key is registered in the
16
 * custom inline header registry, then inline handle will be used to improve the performance.
17
 */
18
class TraceContextHandler {
19
public:
20
  /**
21
   * Construct a handler with the given key. Note that the key will be lowercase and copied.
22
   * @param key the key of the handler.
23
   */
24
  TraceContextHandler(absl::string_view key);
25

            
26
  /**
27
   * Get the key of the handler.
28
   * @return absl::string_view the key of the handler. Note that the key is lowercase.
29
   */
30
94
  const Http::LowerCaseString& key() const { return key_; }
31

            
32
  /**
33
   * Erase the key/value pair from the trace context.
34
   * @param trace_context the trace context to erase the key/value pair.
35
   */
36
  void remove(TraceContext& trace_context) const;
37

            
38
  /**
39
   * Get the value from the trace context by the key.
40
   * @param trace_context the trace context to get the value.
41
   * @return absl::optional<absl::string_view> the value of the key. If the key is not found, then
42
   * absl::nullopt will be returned.
43
   */
44
  absl::optional<absl::string_view> get(const TraceContext& trace_context) const;
45

            
46
  using GetAllResult = absl::InlinedVector<absl::string_view, 1>;
47

            
48
  /**
49
   * Get all values from the trace context by the key. If the underlying trace context is HTTP
50
   * header map, then there may be multiple values for the same key and the get() method will
51
   * return the first value only. This method will return all values for the key.
52
   * @param trace_context the trace context to get the values.
53
   */
54
  GetAllResult getAll(const TraceContext& trace_context) const;
55

            
56
  /*
57
   * Set the key/value pair in the trace context.
58
   * @param trace_context the trace context to set the key/value pair.
59
   * @param value the value to set.
60
   */
61
  void set(TraceContext& trace_context, absl::string_view value) const;
62

            
63
  /**
64
   * Set the key/value pair in the trace context. This method should only be used when the handler
65
   * has longer lifetime than current stream.
66
   * @param trace_context the trace context to set the key/value pair.
67
   * @param value the value to set.
68
   */
69
  void setRefKey(TraceContext& trace_context, absl::string_view value) const;
70

            
71
  /**
72
   * Set the key/value pair in the trace context. This method should only be used when both the
73
   * handler and the value have longer lifetime than current stream.
74
   * @param trace_context the trace context to set the key/value pair.
75
   * @param value the value to set.
76
   */
77
  void setRef(TraceContext& trace_context, absl::string_view value) const;
78

            
79
private:
80
  const Http::LowerCaseString key_;
81
  absl::optional<InlineHandle> handle_;
82
};
83

            
84
} // namespace Tracing
85
} // namespace Envoy