private IList?GetActivitySourceLinkCollection()
            {
                if (_links == null)
                {
                    return(null);
                }

                var linkCollection = ActivityExtensions.CreateLinkCollection();

                if (linkCollection == null)
                {
                    return(null);
                }

                foreach (var activity in _links)
                {
                    ICollection <KeyValuePair <string, object> >?linkTagsCollection = ActivityExtensions.CreateTagsCollection();
                    if (linkTagsCollection != null)
                    {
                        foreach (var tag in activity.Tags)
                        {
                            linkTagsCollection.Add(new KeyValuePair <string, object>(tag.Key, tag.Value !));
                        }
                    }

                    var link = ActivityExtensions.CreateActivityLink(activity.ParentId !, activity.TraceStateString, linkTagsCollection);
                    if (link != null)
                    {
                        linkCollection.Add(link);
                    }
                }

                return(linkCollection);
            }
Exemple #2
0
        private static TestAppContextSwitch SetAppConfigSwitch()
        {
            var s = new TestAppContextSwitch("Azure.Experimental.EnableActivitySource", "true");

            ActivityExtensions.ResetFeatureSwitch();
            return(s);
        }
        /// <summary>
        /// Computes the duration and tracks the respective telemetry item on dispose.
        /// </summary>
        protected virtual void Dispose(bool disposing)
        {
            if (disposing && !this.isDisposed)
            {
                // We need to compare the operation id and name of telemetry with operation id and name of current call context before tracking it
                // to make sure that the customer is tracking the right telemetry.
                lock (this)
                {
                    if (!this.isDisposed)
                    {
                        var operationTelemetry = this.Telemetry;
                        operationTelemetry.Stop();

                        bool isActivityAvailable = false;
                        isActivityAvailable = ActivityExtensions.TryRun(() =>
                        {
                            var currentActivity = Activity.Current;
                            if (currentActivity == null || operationTelemetry.Id != currentActivity.Id)
                            {
                                CoreEventSource.Log.InvalidOperationToStopError(
                                    string.Format(
                                        CultureInfo.InvariantCulture,
                                        "Telemetry Id '{0}' does not match current Activity '{1}'",
                                        operationTelemetry.Id,
                                        currentActivity?.Id));
                                return;
                            }

                            this.telemetryClient.Track(operationTelemetry);

                            currentActivity.Stop();
                        });

                        if (!isActivityAvailable)
                        {
                            var currentOperationContext = CallContextHelpers.GetCurrentOperationContext();
                            if (currentOperationContext == null || operationTelemetry.Id != currentOperationContext.ParentOperationId)
                            {
                                CoreEventSource.Log.InvalidOperationToStopError(
                                    string.Format(
                                        CultureInfo.InvariantCulture,
                                        "Telemetry Id '{0}' does not match current context '{1}'",
                                        operationTelemetry.Id,
                                        currentOperationContext?.ParentOperationId));
                                return;
                            }

                            this.telemetryClient.Track(operationTelemetry);

                            CallContextHelpers.RestoreOperationContext(this.ParentContext);
                        }
                    }

                    this.isDisposed = true;
                }
            }
        }
 private Activity?StartActivitySourceActivity()
 {
     return(ActivityExtensions.ActivitySourceStartActivity(
                this.activitySource,
                this.activityName,
                (int)this.kind,
                startTime: this.startTime,
                tags: this.tagCollection,
                links: this.GetActivitySourceLinkCollection()));
 }
 private Activity?StartActivitySourceActivity()
 {
     return(ActivityExtensions.ActivitySourceStartActivity(
                _activitySource,
                _activityName,
                (int)_kind,
                startTime: _startTime,
                tags: _tagCollection,
                links: GetActivitySourceLinkCollection()));
 }
Exemple #6
0
        public void DetectMentionedId(List <Entity> entities, bool expectsMention)
        {
            var message = new Activity()
            {
                Type     = ActivityTypes.Message,
                Entities = entities,
            };
            var mentionsId = ActivityExtensions.MentionsId(message, "ChannelAccountId");

            Assert.Equal(expectsMention, mentionsId);
        }
        public void Resource()
        {
            IList <string> expected = new[]
            {
                "Resource"
            };

            var candidate = ActivityExtensions.Activities("Resource", null).ToList();

            Check(expected, candidate);
        }
