コード例 #1
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);
                };
            }
        }
コード例 #2
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 = 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();

                    item.Extension = new MyTestExtension {
                        myIntField = 42, myStringField = "value"
                    };

                    track(client, item);

                    var actualEvent = listener.Messages.FirstOrDefault();
#pragma warning disable CS0618                       // Type or member is obsolete
                    if (!(item is UnknownTelemetry)) // Global properties are copied directly into output properties for unknown telemetry
                    {
                        Assert.IsTrue(item.Context.Properties.ContainsKey("globalproperty1"), "Item Properties should contain the globalproperties as its copied before serialization");
                    }
#pragma warning restore CS0618 // Type or member is obsolete

                    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 (string.Equals(key, "ai.user.id", StringComparison.Ordinal))
                            {
                                Assert.AreEqual("testUserId", value);
                                ++keysFound;
                            }
                            else if (string.Equals(key, "ai.operation.id", StringComparison.Ordinal))
                            {
                                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 6 entries in properties
                            // 1. from item's ISupportProperties.Properties
                            // 2. from item context.GlobalProperties
                            // 3. from item context.Properties
                            // 4. from myInfField in item's Extension
                            // 5. from myStringField in item's Extension
                            // 6. PerfCounter name is a custom property.
                            Assert.AreEqual(6, properties.Length);
                        }
                        else if (item is UnknownTelemetry)
                        {
                            // There should be 11 entries in properties, all fields are flattened into properties
                            // 1. from item's ISupportProperties.Properties
                            // 2. from item context.GlobalProperties
                            // 3. from item context.Properties
                            // 4. from myInfField in item's Extension
                            // 5. from myStringField in item's Extension
                            // 6. Unknown Telemetry name.
                            // 7. Unknown Telemetry id
                            // 8. Unknown Telemetry responseCode
                            // 9. Unknown Telemetry source
                            // 10. Unknown Telemetry duration
                            // 11. Unknown Telemetry success
                            Assert.AreEqual(11, properties.Length);
                        }
                        else
                        {
                            // There should be 5 entries in properties
                            // 1. from item's ISupportProperties.Properties
                            // 2. from item context.GlobalProperties
                            // 3. from item context.Properties
                            // 4. from myInfField in item's Extension
                            // 5. from myStringField in item's Extension
                            Assert.AreEqual(5, 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);
            }
        }