1
#pragma once
2

            
3
#include <string>
4

            
5
#include "envoy/local_info/local_info.h"
6
#include "envoy/tracing/tracer.h"
7

            
8
#include "source/common/tracing/common_values.h"
9
#include "source/common/tracing/null_span_impl.h"
10

            
11
namespace Envoy {
12
namespace Tracing {
13

            
14
/**
15
 * Protocol independent tracer utility functions.
16
 */
17
class TracerUtility {
18
public:
19
  /**
20
   * Get string representation of the operation.
21
   * @param operation name to convert.
22
   * @return string representation of the operation.
23
   */
24
  static const std::string& toString(OperationName operation_name);
25

            
26
  /**
27
   * Request might be traceable if the request ID is traceable or we do sampling tracing.
28
   * Note: there is a global switch which turns off tracing completely on server side.
29
   *
30
   * @return decision if request is traceable or not and Reason why.
31
   **/
32
  static Decision shouldTraceRequest(const StreamInfo::StreamInfo& stream_info);
33

            
34
  /**
35
   * Finalize span and set protocol independent tags to the span.
36
   * @param span the downstream or upstream span.
37
   * @param stream_info stream info.
38
   * @param config tracing configuration.
39
   * @param upstream_span true if the span is an upstream span.
40
   */
41
  static void finalizeSpan(Span& span, const StreamInfo::StreamInfo& stream_info,
42
                           const Config& config, bool upstream_span);
43

            
44
private:
45
  static const std::string IngressOperation;
46
  static const std::string EgressOperation;
47
};
48

            
49
class EgressConfigImpl : public Config {
50
public:
51
  // Tracing::Config
52
1
  Tracing::OperationName operationName() const override { return Tracing::OperationName::Egress; }
53
406
  void modifySpan(Tracing::Span&, bool) const override {}
54
406
  bool verbose() const override { return false; }
55
1
  uint32_t maxPathTagLength() const override { return Tracing::DefaultMaxPathTagLength; }
56
  // This EgressConfigImpl is only used for async client tracing. Return false here is OK.
57
3543
  bool spawnUpstreamSpan() const override { return false; }
58
  // Async clients always propagate trace context by default.
59
3543
  bool noContextPropagation() const override { return false; }
60
};
61

            
62
using EgressConfig = ConstSingleton<EgressConfigImpl>;
63

            
64
class NullTracer : public Tracer {
65
public:
66
  // Tracing::Tracer
67
  SpanPtr startSpan(const Config&, TraceContext&, const StreamInfo::StreamInfo&,
68
1
                    Tracing::Decision) override {
69
1
    return SpanPtr{new NullSpan()};
70
1
  }
71
};
72

            
73
class TracerImpl : public Tracer {
74
public:
75
  TracerImpl(DriverSharedPtr driver, const LocalInfo::LocalInfo& local_info);
76

            
77
  // Tracing::Tracer
78
  SpanPtr startSpan(const Config& config, TraceContext& trace_context,
79
                    const StreamInfo::StreamInfo& stream_info,
80
                    Tracing::Decision tracing_decision) override;
81

            
82
5
  DriverSharedPtr driverForTest() const { return driver_; }
83

            
84
private:
85
  DriverSharedPtr driver_;
86
  const LocalInfo::LocalInfo& local_info_;
87
};
88

            
89
} // namespace Tracing
90
} // namespace Envoy