Exemple #8
0
        public DateViewHolder(View itemView, Action <int> OnClick = null) : base(itemView)
        {
            // Locate and cache view references:
            itemView.Click           += (sender, e) => OnClick?.Invoke(AdapterPosition);
            ItemView                  = itemView;
            ItemView.LayoutParameters = new ViewGroup.LayoutParams((int)(Application.Context.Resources.DisplayMetrics.WidthPixels / 5f), ViewGroup.LayoutParams.MatchParent);

            lblDate = itemView.FindViewById <TextView>(Resource.Id.lblDate);

            ActivityExtensions.SetViewFont(itemView as ViewGroup);
        }
Exemple #9
0
 /// <summary>
 /// Static Constructor which sets ActivityID Format to W3C if Format not enforced.
 /// This ensures SDK operates in W3C mode, unless turned off explicitily with the following 2 lines
 /// in user code in application startup.
 /// Activity.DefaultIdFormat = ActivityIdFormat.Hierarchical
 /// Activity.ForceDefaultIdFormat = true.
 /// </summary>
 static TelemetryConfiguration()
 {
     ActivityExtensions.TryRun(() =>
     {
         if (!Activity.ForceDefaultIdFormat)
         {
             Activity.DefaultIdFormat      = ActivityIdFormat.W3C;
             Activity.ForceDefaultIdFormat = true;
         }
     });
 }
 private void CreateAndFireCoreScope()
 {
     // copied from RequestActivityPolicy
     using DiagnosticScope coreScope = new DiagnosticScope(
               "Azure.Core.Http.Request",
               new DiagnosticListener("Azure.Core"),
               null,
               ActivityExtensions.CreateActivitySource("Azure.Core.Http"),
               DiagnosticScope.ActivityKind.Client,
               false);
     coreScope.Start();
 }
 public void AddTag(string name, string value)
 {
     if (_currentActivity == null)
     {
         // Activity is not started yet, add the value to the collection
         // that is going to be passed to StartActivity
         _tagCollection ??= ActivityExtensions.CreateTagsCollection() ?? new List <KeyValuePair <string, object> >();
         _tagCollection?.Add(new KeyValuePair <string, object>(name, value !));
     }
     else
     {
         _currentActivity?.AddTag(name, value !);
     }
 }
        public void ResourceHiearchyAction()
        {
            IList <string> expected = new[]
            {
                "Resource/Foo.Action",
                "Resource/Foo",
                "Resource.Action",
                "Resource",
                ".Action"
            };

            var candidate = ActivityExtensions.Activities("Resource/Foo", "Action").ToList();

            Check(expected, candidate);
        }
        public void ResourceActionHierarchy()
        {
            IList <string> expected = new[]
            {
                "Resource.Action/Bar",
                "Resource.Action",
                "Resource",
                ".Action/Bar",
                ".Action"
            };

            var candidate = ActivityExtensions.Activities("Resource", "Action/Bar").ToList();

            Check(expected, candidate);
        }
Exemple #14
0
        /// <summary>
        /// Computes the duration and tracks the respective telemetry item on dispose.
        /// </summary>
        protected virtual void Dispose(bool disposing)
        {
            if (disposing && !this.isDisposed)
            {
                // We need to compare the operation id and name of telemetry with operation id and name of current call context before tracking it
                // to make sure that the customer is tracking the right telemetry.
                lock (this)
                {
                    if (!this.isDisposed)
                    {
                        var  operationTelemetry  = this.Telemetry;
                        bool isActivityAvailable = false;
#if !NET40
                        isActivityAvailable = ActivityExtensions.TryRun(() =>
                        {
                            var currentActivity = Activity.Current;
                            if (currentActivity == null || operationTelemetry.Id != currentActivity.Id ||
                                operationTelemetry.Context.Operation.Name != currentActivity.GetOperationName())
                            {
                                CoreEventSource.Log.InvalidOperationToStopError();
                                return;
                            }

                            currentActivity.Stop();
                        });
#endif
                        if (!isActivityAvailable)
                        {
                            var currentOperationContext = CallContextHelpers.GetCurrentOperationContext();
                            if (currentOperationContext == null || operationTelemetry.Id != currentOperationContext.ParentOperationId ||
                                operationTelemetry.Context.Operation.Name != currentOperationContext.RootOperationName)
                            {
                                CoreEventSource.Log.InvalidOperationToStopError();
                                return;
                            }

                            CallContextHelpers.RestoreOperationContext(this.ParentContext);
                        }

                        operationTelemetry.Stop();

                        this.telemetryClient.Track(operationTelemetry);
                    }

                    this.isDisposed = true;
                }
            }
        }
