Example #1
0
        /// <summary>
        /// Parse W3C trace context headers to ActivityContext object.
        /// </summary>
        /// <param name="traceParent">W3C trace parent header.</param>
        /// <param name="traceState">W3C trace state.</param>
        /// <param name="context">The ActivityContext object created from the parsing operation.</param>
        public static bool TryParse(string traceParent, string?traceState, out ActivityContext context)
        {
            if (traceParent == null)
            {
                throw new ArgumentNullException(nameof(traceParent));
            }

            return(Activity.TryConvertIdToContext(traceParent, traceState, out context));
        }
Example #2
0
        /// <summary>
        /// Parse W3C trace context headers to ActivityContext object.
        /// </summary>
        /// <param name="traceParent">W3C trace parent header.</param>
        /// <param name="traceState">W3C trace state.</param>
        /// <param name="context">The ActivityContext object created from the parsing operation.</param>
        public static bool TryParse(string?traceParent, string?traceState, out ActivityContext context)
        {
            if (traceParent is null)
            {
                context = default;
                return(false);
            }

            return(Activity.TryConvertIdToContext(traceParent, traceState, out context));
        }
Example #3
0
        /// <summary>
        /// Parse W3C trace context headers to ActivityContext object.
        /// </summary>
        /// <param name="traceParent">W3C trace parent header.</param>
        /// <param name="traceState">Trace state.</param>
        /// <returns>
        /// The ActivityContext object created from the parsing operation.
        /// </returns>
        public static ActivityContext Parse(string traceParent, string?traceState)
        {
            if (traceParent is null)
            {
                throw new ArgumentNullException(nameof(traceParent));
            }

            if (!Activity.TryConvertIdToContext(traceParent, traceState, out ActivityContext context))
            {
                throw new ArgumentException(SR.InvalidTraceParent);
            }

            return(context);
        }
Example #4
0
        private static void GetRootId(out string?parentId, out string?traceState, out bool isW3c, out IEnumerable <KeyValuePair <string, string?> >?baggage)
        {
            Activity?activity = Activity.Current;

            while (activity?.Parent is Activity parent)
            {
                activity = parent;
            }

            traceState = activity?.TraceStateString;
            parentId   = activity?.ParentId ?? activity?.Id;
            isW3c      = parentId is not null?Activity.TryConvertIdToContext(parentId, traceState, isRemote : false, out _) : false;

            baggage = activity?.Baggage;
        }
Example #5
0
        private Activity?StartActivity(string name, ActivityKind kind, ActivityContext context, string?parentId, IEnumerable <KeyValuePair <string, object?> >?tags, IEnumerable <ActivityLink>?links, DateTimeOffset startTime)
        {
            // _listeners can get assigned to null in Dispose.
            SynchronizedList <ActivityListener>?listeners = _listeners;

            if (listeners == null || listeners.Count == 0)
            {
                return(null);
            }

            Activity?activity = null;

            ActivityDataRequest dataRequest = ActivityDataRequest.None;
            bool?useContext = default;
            ActivityCreationOptions <ActivityContext> optionsWithContext = default;

            if (parentId != null)
            {
                var aco = new ActivityCreationOptions <string>(this, name, parentId, kind, tags, links);
                listeners.EnumWithFunc((ActivityListener listener, ref ActivityCreationOptions <string> data, ref ActivityDataRequest request, ref bool?canUseContext, ref ActivityCreationOptions <ActivityContext> dataWithContext) => {
                    GetRequestedData <string>?getRequestedDataUsingParentId = listener.GetRequestedDataUsingParentId;
                    if (getRequestedDataUsingParentId != null)
                    {
                        ActivityDataRequest dr = getRequestedDataUsingParentId(ref data);
                        if (dr > request)
                        {
                            request = dr;
                        }

                        // Stop the enumeration if we get the max value RecordingAndSampling.
                        return(request != ActivityDataRequest.AllDataAndRecorded);
                    }
                    else
                    {
                        // In case we have a parent Id and the listener not providing the GetRequestedDataUsingParentId, we'll try to find out if the following conditions are true:
                        //   - The listener is providing the GetRequestedDataUsingContext callback
                        //   - Can convert the parent Id to a Context
                        // Then we can call the listener GetRequestedDataUsingContext callback with the constructed context.
                        GetRequestedData <ActivityContext>?getRequestedDataUsingContext = listener.GetRequestedDataUsingContext;
                        if (getRequestedDataUsingContext != null)
                        {
                            if (!canUseContext.HasValue)
                            {
                                canUseContext = Activity.TryConvertIdToContext(parentId, out ActivityContext ctx);
                                if (canUseContext.Value)
                                {
                                    dataWithContext = new ActivityCreationOptions <ActivityContext>(data.Source, data.Name, ctx, data.Kind, data.Tags, data.Links);
                                }
                            }

                            if (canUseContext.Value)
                            {
                                ActivityDataRequest dr = getRequestedDataUsingContext(ref dataWithContext);
                                if (dr > request)
                                {
                                    request = dr;
                                }
                                // Stop the enumeration if we get the max value RecordingAndSampling.
                                return(request != ActivityDataRequest.AllDataAndRecorded);
                            }
                        }
                    }
                    return(true);
                }, ref aco, ref dataRequest, ref useContext, ref optionsWithContext);
            }
            else
            {
                ActivityContext initializedContext = context == default && Activity.Current != null ? Activity.Current.Context : context;
                optionsWithContext = new ActivityCreationOptions <ActivityContext>(this, name, initializedContext, kind, tags, links);
                listeners.EnumWithFunc((ActivityListener listener, ref ActivityCreationOptions <ActivityContext> data, ref ActivityDataRequest request, ref bool?canUseContext, ref ActivityCreationOptions <ActivityContext> dataWithContext) => {
                    GetRequestedData <ActivityContext>?getRequestedDataUsingContext = listener.GetRequestedDataUsingContext;
                    if (getRequestedDataUsingContext != null)
                    {
                        if (listener.AutoGenerateRootContextTraceId && !canUseContext.HasValue && data.Parent == default)
                        {
                            ActivityContext ctx = new ActivityContext(ActivityTraceId.CreateRandom(), default, default);