public void RichPayloadEventSourceSanitizeTest()
        {
            if (IsRunningOnEnvironmentSupportingRichPayloadEventSource())
            {
                var request = new RequestTelemetry()
                {
                    Name         = new String('n', 5000),
                    Url          = new Uri("https://www.bing.com/" + new String('u', 5000)),
                    ResponseCode = "200"
                };

                var client = CreateTelemetryClient();

                using (var listener = new Microsoft.ApplicationInsights.TestFramework.TestEventListener())
                {
                    listener.EnableEvents(RichPayloadEventSource.Log.EventSourceInternal, EventLevel.Verbose, RichPayloadEventSource.Keywords.Requests);

                    client.Track(request);

                    IDictionary <string, object> richPayload = (IDictionary <string, object>)listener.Messages.FirstOrDefault().Payload[2];

                    Assert.AreEqual(Property.MaxNameLength, richPayload["name"].ToString().Length);
                    Assert.AreEqual(Property.MaxUrlLength, richPayload["url"].ToString().Length);
                    Assert.AreEqual(true, richPayload["success"]);
                };
            }
        }
        public void RichPayloadEventSourceTraceSentTest()
        {
            var client = new TelemetryClient();
            client.InstrumentationKey = Guid.NewGuid().ToString();

            using (var listener = new Microsoft.ApplicationInsights.TestFramework.TestEventListener())
            {
                listener.EnableEvents(RichPayloadEventSource.Log.EventSourceInternal, EventLevel.Verbose, RichPayloadEventSource.Keywords.Traces);

                var item = new TraceTelemetry("TestTrace", SeverityLevel.Information);
                item.Context.Properties.Add("property1", "value1");
                item.Context.User.Id = "testUserId";
                item.Context.Operation.Id = Guid.NewGuid().ToString();

                client.TrackTrace(item);

                var actualEvent = listener.Messages.FirstOrDefault();

                Assert.IsNotNull(actualEvent);
                Assert.AreEqual(client.InstrumentationKey, actualEvent.Payload[0]);

                object[] tags = actualEvent.Payload[1] as object[];
                Assert.AreEqual("ai.user.id", ((Dictionary<string, object>)(tags[0]))["Key"]);
                Assert.AreEqual("testUserId", ((Dictionary<string, object>)(tags[0]))["Value"]);

                Assert.AreEqual("ai.operation.id", ((Dictionary<string, object>)(tags[1]))["Key"]);
                Assert.AreEqual(item.Context.Operation.Id, ((Dictionary<string, object>)(tags[1]))["Value"]);

                Assert.IsNotNull(actualEvent.Payload[2]);
            }
        }
Пример #3
0
        public void RichPayloadEventSourceEventSentTest()
        {
            var client = new TelemetryClient();

            client.InstrumentationKey = Guid.NewGuid().ToString();

            using (var listener = new Microsoft.ApplicationInsights.TestFramework.TestEventListener())
            {
                listener.EnableEvents(RichPayloadEventSource.Log.EventSourceInternal, EventLevel.Verbose, RichPayloadEventSource.Keywords.Events);

                var item = new EventTelemetry("TestEvent");
                item.Context.Properties.Add("property1", "value1");
                item.Context.User.Id      = "testUserId";
                item.Context.Operation.Id = Guid.NewGuid().ToString();

                client.TrackEvent(item);

                var actualEvent = listener.Messages.FirstOrDefault();

                Assert.IsNotNull(actualEvent);
                Assert.AreEqual(client.InstrumentationKey, actualEvent.Payload[0]);

                object[] tags = actualEvent.Payload[1] as object[];
                Assert.AreEqual("ai.user.id", ((Dictionary <string, object>)(tags[0]))["Key"]);
                Assert.AreEqual("testUserId", ((Dictionary <string, object>)(tags[0]))["Value"]);

                Assert.AreEqual("ai.operation.id", ((Dictionary <string, object>)(tags[1]))["Key"]);
                Assert.AreEqual(item.Context.Operation.Id, ((Dictionary <string, object>)(tags[1]))["Value"]);

                Assert.IsNotNull(actualEvent.Payload[2]);
            }
        }