Exemple #15
0
        public void DetectsMentionedRecipient(List <Entity> entities, bool expectsMention)
        {
            var message = new Activity()
            {
                Type      = ActivityTypes.Message,
                Entities  = entities,
                Recipient = new ChannelAccount
                {
                    Id         = "ChannelAccountId",
                    Name       = "ChannelAccountName",
                    Properties = new JObject {
                        { "Name", "Value" }
                    },
                    Role = "ChannelAccountRole",
                }
            };

            var mentionsRecipient = ActivityExtensions.MentionsRecipient(message);

            Assert.Equal(expectsMention, mentionsRecipient);
        }
Exemple #16
0
        public FeedItemViewHolder(View itemView, Action <int> OnClick) : base(itemView)
        {
            // Locate and cache view references:
            itemView.Click           += (sender, e) => OnClick(AdapterPosition);
            ItemView                  = itemView;
            ItemView.LayoutParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.WrapContent);
            btnLike       = itemView.FindViewById <ImageButton>(Resource.Id.btnLike);
            imgProfile    = itemView.FindViewById <ImageViewAsync>(Resource.Id.imgProfile);
            lblName       = itemView.FindViewById <TextView>(Resource.Id.lblName);
            btnOptions    = itemView.FindViewById <ImageButton>(Resource.Id.btnOptions);
            lblLikes      = itemView.FindViewById <TextView>(Resource.Id.lblLikes);
            btnComment    = itemView.FindViewById <ImageButton>(Resource.Id.btnComment);
            btnTag        = itemView.FindViewById <ImageButton>(Resource.Id.btnTag);
            btnShare      = itemView.FindViewById <ImageButton>(Resource.Id.btnShare);
            lblContent    = itemView.FindViewById <TextView>(Resource.Id.lblContent);
            lblTimeAgo    = itemView.FindViewById <TextView>(Resource.Id.lblTimeAgo);
            lblComments   = itemView.FindViewById <TextView>(Resource.Id.lblComments);
            lblShares     = itemView.FindViewById <TextView>(Resource.Id.lblShares);
            lblTags       = itemView.FindViewById <TextView>(Resource.Id.lblTags);
            lblLinkHost   = itemView.FindViewById <TextView>(Resource.Id.lblLinkHost);
            lblLinkTitle  = itemView.FindViewById <TextView>(Resource.Id.lblLinkTitle);
            imgImage      = itemView.FindViewById <ImageViewAsync>(Resource.Id.imgImage);
            btnPlay       = itemView.FindViewById <ImageButton>(Resource.Id.btnPlay);
            llLinkDetails = itemView.FindViewById <LinearLayout>(Resource.Id.llLinkDetails);
            rlMedia       = itemView.FindViewById <RelativeLayout>(Resource.Id.rlMedia);
            btnFacebook   = itemView.FindViewById <ImageButton>(Resource.Id.btnFacebook);
            btnTwitter    = itemView.FindViewById <ImageButton>(Resource.Id.btnTwitter);
            btnInstagram  = itemView.FindViewById <ImageButton>(Resource.Id.btnInstagram);
            llSharePost   = itemView.FindViewById <LinearLayout>(Resource.Id.linearLayout0);
            lblSharedFrom = itemView.FindViewById <TextView>(Resource.Id.lblNameShares);

            var parameters = rlMedia.LayoutParameters as LinearLayout.LayoutParams;
            var height     = Application.Context.Resources.DisplayMetrics.WidthPixels * 9 / 16f;

            parameters.Height        = (int)height;
            rlMedia.LayoutParameters = parameters;

            lblName.Typeface = CustomTypefaces.RobotoBold;
            ActivityExtensions.SetViewFont(itemView as ViewGroup);
        }
        View GetView(MenuItem item, int position, View convertView, ViewGroup parent)
        {
            View view = convertView;

            if (view == null)
            {
                if (GetType(item, position) == 0)
                {
                    view = Activity.LayoutInflater.Inflate(Resource.Layout.SearchHeaderItem, null);
                    view.FindViewById <TextView>(Resource.Id.lblTitle).Typeface = CustomTypefaces.RobotoBold;
                }
                else if (GetType(item, position) == 1)
                {
                    view = Activity.LayoutInflater.Inflate(Resource.Layout.MenuItem, null);
                    view.FindViewById <TextView>(Resource.Id.lblTitle).Typeface = CustomTypefaces.RobotoBold;
                }
                else
                {
                    view = Activity.LayoutInflater.Inflate(Resource.Layout.MenuFooterItem, null);
                }

                ActivityExtensions.SetViewFont(view as ViewGroup);
            }

            if (GetType(item, position) == 0)
            {
                view.FindViewById <TextView>(Resource.Id.lblTitle).Text = item.Title;
            }
            else if (GetType(item, position) == 1)
            {
                view.FindViewById <TextView>(Resource.Id.lblTitle).Text = item.Title;
                var id = Resources.GetIdentifier(item.Icon.ToLower(), "drawable", Activity.PackageName);
                view.FindViewById <ImageView>(Resource.Id.imgIcon).SetImageResource(id);
            }
            return(view);
        }
        public void ShowHelpIfNecessary(TutorialHelper tutorial, Action dismissed = null)
        {
            if (CrossSettings.Current.GetValueOrDefault(tutorial.Id, false))
            {
                dismissed?.Invoke();
                return;
            }
            CrossSettings.Current.AddOrUpdateValue(tutorial.Id, true);


            View view = LayoutInflater.Inflate(Resource.Layout.TutorialLayout, null);

            ActivityExtensions.SetViewFont(view as ViewGroup);

            var lblTitle    = view.FindViewById <TextView>(Resource.Id.lblTitle);
            var lblSubtitle = view.FindViewById <TextView>(Resource.Id.lblSubtitle);
            var imgImage    = view.FindViewById <ImageView>(Resource.Id.imgImage);
            var btnOk       = view.FindViewById <Button>(Resource.Id.btnOk);
            var id          = Resources.GetIdentifier(tutorial.AndroidIcon.ToLower(), "drawable", PackageName);

            imgImage.SetImageResource(id);

            lblTitle.Text = tutorial.Title;

            btnOk.Typeface    = CustomTypefaces.RobotoBold;
            lblTitle.Typeface = CustomTypefaces.RobotoBold;

            btnOk.Text = tutorial.ButtonText;

            if (tutorial.Id == "Welcome")
            {
                var span = new SpannableString(tutorial.Title);
                span.SetSpan(new FanwordTypefaceSpan(CustomTypefaces.RobotoBold), 8, tutorial.Title.Length, SpanTypes.ExclusiveExclusive);
                span.SetSpan(new ForegroundColorSpan(new Color(249, 95, 6)), 8, tutorial.Title.Length, SpanTypes.ExclusiveExclusive);
                lblTitle.TextFormatted = span;
            }

            lblSubtitle.Text = tutorial.Subtitle;

            var dialog = new AlertDialog.Builder(this).SetView(view).Create();

            if (btnOk.Text == "Follow Profiles")
            {
                btnOk.Click += (sender, e) =>
                {
                    dialog.Dismiss();
                    StartActivity(typeof(SearchActivity));
                };
            }
            else
            {
                btnOk.Click += (sender, e) =>
                {
                    dialog.Dismiss();
                };
            }


            dialog.DismissEvent += (sender, e) =>
            {
                dismissed?.Invoke();
            };

            dialog.Show();
        }
        /// <summary>
        /// Initializes/Adds operation id to the existing telemetry item.
        /// </summary>
        /// <param name="telemetryItem">Target telemetry item to add operation id.</param>
        public void Initialize(ITelemetry telemetryItem)
        {
            try
            {
                var  itemContext         = telemetryItem.Context.Operation;
                var  telemetryProp       = telemetryItem as ISupportProperties;
                bool isActivityAvailable = false;
                isActivityAvailable = ActivityExtensions.TryRun(() =>
                {
                    var currentActivity = Activity.Current;
                    if (currentActivity != null)
                    {
                        if (string.IsNullOrEmpty(itemContext.Id))
                        {
                            itemContext.Id = currentActivity.RootId;

                            if (string.IsNullOrEmpty(itemContext.ParentId))
                            {
                                itemContext.ParentId = currentActivity.Id;
                            }

                            foreach (var baggage in currentActivity.Baggage)
                            {
                                if (telemetryProp != null && !telemetryProp.Properties.ContainsKey(baggage.Key))
                                {
                                    telemetryProp.Properties.Add(baggage);
                                }
                            }
                        }

                        string operationName = currentActivity.GetOperationName();

                        if (string.IsNullOrEmpty(itemContext.Name) && !string.IsNullOrEmpty(operationName))
                        {
                            itemContext.Name = operationName;
                        }
                    }
                });

                if (!isActivityAvailable)
                {
                    if (string.IsNullOrEmpty(itemContext.ParentId) || string.IsNullOrEmpty(itemContext.Id) ||
                        string.IsNullOrEmpty(itemContext.Name))
                    {
                        var parentContext = CallContextHelpers.GetCurrentOperationContext();
                        if (parentContext != null)
                        {
                            if (string.IsNullOrEmpty(itemContext.ParentId) &&
                                !string.IsNullOrEmpty(parentContext.ParentOperationId))
                            {
                                itemContext.ParentId = parentContext.ParentOperationId;
                            }

                            if (string.IsNullOrEmpty(itemContext.Id) &&
                                !string.IsNullOrEmpty(parentContext.RootOperationId))
                            {
                                itemContext.Id = parentContext.RootOperationId;
                            }

                            if (string.IsNullOrEmpty(itemContext.Name) &&
                                !string.IsNullOrEmpty(parentContext.RootOperationName))
                            {
                                itemContext.Name = parentContext.RootOperationName;
                            }

                            if (parentContext.CorrelationContext != null)
                            {
                                foreach (var item in parentContext.CorrelationContext)
                                {
                                    if (telemetryProp != null && !telemetryProp.Properties.ContainsKey(item.Key))
                                    {
                                        telemetryProp.Properties.Add(item);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                TelemetryDebugWriter.WriteLine(
                    "Something went wrong when initializing [" + this.GetType().Name + "]. This initializer will be ignored." + ex);
            }
        }
        /// <summary>
        /// Initializes/Adds operation id to the existing telemetry item.
        /// </summary>
        /// <param name="telemetryItem">Target telemetry item to add operation id.</param>
        public void Initialize(ITelemetry telemetryItem)
        {
            var  itemContext         = telemetryItem.Context.Operation;
            var  telemetryProp       = telemetryItem as ISupportProperties;
            bool isActivityAvailable = false;

            isActivityAvailable = ActivityExtensions.TryRun(() =>
            {
                var currentActivity = Activity.Current;
                if (currentActivity != null)
                {
                    if (string.IsNullOrEmpty(itemContext.Id))
                    {
                        itemContext.Id = currentActivity.RootId;

                        if (string.IsNullOrEmpty(itemContext.ParentId))
                        {
                            itemContext.ParentId = currentActivity.Id;
                        }

                        foreach (var baggage in currentActivity.Baggage)
                        {
                            if (telemetryProp != null && !telemetryProp.Properties.ContainsKey(baggage.Key))
                            {
                                telemetryProp.Properties.Add(baggage);
                            }
                        }
                    }

                    string operationName = currentActivity.GetOperationName();

                    if (string.IsNullOrEmpty(itemContext.Name) && !string.IsNullOrEmpty(operationName))
                    {
                        itemContext.Name = operationName;
                    }
                }
            });

            if (!isActivityAvailable)
            {
                if (string.IsNullOrEmpty(itemContext.ParentId) || string.IsNullOrEmpty(itemContext.Id) || string.IsNullOrEmpty(itemContext.Name))
                {
                    var parentContext = CallContextHelpers.GetCurrentOperationContext();
                    if (parentContext != null)
                    {
                        if (string.IsNullOrEmpty(itemContext.ParentId) &&
                            !string.IsNullOrEmpty(parentContext.ParentOperationId))
                        {
                            itemContext.ParentId = parentContext.ParentOperationId;
                        }

                        if (string.IsNullOrEmpty(itemContext.Id) &&
                            !string.IsNullOrEmpty(parentContext.RootOperationId))
                        {
                            itemContext.Id = parentContext.RootOperationId;
                        }

                        if (string.IsNullOrEmpty(itemContext.Name) &&
                            !string.IsNullOrEmpty(parentContext.RootOperationName))
                        {
                            itemContext.Name = parentContext.RootOperationName;
                        }

                        if (parentContext.CorrelationContext != null)
                        {
                            foreach (var item in parentContext.CorrelationContext)
                            {
                                if (telemetryProp != null && !telemetryProp.Properties.ContainsKey(item.Key))
                                {
                                    telemetryProp.Properties.Add(item);
                                }
                            }
                        }
                    }
                }
            }
        }
Exemple #21
0
 public void ResetFeatureSwitch()
 {
     ActivityExtensions.ResetFeatureSwitch();
 }