Exemple #1
0
        public void Inject <TCarrier>(ISpanContext spanContext, IFormat <TCarrier> format, TCarrier carrier)
        {
            var context = (LambdaSpanContext)spanContext;
            var span    = context.GetSpan();
            var payload = DistributedTracePayload.TryBuildOutgoingPayload(
                type: "App",
                accountId: AccountId,
                appId: PrimaryApplicationId,
                guid: span.Guid(),
                traceId: span.RootSpan.DistributedTracingState.InboundPayload?.TraceId ?? span.RootSpan.TransactionState.TransactionId,
                trustKey: TrustedAccountKey,
                priority: span.RootSpan.PrioritySamplingState.Priority,
                sampled: span.RootSpan.PrioritySamplingState.Sampled,
                timestamp: span.TimeStamp.DateTime,
                transactionId: span.RootSpan.TransactionState.TransactionId
                );

            // If we couldnt build a payload just return
            if (payload == null)
            {
                return;
            }

            if (format.Equals(BuiltinFormats.TextMap))
            {
                // "text" version of payload
                ((ITextMap)carrier).Set(NEWRELIC_TRACE_HEADER, payload.ToJson());
            }
            else if (format.Equals(BuiltinFormats.HttpHeaders))
            {
                // "httpSafe" version of payload
                ((ITextMap)carrier).Set(NEWRELIC_TRACE_HEADER, payload.SerializeAndEncodeDistributedTracePayload());
            }
            //else if (format.Equals(BuiltinFormats.Binary))
            //{
            //	var payloadBytes = Encoding.UTF8.GetBytes(payload.ToJson());
            //	((MemoryStream)carrier).Write(payloadBytes, 0, payloadBytes.Length);
            //}
        }
Exemple #2
0
        private string GetPayloadString <TCarrier>(IFormat <TCarrier> format, TCarrier carrier)
        {
            if (format == null || carrier == null)
            {
                var message = $"Null format or carrier.";
                _logger.Log(message, false, "ERROR");
                throw new ArgumentNullException(message);
            }

            var payload = string.Empty;

            if (format.Equals(BuiltinFormats.TextMap))
            {
                foreach (var entry in (ITextMap)carrier)
                {
                    if (entry.Key.ToLower() == NEWRELIC_TRACE_HEADER)
                    {
                        payload = entry.Value;
                        break;
                    }
                }
            }
            else if (format.Equals(BuiltinFormats.HttpHeaders))
            {
                if (((ITextMap)carrier).GetEnumerator() == null)
                {
                    throw new ArgumentNullException("Invalid carrier.");
                }

                foreach (var entry in (ITextMap)carrier)
                {
                    if (entry.Key.ToLower() == NEWRELIC_TRACE_HEADER)
                    {
                        payload = entry.Value;
                        break;
                    }
                }
            }
            else if (format.Equals(NewRelicFormats.Payload))
            {
                payload = ((IPayload)carrier).GetPayload ?? throw new NullReferenceException();  // we expect that an error will be thrown.
            }
            // Implemented in OT master and in Java side, but not in 0.12.
            //else if(format.Equals(BuiltinFormats.Binary))
            //{
            //	var byteStream = carrier as MemoryStream;
            //	if(byteStream == null)
            //	{
            //		throw new ArgumentNullException("Invalid carrier.");
            //	}

            //	var payloadBytes = byteStream.ToArray();
            //	payload = System.Text.Encoding.UTF8.GetString(payloadBytes);
            //}
            else
            {
                var message = $"Unable to extract payload from carrier: {carrier}";
                _logger.Log(message, false, "ERROR");
                throw new ArgumentNullException(message);
            }

            if (string.IsNullOrEmpty(payload))
            {
                return(null);
            }

            return(payload);
        }