|
| 1 | +#include "pending_trace.h" |
| 2 | + |
| 3 | +#include <cassert> |
| 4 | + |
| 5 | +#include "span.h" |
| 6 | + |
| 7 | +namespace datadog { |
| 8 | +namespace opentracing { |
| 9 | +namespace { |
| 10 | + |
| 11 | +const std::string sampling_priority_metric = "_sampling_priority_v1"; |
| 12 | +const std::string datadog_origin_tag = "_dd.origin"; |
| 13 | +const std::string datadog_hostname_tag = "_dd.hostname"; |
| 14 | +const std::string datadog_upstream_services_tag = "_dd.p.upstream_services"; |
| 15 | +const std::string datadog_propagation_error_tag = "_dd.propagation_error"; |
| 16 | +const std::string event_sample_rate_metric = "_dd1.sr.eausr"; |
| 17 | +const std::string rules_sampler_applied_rate = "_dd.rule_psr"; |
| 18 | +const std::string rules_sampler_limiter_rate = "_dd.limit_psr"; |
| 19 | +const std::string priority_sampler_applied_rate = "_dd.agent_psr"; |
| 20 | + |
| 21 | +// Return whether the specified `span` is without a parent among the specified |
| 22 | +// `all_spans_in_trace`. |
| 23 | +bool is_root(const SpanData& span, const std::unordered_set<uint64_t>& all_spans_in_trace) { |
| 24 | + return |
| 25 | + // root span |
| 26 | + span.parent_id == 0 || |
| 27 | + // local root span of a distributed trace |
| 28 | + all_spans_in_trace.find(span.parent_id) == all_spans_in_trace.end(); |
| 29 | +} |
| 30 | + |
| 31 | +// Alter the specified `span` to prepare it for encoding with the specified |
| 32 | +// `trace`. |
| 33 | +void finish_span(const PendingTrace& trace, SpanData& span) { |
| 34 | + // Propagate the trace origin in every span, if present. This allows, for |
| 35 | + // example, sampling to vary with the trace's stated origin. |
| 36 | + if (!trace.origin.empty()) { |
| 37 | + span.meta[datadog_origin_tag] = trace.origin; |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +// Alter the specified root (i.e. having no parent in the local trace) `span` |
| 42 | +// to prepare it for encoding with the specified `trace`. |
| 43 | +void finish_root_span(PendingTrace& trace, SpanData& span) { |
| 44 | + // Check for sampling. |
| 45 | + if (trace.sampling_priority != nullptr) { |
| 46 | + span.metrics[sampling_priority_metric] = static_cast<int>(*trace.sampling_priority); |
| 47 | + // The span's datadog origin tag is set in `finish_span`, below. |
| 48 | + } |
| 49 | + if (!trace.hostname.empty()) { |
| 50 | + span.meta[datadog_hostname_tag] = trace.hostname; |
| 51 | + } |
| 52 | + if (!std::isnan(trace.analytics_rate) && |
| 53 | + span.metrics.find(event_sample_rate_metric) == span.metrics.end()) { |
| 54 | + span.metrics[event_sample_rate_metric] = trace.analytics_rate; |
| 55 | + } |
| 56 | + if (!std::isnan(trace.sample_result.rule_rate)) { |
| 57 | + span.metrics[rules_sampler_applied_rate] = trace.sample_result.rule_rate; |
| 58 | + } |
| 59 | + if (!std::isnan(trace.sample_result.limiter_rate)) { |
| 60 | + span.metrics[rules_sampler_limiter_rate] = trace.sample_result.limiter_rate; |
| 61 | + } |
| 62 | + if (!std::isnan(trace.sample_result.priority_rate)) { |
| 63 | + span.metrics[priority_sampler_applied_rate] = trace.sample_result.priority_rate; |
| 64 | + } |
| 65 | + trace.applySamplingDecisionToUpstreamServices(); |
| 66 | + span.meta.insert(trace.trace_tags.begin(), trace.trace_tags.end()); |
| 67 | + if (!trace.propagation_error.empty()) { |
| 68 | + span.meta[datadog_propagation_error_tag] = trace.propagation_error; |
| 69 | + } |
| 70 | + // Forward to the finisher that applies to all spans (not just root spans). |
| 71 | + finish_span(trace, span); |
| 72 | +} |
| 73 | + |
| 74 | +} // namespace |
| 75 | + |
| 76 | +PendingTrace::PendingTrace(std::shared_ptr<const Logger> logger, uint64_t trace_id) |
| 77 | + : logger(logger), |
| 78 | + trace_id(trace_id), |
| 79 | + finished_spans(TraceData{new std::vector<std::unique_ptr<SpanData>>()}), |
| 80 | + all_spans() {} |
| 81 | + |
| 82 | +PendingTrace::PendingTrace(std::shared_ptr<const Logger> logger, uint64_t trace_id, |
| 83 | + std::unique_ptr<SamplingPriority> sampling_priority) |
| 84 | + : logger(logger), |
| 85 | + trace_id(trace_id), |
| 86 | + finished_spans(TraceData{new std::vector<std::unique_ptr<SpanData>>()}), |
| 87 | + all_spans(), |
| 88 | + sampling_priority(std::move(sampling_priority)) {} |
| 89 | + |
| 90 | +void PendingTrace::finish() { |
| 91 | + // Apply changes to spans, in particular treating the root / local-root |
| 92 | + // span as special. |
| 93 | + for (const auto& span : *finished_spans) { |
| 94 | + if (is_root(*span, all_spans)) { |
| 95 | + finish_root_span(*this, *span); |
| 96 | + } else { |
| 97 | + finish_span(*this, *span); |
| 98 | + } |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +void PendingTrace::applySamplingDecisionToUpstreamServices() { |
| 103 | + if (applied_sampling_decision_to_upstream_services || sampling_decision_extracted || |
| 104 | + sampling_priority == nullptr) { |
| 105 | + // We did not make the sampling decision, or we've already done this. |
| 106 | + return; |
| 107 | + } |
| 108 | + |
| 109 | + // In unit tests, we sometimes don't have a service name. In those cases, |
| 110 | + // omit our `UpstreamService` entry (those tests are not looking for the |
| 111 | + // corresponding tag). |
| 112 | + if (service.empty()) { |
| 113 | + return; |
| 114 | + } |
| 115 | + |
| 116 | + // If we have a sampling priority and `sampling_decision_extracted == false`, |
| 117 | + // then the sampling priority was determined by this tracer, and so we will |
| 118 | + // have set a corresponding sampling mechanism. |
| 119 | + assert(sample_result.sampling_mechanism != nullptr); |
| 120 | + |
| 121 | + UpstreamService this_service; |
| 122 | + this_service.service_name = service; |
| 123 | + this_service.sampling_priority = *sampling_priority; |
| 124 | + this_service.sampling_mechanism = int(sample_result.sampling_mechanism.get<SamplingMechanism>()); |
| 125 | + this_service.sampling_rate = sample_result.applied_rate; |
| 126 | + |
| 127 | + appendUpstreamService(trace_tags[upstream_services_tag], this_service); |
| 128 | + applied_sampling_decision_to_upstream_services = true; |
| 129 | +} |
| 130 | + |
| 131 | +} // namespace opentracing |
| 132 | +} // namespace datadog |
0 commit comments