public static CloudEventEntry TryConvertToCloudEventEntry(this EventEntry entry)
        {
            Guard.ArgumentNotNull(entry, "entry");

            var entity = new CloudEventEntry()
            {
                EventId = entry.EventId,
                Keywords = (long)entry.Schema.Keywords,
                ProviderId = entry.ProviderId,
                ProviderName = entry.Schema.ProviderName,
                Level = (int)entry.Schema.Level,
                Message = entry.FormattedMessage,
                Opcode = (int)entry.Schema.Opcode,
                Task = (int)entry.Schema.Task,
                Version = entry.Schema.Version,
                EventDate = entry.Timestamp.UtcDateTime
            };

            if (!InitializePayload(entity, entry.Payload, entry.Schema))
            {
                return null;
            }

            return entity;
        }
        private static bool InitializePayload(CloudEventEntry entity, IList<object> payload, EventSchema schema)
        {
            try
            {
                entity.Payload = new Dictionary<string, object>(payload.Count);

                for (int i = 0; i < payload.Count; i++)
                {
                    entity.Payload.Add(schema.Payload[i], payload[i]);
                }

                return true;
            }
            catch (Exception e)
            {
                SemanticLoggingEventSource.Log.WindowsAzureTableSinkEntityCreationFailed(e.ToString());
                return false;
            }
        }
        public void when_having_big_overall_payloads_then_stores_warning_and_does_not_contain_payload()
        {
            var entity = new CloudEventEntry(EventEntryTestHelper.Create(payloadNames: Enumerable.Range(0, 50).Select(i => "arg" + i), payload: Enumerable.Range(0, 50).Select(i => new string('a', 2000))));
            entity.CreateKey(true, 0);

            var dict = entity.CreateTableEntity().WriteEntity(null);

            StringAssert.Contains(dict["Payload"].StringValue, "'payload_serialization_error'");

            Assert.AreEqual(0, dict.Keys.Count(x => x.StartsWith("Payload_")));
        }
        public void when_having_big_payload_then_truncates()
        {
            var entity = new CloudEventEntry(EventEntryTestHelper.Create(payloadNames: new string[] { "arg1" }, payload: new object[] { new string('a', 500000) }));
            entity.CreateKey(true, 0);

            var dict = entity.CreateTableEntity().WriteEntity(null);

            StringAssert.Contains(dict["Payload"].StringValue, "'payload_serialization_error'");
            Assert.IsFalse(dict.ContainsKey("Payload_arg1"));
        }
        public void when_having_big_message_value_then_truncates()
        {
            var entity = new CloudEventEntry(EventEntryTestHelper.Create(formattedMessage: new string('a', 500000)));
            entity.CreateKey(true, 0);

            var dict = entity.CreateTableEntity().WriteEntity(null);

            Assert.AreEqual(new string('a', 30000) + "--TRUNCATED--", dict["Message"].StringValue);
        }
        public void when_generating_key_then_prefixes_with_instance_name()
        {
            var entity = new CloudEventEntry(EventEntryTestHelper.Create(
                timestamp: DateTimeOffset.UtcNow))
            {
                InstanceName = "MyInstanceName"
            };

            entity.CreateKey(false, 0);

            StringAssert.StartsWith(entity.RowKey, "MyInstanceName");
        }
        public void when_writing_entity_keeps_proper_type()
        {
            var guid = Guid.NewGuid();
            var binary = new byte[] { 1, 2, 3, 4 };

            var entity = new CloudEventEntry(EventEntryTestHelper.Create(
                payloadNames: new string[] { "string1", "int1", "long1", "double1", "bool1", "bool2", "guid1", "binary1" },
                payload: new object[] { "This is a string", 123456, 123456L, 123456D, true, false, guid, binary }));
            entity.CreateKey(true, 0);

            var dict = entity.CreateTableEntity().WriteEntity(null);

            Assert.AreEqual<string>("This is a string", dict["Payload_string1"].StringValue);
            Assert.AreEqual<int>(123456, dict["Payload_int1"].Int32Value.Value);
            Assert.AreEqual<long>(123456L, dict["Payload_long1"].Int64Value.Value);
            Assert.AreEqual<double>(123456D, dict["Payload_double1"].DoubleValue.Value);
            Assert.AreEqual<Guid>(guid, dict["Payload_guid1"].GuidValue.Value);
            Assert.AreEqual<bool>(true, dict["Payload_bool1"].BooleanValue.Value);
            Assert.AreEqual<bool>(false, dict["Payload_bool2"].BooleanValue.Value);
            Assert.AreEqual<byte[]>(binary, dict["Payload_binary1"].BinaryValue);
        }
        public void when_writing_entity_adds_payload_to_dictionary()
        {
            var entity = new CloudEventEntry(EventEntryTestHelper.Create(payloadNames: new string[] { "message1", "message2" }, payload: new object[] { "value1", "value2" }));
            entity.CreateKey(true, 0);

            var dict = entity.CreateTableEntity().WriteEntity(null);

            Assert.IsTrue(dict.ContainsKey("Payload_message1"));
            Assert.IsTrue(dict.ContainsKey("Payload_message2"));
        }
 protected override void Act()
 {
     base.Act();
     this.entry = new CloudEventEntry(EventEntryTestHelper.Create(
         eventId: 12,
         providerId: Guid.NewGuid(),
         providerName: "Provider Name",
         timestamp: new DateTimeOffset(2013, 4, 10, 16, 0, 0, TimeSpan.Zero),
         keywords: (EventKeywords)16,
         level: EventLevel.Informational,
         formattedMessage: "My message",
         opcode: (EventOpcode)4,
         task: (EventTask)24,
         version: 2,
         payloadNames: new string[] { "arg1" },
         payload: new object[] { "value arg1" },
         activityId: Guid.Parse("{562D0422-F427-4849-A6CD-7990A46F1223}")))
     {
         InstanceName = "Instance Name",
     };
     sink.OnNext(this.entry);
 }