Пример #4
0
        /// <summary>
        /// Helper method to setup shared context and call the desired tracking for testing.
        /// </summary>
        /// <param name="keywords">The event keywords to enable.</param>
        /// <param name="item">The telemetry item to track.</param>
        /// <param name="track">The tracking callback to execute.</param>
        private void DoTracking(EventKeywords keywords, ITelemetry item, Type dataType, Action <TelemetryClient, ITelemetry> track)
        {
            if (IsRunningOnEnvironmentSupportingRichPayloadEventSource())
            {
                var client = new TelemetryClient();
                client.InstrumentationKey = Guid.NewGuid().ToString();

                using (var listener = new Microsoft.ApplicationInsights.TestFramework.TestEventListener())
                {
                    listener.EnableEvents(RichPayloadEventSource.Log.EventSourceInternal, EventLevel.Verbose, keywords);

                    item.Context.Properties.Add("property1", "value1");
                    item.Context.User.Id      = "testUserId";
                    item.Context.Operation.Id = Guid.NewGuid().ToString();

                    track(client, item);

                    var actualEvent = listener.Messages.FirstOrDefault();

                    Assert.IsNotNull(actualEvent);
                    Assert.AreEqual(client.InstrumentationKey, actualEvent.Payload[0]);

                    int      keysFound = 0;
                    object[] tags      = actualEvent.Payload[1] as object[];
                    foreach (object tagObject in tags)
                    {
                        Dictionary <string, object> tag = (Dictionary <string, object>)tagObject;
                        Assert.IsNotNull(tag);
                        string key   = (string)tag["Key"];
                        object value = tag["Value"];
                        if (!string.IsNullOrWhiteSpace(key))
                        {
                            if (key == "ai.user.id")
                            {
                                Assert.AreEqual("testUserId", value);
                                ++keysFound;
                            }
                            else if (key == "ai.operation.id")
                            {
                                Assert.AreEqual(item.Context.Operation.Id, value);
                                ++keysFound;
                            }
                        }
                    }

                    Assert.AreEqual(2, keysFound);
                    Assert.IsNotNull(actualEvent.Payload[2]);

                    var expectedProperties      = dataType.GetProperties().AsEnumerable();
                    var actualPropertiesPayload = (IDictionary <string, object>)actualEvent.Payload[2];
                    VerifyEventPayload(expectedProperties, actualPropertiesPayload);
                }
            }
            else
            {
                // 4.5 doesn't have RichPayload events
                Assert.IsNull(RichPayloadEventSource.Log.EventSourceInternal);
            }
        }
