Beispiel #1
0
        // Serializes a DistributedContext to the on-the-wire format.
        // Encoded tags are of the form: <version_id><encoded_tags>
        internal static byte[] SerializeBinary(DistributedContext dc)
        {
            // Use a ByteArrayDataOutput to avoid needing to handle IOExceptions.
            // ByteArrayDataOutput byteArrayDataOutput = ByteStreams.newDataOutput();
            var byteArrayDataOutput = new MemoryStream();

            byteArrayDataOutput.WriteByte(VersionId);
            var totalChars = 0; // Here chars are equivalent to bytes, since we're using ascii chars.

            foreach (var tag in dc.CorrelationContext.Entries)
            {
                totalChars += tag.Key.Length;
                totalChars += tag.Value.Length;
                EncodeTag(tag, byteArrayDataOutput);
            }

            // for (Iterator<Tag> i = InternalUtils.getTags(tags); i.hasNext();) {
            //    Tag tag = i.next();
            //    totalChars += tag.getKey().getName().length();
            //    totalChars += tag.getValue().asString().length();
            //    encodeTag(tag, byteArrayDataOutput);
            // }
            if (totalChars > TagContextSerializedSizeLimit)
            {
                OpenTelemetrySdkEventSource.Log.FailedToInjectContext("Size of DistributedContext exceeds the maximum serialized size "
                                                                      + TagContextSerializedSizeLimit);
                return(InvalidContext);
            }

            return(byteArrayDataOutput.ToArray());
        }
Beispiel #2
0
        internal static object Run(string projectId)
        {
            var spanExporter = new StackdriverTraceExporter(projectId);

            using (var tracerFactory = TracerFactory.Create(builder => builder.AddProcessorPipeline(c => c.SetExporter(spanExporter))))
            {
                var tracer = tracerFactory.GetTracer("stackdriver-test");

                DistributedContext.Carrier = AsyncLocalDistributedContextCarrier.Instance; // Enable asynclocal carrier for the context
                DistributedContext dc = DistributedContextBuilder.CreateContext(FrontendKey, "mobile-ios9.3.5");

                using (DistributedContext.SetCurrent(dc))
                {
                    using (tracer.StartActiveSpan("/getuser", out TelemetrySpan span))
                    {
                        span.AddEvent("Processing video.");
                        span.PutHttpMethodAttribute("GET");
                        span.PutHttpHostAttribute("localhost", 8080);
                        span.PutHttpPathAttribute("/resource");
                        span.PutHttpStatusCodeAttribute(200);
                        span.PutHttpUserAgentAttribute("Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0");

                        Thread.Sleep(TimeSpan.FromMilliseconds(10));
                    }
                }

                Thread.Sleep(TimeSpan.FromMilliseconds(5100));

                Console.WriteLine("Done... wait for events to arrive to backend!");
                Console.ReadLine();

                return(null);
            }
        }
Beispiel #3
0
        internal static object Run(string projectId)
        {
            var spanExporter = new StackdriverTraceExporter(projectId);

            using (var tracerFactory = TracerFactory.Create(builder => builder.AddProcessorPipeline(c => c.SetExporter(spanExporter))))
            {
                var tracer = tracerFactory.GetTracer("stackdriver-test");

                DistributedContext.Carrier = AsyncLocalDistributedContextCarrier.Instance; // Enable asynclocal carrier for the context
                DistributedContext dc = new DistributedContext(FrontendKey, "mobile-ios9.3.5");

                using (DistributedContext.SetCurrent(dc))
                {
                    using (tracer.StartActiveSpan("incoming request", out var span))
                    {
                        span.AddEvent("Processing video.");
                        Thread.Sleep(TimeSpan.FromMilliseconds(10));
                    }
                }

                Thread.Sleep(TimeSpan.FromMilliseconds(5100));

                Console.WriteLine("Done... wait for events to arrive to backend!");
                Console.ReadLine();

                return(null);
            }
        }
        internal static object Run()
        {
            DistributedContext.Carrier = AsyncLocalDistributedContextCarrier.Instance; // Enable asynclocal carrier for the context
            DistributedContext dc = DistributedContextBuilder.CreateContext(FrontendKey, "mobile-ios9.3.5");

            using (var tracerFactory = TracerFactory.Create(builder => builder
                                                            .SetResource(Resources.CreateServiceResource("my-service"))
                                                            .UseApplicationInsights(config => config.InstrumentationKey = "instrumentation-key")))
            {
                var tracer = tracerFactory.GetTracer("application-insights-test");

                using (DistributedContext.SetCurrent(dc))
                    using (tracer.StartActiveSpan("incoming request", out var span))
                    {
                        span.AddEvent("Start processing video.");
                        Thread.Sleep(TimeSpan.FromMilliseconds(10));
                        span.AddEvent("Finished processing video.");
                    }

                Thread.Sleep(TimeSpan.FromMilliseconds(5100));

                Console.WriteLine("Done... wait for events to arrive to backend!");
                Console.ReadLine();

                return(null);
            }
        }
        public void EmptyContext()
        {
            DistributedContext dc = new DistributedContext(new List <DistributedContextEntry>());

            Assert.Empty(dc.Entries);
            Assert.Equal(DistributedContext.Empty, dc);
        }
        private void TestRoundtripSerialization(DistributedContext expected)
        {
            var bytes  = serializer.ToByteArray(expected);
            var actual = serializer.FromByteArray(bytes);

            Assert.Equal(expected, actual);
        }