Ejemplo n.º 10
0
        public void when_having_big_payload_then_truncates()
        {
            var entity = new CloudEventEntry();
            entity.CreateKey(true, 0);
            entity.Payload.Add("arg1", new string('a', 500000));

            var dict = entity.CreateTableEntity().WriteEntity(null);

            StringAssert.Contains(dict["Payload"].StringValue, "'payload_serialization_error'");
            Assert.IsFalse(dict.ContainsKey("Payload_arg1"));
        }
Ejemplo n.º 11
0
        public void when_generating_key_then_prefixes_with_instance_name()
        {
            var entity = new CloudEventEntry();
            entity.EventDate = DateTime.UtcNow;
            entity.InstanceName = "MyInstanceName";

            entity.CreateKey(false, 0);

            StringAssert.StartsWith(entity.RowKey, "MyInstanceName");
        }
Ejemplo n.º 12
0
        public void when_writing_entity_keeps_proper_type()
        {
            var guid = Guid.NewGuid();
            var binary = new byte[] { 1, 2, 3, 4 };

            var entity = new CloudEventEntry();
            entity.CreateKey(true, 0);
            entity.Payload.Add("string1", "This is a string");
            entity.Payload.Add("int1", 123456);
            entity.Payload.Add("long1", 123456L);
            entity.Payload.Add("double1", 123456D);
            entity.Payload.Add("bool1", true);
            entity.Payload.Add("bool2", false);
            entity.Payload.Add("guid1", guid);
            entity.Payload.Add("binary1", binary);

            var dict = entity.CreateTableEntity().WriteEntity(null);

            Assert.AreEqual<string>("This is a string", dict["Payload_string1"].StringValue);
            Assert.AreEqual<int>(123456, dict["Payload_int1"].Int32Value.Value);
            Assert.AreEqual<long>(123456L, dict["Payload_long1"].Int64Value.Value);
            Assert.AreEqual<double>(123456D, dict["Payload_double1"].DoubleValue.Value);
            Assert.AreEqual<Guid>(guid, dict["Payload_guid1"].GuidValue.Value);
            Assert.AreEqual<bool>(true, dict["Payload_bool1"].BooleanValue.Value);
            Assert.AreEqual<bool>(false, dict["Payload_bool2"].BooleanValue.Value);
            Assert.AreEqual<byte[]>(binary, dict["Payload_binary1"].BinaryValue);
        }
Ejemplo n.º 13
0
        public void when_writing_entity_adds_payload_to_dictionary()
        {
            var entity = new CloudEventEntry();
            entity.CreateKey(true, 0);
            entity.Payload.Add("message1", "value1");
            entity.Payload.Add("message2", "value2");

            var dict = entity.CreateTableEntity().WriteEntity(null);

            Assert.IsTrue(dict.ContainsKey("Payload_message1"));
            Assert.IsTrue(dict.ContainsKey("Payload_message2"));
        }
        public void when_several_payload_values_then_takes_first_ones_as_columns()
        {
            int numberOfAllowedItems = 200;

            var entity = new CloudEventEntry(EventEntryTestHelper.Create(
                payloadNames: Enumerable.Range(0, 300).Select(i => "arg" + i),
                payload: Enumerable.Range(0, 300).Select(i => (object)i)));
            entity.CreateKey(true, 0);

            var dict = entity.CreateTableEntity().WriteEntity(null);

            for (int i = 0; i < numberOfAllowedItems; i++)
            {
                Assert.IsTrue(dict.ContainsKey("Payload_arg" + i), i.ToString());
                Assert.AreEqual<int>(i, dict["Payload_arg" + i].Int32Value.Value);
            }

            for (int i = numberOfAllowedItems; i < 300; i++)
            {
                Assert.IsFalse(dict.ContainsKey("Payload_arg" + i), i.ToString());
            }

            var deserializedPayloadField = JsonConvert.DeserializeObject<Dictionary<string, object>>(dict["Payload"].StringValue);

            foreach (var payloadItem in entity.Payload)
            {
                Assert.IsTrue(deserializedPayloadField.ContainsKey(payloadItem.Key));
                Assert.AreEqual<int>((int)payloadItem.Value, (int)(long)deserializedPayloadField[payloadItem.Key]);
            }
        }
