1
#include "source/extensions/tracers/xray/config.h"
2

            
3
#include <string>
4

            
5
#include "envoy/config/core/v3/address.pb.h"
6
#include "envoy/config/trace/v3/xray.pb.h"
7
#include "envoy/config/trace/v3/xray.pb.validate.h"
8
#include "envoy/registry/registry.h"
9

            
10
#include "source/common/common/utility.h"
11
#include "source/common/config/datasource.h"
12
#include "source/extensions/tracers/xray/xray_tracer_impl.h"
13

            
14
namespace Envoy {
15
namespace Extensions {
16
namespace Tracers {
17
namespace XRay {
18

            
19
9
XRayTracerFactory::XRayTracerFactory() : FactoryBase("envoy.tracers.xray") {}
20

            
21
Tracing::DriverSharedPtr
22
XRayTracerFactory::createTracerDriverTyped(const envoy::config::trace::v3::XRayConfig& proto_config,
23
5
                                           Server::Configuration::TracerFactoryContext& context) {
24
5
  std::string sampling_rules_json;
25
5
  TRY_NEEDS_AUDIT {
26
5
    sampling_rules_json =
27
5
        THROW_OR_RETURN_VALUE(Config::DataSource::read(proto_config.sampling_rule_manifest(), true,
28
5
                                                       context.serverFactoryContext().api()),
29
5
                              std::string);
30
5
  }
31
5
  END_TRY catch (EnvoyException& e) {
32
5
    ENVOY_LOG(error, "Failed to read sampling rules manifest because of {}.", e.what());
33
5
  }
34

            
35
5
  if (proto_config.daemon_endpoint().protocol() != envoy::config::core::v3::SocketAddress::UDP) {
36
1
    throw EnvoyException("X-Ray daemon endpoint must be a UDP socket address");
37
1
  }
38

            
39
4
  if (proto_config.daemon_endpoint().port_specifier_case() !=
40
4
      envoy::config::core::v3::SocketAddress::PortSpecifierCase::kPortValue) {
41
1
    throw EnvoyException("X-Ray daemon port must be specified as number. Not a named port.");
42
1
  }
43

            
44
3
  const std::string endpoint = fmt::format("{}:{}", proto_config.daemon_endpoint().address(),
45
3
                                           proto_config.daemon_endpoint().port_value());
46

            
47
3
  auto aws = absl::flat_hash_map<std::string, Protobuf::Value>{};
48
3
  for (const auto& field : proto_config.segment_fields().aws().fields()) {
49
2
    aws.emplace(field.first, field.second);
50
2
  }
51
3
  const auto& origin = proto_config.segment_fields().origin();
52
3
  XRayConfiguration xconfig{endpoint, proto_config.segment_name(), sampling_rules_json, origin,
53
3
                            std::move(aws)};
54

            
55
3
  return std::make_shared<XRay::Driver>(xconfig, context);
56
4
}
57

            
58
/**
59
 * Static registration for the XRay tracer. @see RegisterFactory.
60
 */
61
REGISTER_FACTORY(XRayTracerFactory, Server::Configuration::TracerFactory);
62

            
63
} // namespace XRay
64
} // namespace Tracers
65
} // namespace Extensions
66
} // namespace Envoy