Beispiel #7
0
        public async void AsyncContextCarrier()
        {
            DistributedContext.Carrier = AsyncLocalDistributedContextCarrier.Instance;
            List <CorrelationContextEntry> list = new List <CorrelationContextEntry>(2)
            {
                new CorrelationContextEntry(KEY_1, VALUE_1), new CorrelationContextEntry(KEY_2, VALUE_2),
            };

            var dc1 = DistributedContextBuilder.CreateContext(KEY_1, VALUE_1);
            var dc2 = DistributedContextBuilder.CreateContext(list);

            DistributedContext.SetCurrent(DistributedContext.Empty);
            Assert.Equal(DistributedContext.Empty, DistributedContext.Current);

            using (DistributedContext.SetCurrent(dc1))
            {
                Assert.Equal(dc1, DistributedContext.Current);
                using (DistributedContext.SetCurrent(dc2))
                {
                    Assert.Equal(dc2, DistributedContext.Current);
                }

                Assert.Equal(dc1, DistributedContext.Current);

                using (DistributedContext.SetCurrent(dc2))
                {
                    await Task.Run(() => Assert.Equal(dc2, DistributedContext.Current));
                }
                await Task.Run(() => Assert.Equal(dc1, DistributedContext.Current));
            }
            Assert.Equal(DistributedContext.Empty, DistributedContext.Current);
            await Task.Run(() => Assert.Equal(DistributedContext.Empty, DistributedContext.Current));
        }
Beispiel #8
0
        public void TestSerializeTooLargeTagContext()
        {
            List <DistributedContextEntry> list = new List <DistributedContextEntry>();

            for (var i = 0; i < SerializationUtils.TagContextSerializedSizeLimit / 8 - 1; i++)
            {
                // Each tag will be with format {key : "0123", value : "0123"}, so the length of it is 8.
                String str;
                if (i < 10)
                {
                    str = "000" + i;
                }
                else if (i < 100)
                {
                    str = "00" + i;
                }
                else if (i < 1000)
                {
                    str = "0" + i;
                }
                else
                {
                    str = i.ToString();
                }
                list.Add(new DistributedContextEntry(str, str));
            }
            // The last tag will be of size 9, so the total size of the TagContext (8193) will be one byte
            // more than limit.
            list.Add(new DistributedContextEntry("last", "last1"));

            DistributedContext dc = DistributedContextBuilder.CreateContext(list);

            Assert.Empty(serializer.ToByteArray(dc));
        }
        public void TestNoTagsSerialization()
        {
            DistributedContext dc = serializer.FromByteArray(serializer.ToByteArray(DistributedContext.Empty));

            Assert.Empty(dc.Entries);

            dc = serializer.FromByteArray(new byte[] { SerializationUtils.VersionId }); // One byte that represents Version ID.
            Assert.Empty(dc.Entries);
        }
        public void NonEmptyContext()
        {
            List <DistributedContextEntry> list = new List <DistributedContextEntry>(2)
            {
                new DistributedContextEntry(K1, V1), new DistributedContextEntry(K2, V2)
            };
            DistributedContext dc = new DistributedContext(list);

            Assert.Equal(list, dc.Entries);
        }
        public void TestDeserializeOneTag()
        {
            var output = new MemoryStream();

            output.WriteByte(SerializationUtils.VersionId);
            EncodeTagToOutPut("Key", "Value", output);
            DistributedContext expected = DistributedContextBuilder.CreateContext("Key", "Value");

            Assert.Equal(expected, serializer.FromByteArray(output.ToArray()));
        }