Пример #5
0
        public void RichPayloadEventSourceSanitizeTest()
        {
            if (IsRunningOnEnvironmentSupportingRichPayloadEventSource())
            {
                var request = new RequestTelemetry()
                {
                    Name         = new String('n', 5000),
                    Url          = new Uri("https://www.bing.com/" + new String('u', 5000)),
                    ResponseCode = "200"
                };

                string propKeyNameToBeTrimmed       = new String('a', Property.MaxDictionaryNameLength) + 1;
                string propValueToBeTrimmed         = new String('b', Property.MaxValueLength) + 1;
                string globalPropKeyNameToBeTrimmed = new String('c', Property.MaxDictionaryNameLength) + 1;
                string globalPropValueToBeTrimmed   = new String('d', Property.MaxValueLength) + 1;

                string propKeyNameAfterTrimmed       = new String('a', Property.MaxDictionaryNameLength);
                string propValueAfterTrimmed         = new String('b', Property.MaxValueLength);
                string globalPropKeyNameAfterTrimmed = new String('c', Property.MaxDictionaryNameLength);
                string globalPropValueAfterTrimmed   = new String('d', Property.MaxValueLength);

                request.Properties.Add(propKeyNameToBeTrimmed, propValueToBeTrimmed);
                request.Properties.Add(globalPropKeyNameToBeTrimmed, globalPropValueToBeTrimmed);

                var client = CreateTelemetryClient();

                using (var listener = new Microsoft.ApplicationInsights.TestFramework.TestEventListener())
                {
                    listener.EnableEvents(RichPayloadEventSource.Log.EventSourceInternal, EventLevel.Verbose, RichPayloadEventSource.Keywords.Requests);

                    client.Track(request);

                    IDictionary <string, object> richPayload = (IDictionary <string, object>)listener.Messages.FirstOrDefault().Payload[2];

                    Assert.AreEqual(Property.MaxNameLength, richPayload["name"].ToString().Length);
                    Assert.AreEqual(Property.MaxUrlLength, richPayload["url"].ToString().Length);
                    Assert.AreEqual(true, richPayload["success"]);

                    // Validates sanitize is done on Properties and GlobalProperties.
                    var prop    = ((object[])richPayload["properties"])[0];
                    var gblProp = ((object[])richPayload["properties"])[1];
                    ValidatePropertyDictionary((IDictionary <string, object>)prop, propKeyNameAfterTrimmed.Length, propValueAfterTrimmed.Length);
                    ValidatePropertyDictionary((IDictionary <string, object>)gblProp, propKeyNameAfterTrimmed.Length, propValueAfterTrimmed.Length);
                };
            }
        }