Ejemplo n.º 15
0
        public void when_having_big_overall_payloads_then_stores_warning_and_does_not_contain_payload()
        {
            var entity = new CloudEventEntry();
            entity.CreateKey(true, 0);

            for (int i = 0; i < 50; i++)
            {
                entity.Payload.Add("arg" + i, new string('a', 2000));
            }

            var dict = entity.CreateTableEntity().WriteEntity(null);

            StringAssert.Contains(dict["Payload"].StringValue, "'payload_serialization_error'");

            Assert.AreEqual(0, dict.Keys.Count(x => x.StartsWith("Payload_")));
        }
        public static DynamicTableEntity CreateTableEntity(this CloudEventEntry entry)
        {
            var dictionary = new Dictionary <string, EntityProperty>();

            dictionary.Add("EventId", new EntityProperty(entry.EventId));
            dictionary.Add("EventDate", new EntityProperty(entry.EventDate));
            dictionary.Add("Keywords", new EntityProperty(entry.Keywords));
            dictionary.Add("ProviderId", new EntityProperty(entry.ProviderId));
            dictionary.Add("ProviderName", new EntityProperty(entry.ProviderName));
            dictionary.Add("InstanceName", new EntityProperty(entry.InstanceName));
            dictionary.Add("Level", new EntityProperty(entry.Level));
            if (entry.Message != null)
            {
                dictionary.Add("Message", new EntityProperty(Normalize(entry.Message)));
            }

            dictionary.Add("Opcode", new EntityProperty(entry.Opcode));
            dictionary.Add("Task", new EntityProperty(entry.Task));
            dictionary.Add("Version", new EntityProperty(entry.Version));

            // Create a "Payload"
            if (entry.Payload != null && entry.Payload.Count > 0)
            {
                var json = EventEntryUtil.JsonSerializePayload(entry.Payload);
                if (json.Length > MaxStringLength)
                {
                    dictionary.Add("Payload", new EntityProperty("{ 'payload_serialization_error':'The payload is too big to serialize.' }"));
                }
                else
                {
                    dictionary.Add("Payload", new EntityProperty(json));

                    foreach (var item in entry.Payload.Take(MaxPayloadItems))
                    {
                        var value = item.Value;
                        if (value != null)
                        {
                            EntityProperty property = null;
                            var            type     = value.GetType();

                            if (type == typeof(string))
                            {
                                property = new EntityProperty((string)value);
                            }
                            else if (type == typeof(int))
                            {
                                property = new EntityProperty((int)value);
                            }
                            else if (type == typeof(long))
                            {
                                property = new EntityProperty((long)value);
                            }
                            else if (type == typeof(double))
                            {
                                property = new EntityProperty((double)value);
                            }
                            else if (type == typeof(Guid))
                            {
                                property = new EntityProperty((Guid)value);
                            }
                            else if (type == typeof(bool))
                            {
                                property = new EntityProperty((bool)value);
                            }
                            else if (type.IsEnum)
                            {
                                var typeCode = ((Enum)value).GetTypeCode();
                                if (typeCode <= TypeCode.Int32)
                                {
                                    property = new EntityProperty(Convert.ToInt32(value, CultureInfo.InvariantCulture));
                                }
                                else
                                {
                                    property = new EntityProperty(Convert.ToInt64(value, CultureInfo.InvariantCulture));
                                }
                            }
                            else if (type == typeof(byte[]))
                            {
                                property = new EntityProperty((byte[])value);
                            }

                            //// TODO: add & review DateTimeOffset if it's supported

                            if (property != null)
                            {
                                dictionary.Add(string.Format(CultureInfo.InvariantCulture, "Payload_{0}", item.Key), property);
                            }
                        }
                    }
                }
            }

            return(new DynamicTableEntity(entry.PartitionKey, entry.RowKey, null, dictionary));
        }
 protected override void Act()
 {
     base.Act();
     this.entry = new CloudEventEntry
                      {
                          EventId = 12,
                          ProviderId = Guid.NewGuid(),
                          ProviderName = "Provider Name",
                          EventDate = new DateTime(2013, 4, 10, 16, 0, 0, DateTimeKind.Utc),
                          Keywords = 16,
                          InstanceName = "Instance Name",
                          Level = (int)EventLevel.Informational,
                          Message = "My message",
                          Opcode = 4,
                          Task = 24,
                          Version = 2,
                          Payload = { { "arg1", "value arg1" } }
                      };
     sink.OnNext(this.entry);
 }