Example #1
0
        private IDistributedTracePayload TryGetOutboundDistributedTraceApiModelInternal(IInternalTransaction transaction, ISegment segment, DateTime timestamp)
        {
            var accountId = _configurationService.Configuration.AccountId;
            var appId     = _configurationService.Configuration.PrimaryApplicationId;

            if (!_configurationService.Configuration.SpanEventsEnabled && !_configurationService.Configuration.TransactionEventsEnabled)
            {
                Log.Finest("Did not generate payload because Span Events and Transaction Events were both disabled, preventing a traceable payload.");
                return(DistributedTraceApiModel.EmptyModel);
            }

            transaction.SetSampled(_adaptiveSampler);
            var transactionIsSampled = transaction.Sampled;

            if (transactionIsSampled.HasValue == false)
            {
                Log.Error("Did not generate payload because transaction sampled value was null.");
                return(DistributedTraceApiModel.EmptyModel);
            }

            var payloadGuid   = _configurationService.Configuration.SpanEventsEnabled ? segment?.SpanId : null;
            var trustKey      = _configurationService.Configuration.TrustedAccountKey;
            var transactionId = (_configurationService.Configuration.TransactionEventsEnabled) ? transaction.Guid : null;
            var traceId       = transaction.TraceId;

            var distributedTracePayload = DistributedTracePayload.TryBuildOutgoingPayload(
                DistributedTraceTypeDefault,
                accountId,
                appId,
                payloadGuid,
                traceId,
                trustKey,
                transaction.Priority,
                transactionIsSampled,
                timestamp,
                transactionId);

            if (distributedTracePayload == null)
            {
                return(DistributedTraceApiModel.EmptyModel);
            }

            string encodedPayload;

            try
            {
                encodedPayload = DistributedTracePayload.SerializeAndEncodeDistributedTracePayload(distributedTracePayload);
            }
            catch (Exception ex)
            {
                Log.Error($"Failed to get encoded distributed trace headers for outbound request: {ex}");
                _agentHealthReporter.ReportSupportabilityDistributedTraceCreatePayloadException();
                return(DistributedTraceApiModel.EmptyModel);
            }

            transaction.TransactionMetadata.HasOutgoingTraceHeaders = true;

            if (_configurationService.Configuration.PayloadSuccessMetricsEnabled)
            {
                _agentHealthReporter.ReportSupportabilityDistributedTraceCreatePayloadSuccess();
            }

            return(new DistributedTraceApiModel(encodedPayload));
        }