Пример #6
0
        private static void VerifyMethodImplementation(EventSource eventSource, MethodInfo eventMethod)
        {
            using (var listener = new Microsoft.ApplicationInsights.TestFramework.TestEventListener())
            {
                const long AllKeywords = -1;
                listener.EnableEvents(eventSource, EventLevel.Verbose, (EventKeywords)AllKeywords);
                try
                {
                    object[] eventArguments = GenerateEventArguments(eventMethod);
                    eventMethod.Invoke(eventSource, eventArguments);

                    EventWrittenEventArgs actualEvent = listener.Messages.First();
                    VerifyEventId(eventMethod, actualEvent);
                    VerifyEventLevel(eventMethod, actualEvent);
                    VerifyEventMessage(eventMethod, actualEvent, eventArguments);
                    VerifyEventApplicationName(eventMethod, actualEvent);
                }
                catch (Exception e)
                {
                    throw new Exception(eventMethod.Name + " is implemented incorrectly: " + e.Message, e);
                }
            }
        }
        /// <summary>
        /// Helper method to setup shared context and call the desired tracking for testing.
        /// </summary>
        /// <param name="keywords">The event keywords to enable.</param>
        /// <param name="item">The telemetry item to track.</param>
        /// <param name="track">The tracking callback to execute.</param>
        private void DoTracking(EventKeywords keywords, ITelemetry item, Action<TelemetryClient, ITelemetry> track)
        {
            var client = new TelemetryClient();
            client.InstrumentationKey = Guid.NewGuid().ToString();

            using (var listener = new Microsoft.ApplicationInsights.TestFramework.TestEventListener())
            {
                listener.EnableEvents(RichPayloadEventSource.Log.EventSourceInternal, EventLevel.Verbose, keywords);

                item.Context.Properties.Add("property1", "value1");
                item.Context.User.Id = "testUserId";
                item.Context.Operation.Id = Guid.NewGuid().ToString();

                track(client, item);

                var actualEvent = listener.Messages.FirstOrDefault();

                Assert.IsNotNull(actualEvent);
                Assert.AreEqual(client.InstrumentationKey, actualEvent.Payload[0]);

                int keysFound = 0;
                object[] tags = actualEvent.Payload[1] as object[];
                foreach (object tagObject in tags)
                {
                    Dictionary<string, object> tag = (Dictionary<string, object>)tagObject;
                    Assert.IsNotNull(tag);
                    string key = (string)tag["Key"];
                    object value = tag["Value"];
                    if (!string.IsNullOrWhiteSpace(key))
                    {
                        if (key == "ai.user.id")
                        {
                            Assert.AreEqual("testUserId", value);
                            ++keysFound;
                        }
                        else if (key == "ai.operation.id")
                        {
                            Assert.AreEqual(item.Context.Operation.Id, value);
                            ++keysFound;
                        }
                    }
                }

                Assert.AreEqual(2, keysFound);
                Assert.IsNotNull(actualEvent.Payload[2]);
            }
        }
        /// <summary>
        /// Helper method to setup shared context and call the desired tracking for testing.
        /// </summary>
        /// <param name="keywords">The event keywords to enable.</param>
        /// <param name="item">The telemetry item to track.</param>
        /// <param name="track">The tracking callback to execute.</param>
        private void DoTracking(EventKeywords keywords, ITelemetry item, Type dataType, Action <TelemetryClient, ITelemetry> track)
        {
            if (IsRunningOnEnvironmentSupportingRichPayloadEventSource())
            {
                var client = CreateTelemetryClient();

                using (var listener = new Microsoft.ApplicationInsights.TestFramework.TestEventListener())
                {
                    listener.EnableEvents(RichPayloadEventSource.Log.EventSourceInternal, EventLevel.Verbose, keywords);

#pragma warning disable CS0618 // Type or member is obsolete
                    item.Context.Properties.Add("property1", "value1");
#pragma warning restore CS0618 // Type or member is obsolete
                    (item as ISupportProperties)?.Properties.Add("itemprop1", "itemvalue1");
                    item.Context.GlobalProperties.Add("globalproperty1", "globalvalue1");
                    item.Context.User.Id      = "testUserId";
                    item.Context.Operation.Id = Guid.NewGuid().ToString();

                    track(client, item);

                    var actualEvent = listener.Messages.FirstOrDefault();

                    Assert.IsNotNull(actualEvent);
                    Assert.AreEqual(client.InstrumentationKey, actualEvent.Payload[0]);

                    int      keysFound = 0;
                    object[] tags      = actualEvent.Payload[1] as object[];
                    foreach (object tagObject in tags)
                    {
                        Dictionary <string, object> tag = (Dictionary <string, object>)tagObject;
                        Assert.IsNotNull(tag);
                        string key   = (string)tag["Key"];
                        object value = tag["Value"];
                        if (!string.IsNullOrWhiteSpace(key))
                        {
                            if (key == "ai.user.id")
                            {
                                Assert.AreEqual("testUserId", value);
                                ++keysFound;
                            }
                            else if (key == "ai.operation.id")
                            {
                                Assert.AreEqual(item.Context.Operation.Id, value);
                                ++keysFound;
                            }
                        }
                    }

                    Assert.AreEqual(2, keysFound);
                    Assert.IsNotNull(actualEvent.Payload[2]);

                    if (item is ISupportProperties)
                    {
                        object[] properties = (object[])((IDictionary <string, object>)actualEvent.Payload[2])["properties"];
#pragma warning disable CS0618 // Type or member is obsolete
                        if (!(item is PerformanceCounterTelemetry))
#pragma warning restore CS0618 // Type or member is obsolete
                        {
                            // There should be 3 entries in properties
                            // 1. from item's ISupportProperties.Properties
                            // 2. from item context.GlobalProperties
                            // 3. from item context.Properties
                            Assert.AreEqual(3, properties.Length);
                        }
                        else
                        {
                            // There should be 4 entries in properties
                            // 4. PerfCounter name is a custom property.
                            Assert.AreEqual(4, properties.Length);
                        }
                    }

                    var expectedProperties      = dataType.GetProperties().AsEnumerable();
                    var actualPropertiesPayload = (IDictionary <string, object>)actualEvent.Payload[2];
                    VerifyEventPayload(expectedProperties, actualPropertiesPayload);
                }
            }
            else
            {
                // 4.5 doesn't have RichPayload events
                Assert.IsNull(RichPayloadEventSource.Log.EventSourceInternal);
            }
        }