Beispiel #12
0
        public void TestDeserializeDuplicateTags()
        {
            var output = new MemoryStream();

            output.WriteByte(SerializationUtils.VersionId);
            EncodeTagToOutPut("Key1", "Value1", output);
            EncodeTagToOutPut("Key1", "Value2", output);
            DistributedContext expected = new DistributedContext("Key1", "Value2");

            Assert.Equal(expected, serializer.FromByteArray(output.ToArray()));
        }
        public void AddExistingKey()
        {
            List <DistributedContextEntry> list = new List <DistributedContextEntry>(2)
            {
                new DistributedContextEntry(K1, V1), new DistributedContextEntry(K1, V2)
            };
            DistributedContext dc = new DistributedContext(list);

            Assert.Equal(new List <DistributedContextEntry>(1)
            {
                new DistributedContextEntry(K1, V2)
            }, dc.Entries);
        }
        public void TestDeserializeMultipleTags()
        {
            var output = new MemoryStream();

            output.WriteByte(SerializationUtils.VersionId);
            EncodeTagToOutPut("Key1", "Value1", output);
            EncodeTagToOutPut("Key2", "Value2", output);
            DistributedContext expected = DistributedContextBuilder.CreateContext(new List <DistributedContextEntry>(2)
            {
                new DistributedContextEntry("Key1", "Value1"), new DistributedContextEntry("Key2", "Value2")
            });

            Assert.Equal(expected, serializer.FromByteArray(output.ToArray()));
        }
Beispiel #15
0
        public void ContextCreation()
        {
            DistributedContext dc = DistributedContextBuilder.CreateContext(null);

            Assert.Equal(DistributedContext.Empty, dc);

            dc = DistributedContextBuilder.CreateContext(DistributedContext.Empty.Entries);
            Assert.Equal(DistributedContext.Empty, dc);

            dc = DistributedContextBuilder.CreateContext(KEY_1, VALUE_1);
            Assert.Equal(DistributedContextBuilder.CreateContext(List1), dc);

            Assert.Equal(dc, new DistributedContextBuilder(dc).Build());
        }
        public void AddExtraKey()
        {
            List <DistributedContextEntry> list = new List <DistributedContextEntry>(1)
            {
                new DistributedContextEntry(K1, V1)
            };
            DistributedContext dc = new DistributedContext(list);

            Assert.Equal(list, dc.Entries);

            list.Add(new DistributedContextEntry(K2, V2));
            DistributedContext dc1 = new DistributedContext(list);

            Assert.Equal(list, dc1.Entries);
        }
        public void TestRoundtripSerialization_NormalTagContext()
        {
            TestRoundtripSerialization(DistributedContext.Empty);
            TestRoundtripSerialization(DistributedContextBuilder.CreateContext(K1, V1));

            DistributedContext expected = DistributedContextBuilder.CreateContext(new List <DistributedContextEntry>(3)
            {
                new DistributedContextEntry(K1, V1),
                new DistributedContextEntry(K2, V2),
                new DistributedContextEntry(K3, V3),
            });

            TestRoundtripSerialization(expected);

            TestRoundtripSerialization(DistributedContextBuilder.CreateContext(K1, V_EMPTY));
        }
        public void EnsureEmptyListAfterBuild()
        {
            DistributedContextBuilder dcb = new DistributedContextBuilder(inheritCurrentContext: false);

            Assert.Equal(DistributedContext.Empty, dcb.Build());

            dcb.Add(list2);
            Assert.Equal(DistributedContextBuilder.CreateContext(list2), dcb.Build());
            Assert.Equal(DistributedContext.Empty, dcb.Build());

            DistributedContext dc = dcb.Add(list1).Build();

            Assert.Equal(dc, dcb.Add(list1).Build());

            dcb = new DistributedContextBuilder(dc);
            Assert.Equal(dc, dcb.Build());
            Assert.Equal(DistributedContext.Empty, dcb.Build());
        }
