Example #1
0
        public void WhenUsingUdsAgent_DoesntSendTelemetry()
        {
            const string expectedOperationName = "http.request";
            const int    expectedSpanCount     = 1;
            const string serviceVersion        = "1.0.0";

            EnvironmentHelper.TransportType = TestTransports.Uds;
            using var agent = EnvironmentHelper.GetMockAgent();

            SetServiceVersion(serviceVersion);

            int telemetryPort = TcpPortProvider.GetOpenPort();

            using var telemetry = new MockTelemetryAgent <TelemetryData>(telemetryPort);

            SetEnvironmentVariable("DD_INSTRUMENTATION_TELEMETRY_ENABLED", "false");

            int httpPort = TcpPortProvider.GetOpenPort();

            Output.WriteLine($"Assigning port {httpPort} for the httpPort.");
            using (ProcessResult processResult = RunSampleAndWaitForExit(agent, arguments: $"Port={httpPort}"))
            {
                Assert.True(processResult.ExitCode == 0, $"Process exited with code {processResult.ExitCode}");

                var spans = agent.WaitForSpans(expectedSpanCount, operationName: expectedOperationName);
                Assert.Equal(expectedSpanCount, spans.Count);
            }

            // Shouldn't have any, but wait for 5s
            telemetry.WaitForLatestTelemetry(x => true);
            telemetry.Telemetry.Should().BeEmpty();
        }
        public static MockTelemetryAgent <TelemetryData> ConfigureTelemetry(this TestHelper helper)
        {
            int telemetryPort = TcpPortProvider.GetOpenPort();
            var telemetry     = new MockTelemetryAgent <TelemetryData>(telemetryPort);

            helper.SetEnvironmentVariable("DD_INSTRUMENTATION_TELEMETRY_ENABLED", "true");
            helper.SetEnvironmentVariable("DD_TRACE_TELEMETRY_URL", $"http://localhost:{telemetry.Port}");
            // add an api key to force using the custom url
            helper.SetEnvironmentVariable("DD_API_KEY", "INVALID_KEY_FOR_TESTS");
            return(telemetry);
        }
        public static TelemetryData AssertIntegration(this MockTelemetryAgent <TelemetryData> telemetry, IntegrationId integrationId, bool enabled, bool?autoEnabled)
        {
            telemetry.WaitForLatestTelemetry(x => x.RequestType == TelemetryRequestTypes.AppClosing);

            var allData = telemetry.Telemetry.ToArray();

            allData.Should().ContainSingle(x => x.RequestType == TelemetryRequestTypes.AppClosing);

            var(latestIntegrationsData, integrationsPayload) =
                allData
                .Where(
                    x => x.RequestType == TelemetryRequestTypes.AppStarted ||
                    x.RequestType == TelemetryRequestTypes.AppIntegrationsChanged)
                .OrderByDescending(x => x.SeqId)
                .Select(
                    data =>
            {
                var integrations = data.Payload is AppStartedPayload payload
                                                   ? payload.Integrations
                                                   : ((AppIntegrationsChangedPayload)data.Payload).Integrations;
                return(data, integrations);
            })
                .FirstOrDefault(x => x.integrations is not null);

            latestIntegrationsData.Should().NotBeNull();
            integrationsPayload.Should().NotBeNull();

            var integration = integrationsPayload
                              .FirstOrDefault(x => x.Name == integrationId.ToString());

            integration.Should().NotBeNull();
            integration.Enabled.Should().Be(enabled, $"{integration.Name} should only be enabled if we generate a span");
            if (autoEnabled.HasValue)
            {
                integration.AutoEnabled.Should().Be(autoEnabled.Value, $"{integration.Name} should only be auto-enabled if available");
            }

            integration.Error.Should().BeNullOrEmpty();

            return(latestIntegrationsData);
        }
        public async Task CanSendTelemetry()
        {
            using var agent = new MockTelemetryAgent <TelemetryData>(TcpPortProvider.GetOpenPort());
            var telemetryUri = new Uri($"http://localhost:{agent.Port}");

            // Uses framework specific transport
            var transport = new TelemetryTransportFactory(telemetryUri, apiKey: null).Create();
            var data      = GetSampleData();

            var result = await transport.PushTelemetry(data);

            result.Should().Be(TelemetryPushResult.Success);
            var received = agent.WaitForLatestTelemetry(x => x.SeqId == data.SeqId);

            received.Should().NotBeNull();

            // check some basic values
            received.SeqId.Should().Be(data.SeqId);
            received.Application.Env.Should().Be(data.Application.Env);
            received.Application.ServiceName.Should().Be(data.Application.ServiceName);
        }
 public static TelemetryData AssertIntegrationDisabled(this MockTelemetryAgent <TelemetryData> telemetry, IntegrationId integrationId)
 {
     return(telemetry.AssertIntegration(integrationId, enabled: false, autoEnabled: true));
 }