public void CheckForceFlushExport(int timeout)
        {
            var exportedItems = new List <Activity>();

            using var exporter  = new InMemoryExporter <Activity>(exportedItems);
            using var processor = new BatchExportProcessor <Activity>(
                      exporter,
                      maxQueueSize: 3,
                      maxExportBatchSize: 3,
                      exporterTimeoutMilliseconds: 30000);

            processor.OnEnd(new Activity("start1"));
            processor.OnEnd(new Activity("start2"));

            Assert.Equal(0, processor.ProcessedCount);

            // waiting to see if time is triggering the exporter
            Thread.Sleep(1_000);
            Assert.Empty(exportedItems);

            // forcing flush
            processor.ForceFlush(timeout);

            if (timeout == 0)
            {
                // ForceFlush(0) will trigger flush and return immediately, so let's sleep for a while
                Thread.Sleep(1_000);
            }

            Assert.Equal(2, exportedItems.Count);

            Assert.Equal(2, processor.ProcessedCount);
            Assert.Equal(2, processor.ReceivedCount);
            Assert.Equal(0, processor.DroppedCount);
        }
Esempio n. 2
0
        public void CheckShutdownExport(int timeout)
        {
            var exportedItems = new List <Activity>();

            using var exporter  = new InMemoryExporter <Activity>(exportedItems);
            using var processor = new BatchExportProcessor <Activity>(
                      exporter,
                      maxQueueSize: 3,
                      maxExportBatchSize: 3,
                      exporterTimeoutMilliseconds: 30000);

            var activity = new Activity("start");

            activity.ActivityTraceFlags = ActivityTraceFlags.Recorded;

            processor.OnEnd(activity);
            processor.Shutdown(timeout);

            if (timeout == 0)
            {
                // ForceFlush(0) will trigger flush and return immediately, so let's sleep for a while
                Thread.Sleep(1_000);
            }

            Assert.Single(exportedItems);

            Assert.Equal(1, processor.ProcessedCount);
            Assert.Equal(1, processor.ReceivedCount);
            Assert.Equal(0, processor.DroppedCount);
        }