Beispiel #19
0
        public async void TestContextInheritance()
        {
            DistributedContext.Carrier = AsyncLocalDistributedContextCarrier.Instance;
            var list1 = new List <CorrelationContextEntry>(1)
            {
                new CorrelationContextEntry(KEY_1, VALUE_1)
            };
            var list2 = new List <CorrelationContextEntry>(2)
            {
                new CorrelationContextEntry(KEY_1, VALUE_1), new CorrelationContextEntry(KEY_2, VALUE_2)
            };

            DistributedContext.SetCurrent(DistributedContext.Empty);
            await Task.Run(() => Assert.Equal(DistributedContext.Empty, DistributedContext.Current));

            using (DistributedContext.SetCurrent(DistributedContextBuilder.CreateContext(list1)))
            {
                await Task.Run(() => Assert.Equal(DistributedContextBuilder.CreateContext(list1), DistributedContext.Current));

                using (DistributedContext.SetCurrent(new DistributedContextBuilder(inheritCurrentContext: true).Build()))
                {
                    await Task.Run(() => Assert.Equal(DistributedContextBuilder.CreateContext(list1), DistributedContext.Current));

                    using (DistributedContext.SetCurrent(new DistributedContextBuilder(inheritCurrentContext: true).Correlations(b => b.Add(KEY_2, VALUE_2)).Build()))
                    {
                        await Task.Run(() => Assert.Equal(DistributedContextBuilder.CreateContext(list2), DistributedContext.Current));

                        using (DistributedContext.SetCurrent(new DistributedContextBuilder(inheritCurrentContext: true).Correlations(b => b.Remove(KEY_2)).Build()))
                        {
                            await Task.Run(() => Assert.Equal(DistributedContextBuilder.CreateContext(list1), DistributedContext.Current));
                        }
                    }

                    await Task.Run(() => Assert.Equal(DistributedContextBuilder.CreateContext(list1), DistributedContext.Current));

                    using (DistributedContext.SetCurrent(new DistributedContextBuilder(inheritCurrentContext: false).Build()))
                    {
                        await Task.Run(() => Assert.Equal(DistributedContext.Empty, DistributedContext.Current));
                    }

                    await Task.Run(() => Assert.Equal(DistributedContextBuilder.CreateContext(list1), DistributedContext.Current));
                }
            }
        }
        public void TestDeserializeNonConsecutiveDuplicateKeys()
        {
            var output = new MemoryStream();

            output.WriteByte(SerializationUtils.VersionId);
            EncodeTagToOutPut("Key1", "Value1", output);
            EncodeTagToOutPut("Key2", "Value2", output);
            EncodeTagToOutPut("Key3", "Value3", output);
            EncodeTagToOutPut("Key1", "Value4", output);
            EncodeTagToOutPut("Key2", "Value5", output);

            DistributedContext expected = DistributedContextBuilder.CreateContext(new List <DistributedContextEntry>(3)
            {
                new DistributedContextEntry("Key1", "Value4"),
                new DistributedContextEntry("Key2", "Value5"),
                new DistributedContextEntry("Key3", "Value3"),
            });

            Assert.Equal(expected, serializer.FromByteArray(output.ToArray()));
        }
Beispiel #21
0
        public void NoopContextCarrier()
        {
            DistributedContext.Carrier = NoopDistributedContextCarrier.Instance;
            List <CorrelationContextEntry> list = new List <CorrelationContextEntry>(2)
            {
                new CorrelationContextEntry(KEY_1, VALUE_1), new CorrelationContextEntry(KEY_2, VALUE_2),
            };

            Assert.Equal(DistributedContext.Empty, DistributedContext.Current);

            using (DistributedContext.SetCurrent(DistributedContextBuilder.CreateContext(KEY_1, VALUE_1)))
            {
                Assert.Equal(DistributedContext.Empty, DistributedContext.Current);
                using (DistributedContext.SetCurrent(DistributedContextBuilder.CreateContext(list)))
                {
                    Assert.Equal(DistributedContext.Empty, DistributedContext.Current);
                }
            }

            Assert.Equal(DistributedContext.Empty, DistributedContext.Current);
        }
        public void StopParsingAtUnknownField()
        {
            var output = new MemoryStream();

            output.WriteByte(SerializationUtils.VersionId);
            EncodeTagToOutPut("Key1", "Value1", output);
            EncodeTagToOutPut("Key2", "Value2", output);

            // Write unknown field ID 1.
            output.WriteByte(1);
            output.Write(new byte[] { 1, 2, 3, 4 }, 0, 4);

            EncodeTagToOutPut("Key3", "Value3", output);

            DistributedContext expected = DistributedContextBuilder.CreateContext(new List <DistributedContextEntry>(2)
            {
                new DistributedContextEntry("Key1", "Value1"),
                new DistributedContextEntry("Key2", "Value2"),
            });

            Assert.Equal(expected, serializer.FromByteArray(output.ToArray()));
        }
 public abstract byte[] ToByteArray(DistributedContext tags);
 public override byte[] ToByteArray(DistributedContext context)
 {
     return(SerializationUtils.SerializeBinary(context));
 }