Esempio n. 1
0
        private static bool PerformInitialization()
        {
            // We will now try to load the "right" version of System.Diagnostics.DiagnosticSource.dll
            // (henceforth we'll use the abbreviation DiagnosticSource.dll).
            // Here, "right" is defined as: if the app is using DiagnosticSource.dll we prefer to load that version.
            // Otherwise we prefer the version included with this library, which should be a relatively recent one.

            Assembly diagnosticSourceAssembly = LoadDiagnosticSourceAssembly();

            if (diagnosticSourceAssembly == null)
            {
                return(false);
            }

            Type activityType = diagnosticSourceAssembly.GetType(ActivityType_FullName, throwOnError: true);

            if (activityType == null)
            {
                return(false);
            }

            var supportedFeatures = new SupportedFeatures()
            {
            };

            s_invoker = new DynamicInvokerOld(supportedFeatures, activityType, null, null);
            return(true);
        }
Esempio n. 2
0
        public void AddTag(string key, string value)
        {
            if (_activityInstance == null)
            {
                return;
            }

            DynamicInvokerOld invoker = DynamicLoader.Invoker;

            if (invoker.SupportedFeatures.FeatureSet_5000 || invoker.SupportedFeatures.FeatureSet_4020)
            {
                invoker.Activity.AddTag(_activityInstance, key, value);
            }
            else
            {
                string errMsg = FormatNotSupportedErrorMessage($"{nameof(AddTag)}(..)", "4020", invoker);
                throw new NotSupportedException(errMsg);
            }
        }
Esempio n. 3
0
        public string GetBaggageItem(string key)
        {
            if (_activityInstance == null)
            {
                return(null);
            }

            DynamicInvokerOld invoker = DynamicLoader.Invoker;

            if (invoker.SupportedFeatures.FeatureSet_5000 || invoker.SupportedFeatures.FeatureSet_4020)
            {
                return(invoker.Activity.GetBaggageItem(_activityInstance, key));
            }
            else
            {
                string errMsg = FormatNotSupportedErrorMessage($"{nameof(GetBaggageItem)}(..)", "4020", invoker);
                throw new NotSupportedException(errMsg);
            }
        }
Esempio n. 4
0
        private static string FormatNotSupportedErrorMessage(string apiName, string minRequiredFeatureSet, DynamicInvokerOld invoker)
        {
            string errMsg = $"{nameof(ActivityStub)}.{apiName} is not supported."
                            + $" Status: {{{nameof(DynamicLoader)}.{nameof(DynamicLoader.InitializationState)}={DynamicLoader.InitializationState.ToString()};"
                            + $" MinRequiredFeatureSet={minRequiredFeatureSet};"
                            + $" SupportedFeatureSets={invoker.SupportedFeatures.FormatFeatureSetSupportList()}}}";

            return(errMsg);
        }
Esempio n. 5
0
        public static ActivityStub StartNewActivity(string operationName,
                                                    ActivityKindStub activityKind,
                                                    ActivityContextStub parentContext,
                                                    IEnumerable <KeyValuePair <string, string> > tags)
        {
            if (!DynamicLoader.EnsureInitialized())
            {
                return(NoOpSingeltons.ActivityStub);
            }

            DynamicInvokerOld invoker = DynamicLoader.Invoker;

            if (invoker.SupportedFeatures.FeatureSet_5000)
            {
                object       activityInstance = invoker.ActivitySource.StartActivity(operationName, activityKind, parentContext, tags);
                ActivityStub activityStub     = Wrap(activityInstance);

                return(activityStub);
            }
            else if (invoker.SupportedFeatures.FeatureSet_4020)
            {
                object       activityInstance = invoker.Activity.Ctor(operationName);
                ActivityStub activityStub     = Wrap(activityInstance);

                // Older Activity versions do not have a concept of Trace and Span. Instead they have an Id and a RoodId.
                // We will generate a parent Id to match the logic employed by older activities so that RoodId acts as a TraceId.
                // See:
                // https://github.com/dotnet/runtime/blob/7666224cfcfb4349da28f9f7fb1de931528ef208/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Activity.cs#L39
                // https://github.com/dotnet/runtime/blob/7666224cfcfb4349da28f9f7fb1de931528ef208/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Activity.cs#L358
                // https://github.com/dotnet/runtime/blob/7666224cfcfb4349da28f9f7fb1de931528ef208/src/libraries/System.Diagnostics.DiagnosticSource/src/System/Diagnostics/Activity.cs#L405
                // https://github.com/dotnet/corefx/blob/master/src/System.Diagnostics.DiagnosticSource/src/ActivityUserGuide.md#id-format

                if (parentContext.IsNotInitialized())
                {
                    string parentId = "|" + parentContext.TraceIdHexString + "." + parentContext.SpanIdHexString + "_";
                    invoker.Activity.SetParentId(activityInstance, parentId);
                }

                // Internal is the default. For internal, we avoid creating supplemantal data.
                if (activityKind != ActivityKindStub.Internal)
                {
                    SupplementalActivityData supplementalData = activityStub.GetOrCreateSupplementalData();
                    supplementalData.ActivityKind = activityKind;
                }

                if (tags != null)
                {
                    foreach (KeyValuePair <string, string> tag in tags)
                    {
                        invoker.Activity.AddTag(activityInstance, tag.Key, tag.Value);
                    }
                }

                object diagnosticSource = invoker.DiagnosticListener.DefaultDiagnosticSource;
                invoker.DiagnosticListener.StartActivity(activityInstance, activityInstance);

                return(activityStub);
            }
            else
            {
                string errMsg = FormatNotSupportedErrorMessage($"{nameof(StartNewActivity)}(..)", "4020", invoker);
                throw new NotSupportedException(errMsg);
            }
        }