Esempio n. 3
0
        public void CheckExportForRecordingButNotSampledActivity()
        {
            var exportedItems = new List <Activity>();

            using var exporter  = new InMemoryExporter <Activity>(exportedItems);
            using var processor = new BatchExportProcessor <Activity>(
                      exporter,
                      maxQueueSize: 1,
                      maxExportBatchSize: 1);

            var activity = new Activity("start");

            activity.ActivityTraceFlags = ActivityTraceFlags.None;

            processor.OnEnd(activity);
            processor.Shutdown();

            Assert.Empty(exportedItems);
            Assert.Equal(0, processor.ProcessedCount);
        }
        public void StackdriverExporter_TraceClientThrows_ExportResultFailure()
        {
            Exception    exception          = null;
            ExportResult result             = ExportResult.Success;
            const string ActivitySourceName = "stackdriver.test";
            var          source             = new ActivitySource(ActivitySourceName);
            var          traceClientMock    = new Mock <TraceServiceClient>(MockBehavior.Strict);

            traceClientMock.Setup(x =>
                                  x.BatchWriteSpans(It.IsAny <BatchWriteSpansRequest>(), It.IsAny <CallSettings>()))
            .Throws(new RpcException(Status.DefaultCancelled))
            .Verifiable($"{nameof(TraceServiceClient.BatchWriteSpans)} was never called");
            var activityExporter = new StackdriverTraceExporter("test", traceClientMock.Object);
            var testExporter     = new TestExporter <Activity>(RunTest);

            var processor = new BatchExportProcessor <Activity>(testExporter);

            for (int i = 0; i < 10; i++)
            {
                using Activity activity = source.StartActivity("Test Activity");
                processor.OnEnd(activity);
            }

            processor.Shutdown();

            void RunTest(Batch <Activity> batch)
            {
                exception = Record.Exception(() =>
                {
                    result = activityExporter.Export(batch);
                });
            }

            Assert.Null(exception);
            Assert.StrictEqual(ExportResult.Failure, result);
            traceClientMock.VerifyAll();
        }
        public void CheckShutdownExport(int timeout)
        {
            using var exporter  = new TestActivityExporter();
            using var processor = new BatchExportProcessor <Activity>(
                      exporter,
                      maxQueueSize: 3,
                      maxExportBatchSize: 3,
                      exporterTimeoutMilliseconds: 30000);

            processor.OnEnd(new Activity("start"));
            processor.Shutdown(timeout);

            if (timeout == 0)
            {
                // ForceFlush(0) will trigger flush and return immediately, so let's sleep for a while
                Thread.Sleep(1_000);
            }

            Assert.Single(exporter.Exported);

            Assert.Equal(1, processor.ProcessedCount);
            Assert.Equal(1, processor.ReceivedCount);
            Assert.Equal(0, processor.DroppedCount);
        }
        public void ToOtlpResourceSpansTest(bool addResource, string optionsServiceName)
        {
            var evenTags = new[] { new KeyValuePair <string, object>("k0", "v0") };
            var oddTags  = new[] { new KeyValuePair <string, object>("k1", "v1") };
            var sources  = new[]
            {
                new ActivitySource("even", "2.4.6"),
                new ActivitySource("odd", "1.3.5"),
            };

            var builder = Sdk.CreateTracerProviderBuilder()
                          .AddSource(sources[0].Name)
                          .AddSource(sources[1].Name);

            Resources.Resource resource = null;
            if (addResource)
            {
                resource = new Resources.Resource(
                    new List <KeyValuePair <string, object> >
                {
                    new KeyValuePair <string, object>(Resources.Resource.ServiceNameKey, "service-name"),
                    new KeyValuePair <string, object>(Resources.Resource.ServiceNamespaceKey, "ns1"),
                });

                builder.SetResource(resource);
            }

            using var openTelemetrySdk = builder.Build();

            var       processor  = new BatchExportProcessor <Activity>(new TestExporter <Activity>(RunTest));
            const int numOfSpans = 10;
            bool      isEven;

            for (var i = 0; i < numOfSpans; i++)
            {
                isEven = i % 2 == 0;
                var source       = sources[i % 2];
                var activityKind = isEven ? ActivityKind.Client : ActivityKind.Server;
                var activityTags = isEven ? evenTags : oddTags;

                using Activity activity = source.StartActivity($"span-{i}", activityKind, parentContext: default, activityTags);
                processor.OnEnd(activity);
            }

            processor.Shutdown();

            void RunTest(Batch <Activity> batch)
            {
                var request = new OtlpCollector.ExportTraceServiceRequest();

                request.AddBatch(
                    new OtlpExporter(
                        new OtlpExporterOptions
                {
                    ServiceName = optionsServiceName,
                },
                        new NoopTraceServiceClient()),
                    batch);

                Assert.Single(request.ResourceSpans);
                var oltpResource = request.ResourceSpans.First().Resource;

                if (addResource)
                {
                    Assert.Contains(oltpResource.Attributes, (kvp) => kvp.Key == Resources.Resource.ServiceNameKey && kvp.Value.StringValue == "service-name");
                    Assert.Contains(oltpResource.Attributes, (kvp) => kvp.Key == Resources.Resource.ServiceNamespaceKey && kvp.Value.StringValue == "ns1");
                }
                else
                {
                    Assert.Contains(oltpResource.Attributes, (kvp) => kvp.Key == Resources.Resource.ServiceNameKey && kvp.Value.StringValue == optionsServiceName);
                }

                foreach (var instrumentationLibrarySpans in request.ResourceSpans.First().InstrumentationLibrarySpans)
                {
                    Assert.Equal(numOfSpans / 2, instrumentationLibrarySpans.Spans.Count);
                    Assert.NotNull(instrumentationLibrarySpans.InstrumentationLibrary);

                    var expectedSpanNames = new List <string>();
                    var start             = instrumentationLibrarySpans.InstrumentationLibrary.Name == "even" ? 0 : 1;
                    for (var i = start; i < numOfSpans; i += 2)
                    {
                        expectedSpanNames.Add($"span-{i}");
                    }

                    var otlpSpans = instrumentationLibrarySpans.Spans;
                    Assert.Equal(expectedSpanNames.Count, otlpSpans.Count);

                    var kv0 = new OtlpCommon.KeyValue {
                        Key = "k0", Value = new OtlpCommon.AnyValue {
                            StringValue = "v0"
                        }
                    };
                    var kv1 = new OtlpCommon.KeyValue {
                        Key = "k1", Value = new OtlpCommon.AnyValue {
                            StringValue = "v1"
                        }
                    };

                    var expectedTag = instrumentationLibrarySpans.InstrumentationLibrary.Name == "even"
                        ? kv0
                        : kv1;

                    foreach (var otlpSpan in otlpSpans)
                    {
                        Assert.Contains(otlpSpan.Name, expectedSpanNames);
                        Assert.Contains(expectedTag, otlpSpan.Attributes);
                    }
                }
            }
        }