Example #1
0
        public void ShouldUseTheValueByTheKeyProcessorForTelemetry()
        {
            var envMock = new Mock <IK8sEnvironment>();

            envMock.Setup(env => env.ContainerName).Returns("Hello.RoleName");
            envMock.Setup(env => env.ContainerID).Returns("Hello.Cid");

            var envFactoryMock = new Mock <IK8sEnvironmentFactory>();

            envFactoryMock.Setup(f => f.CreateAsync(It.IsAny <DateTime>())).ReturnsAsync(() => envMock.Object);

            Mock <ITelemetryKeyCache> keyCacheMock = new Mock <ITelemetryKeyCache>();

            keyCacheMock.Setup(c => c.GetProcessedKey(It.IsAny <string>())).Returns <string>(input => input.Replace('.', '_'));

            KubernetesTelemetryInitializer target = new KubernetesTelemetryInitializer(
                envFactoryMock.Object,
                GetOptions(TimeSpan.FromSeconds(1)),
                SDKVersionUtils.Instance,
                keyCacheMock.Object);
            ITelemetry telemetry = new TraceTelemetry();

            target.Initialize(telemetry);

            ISupportProperties telemetryWithProperties = telemetry as ISupportProperties;

            Assert.False(telemetryWithProperties.Properties.ContainsKey("Kubernetes.Container.ID"));
            Assert.True(telemetryWithProperties.Properties.ContainsKey("Kubernetes_Container_ID"));
        }
Example #2
0
        public void InitializeWithEmptyForOptionalPropertyDoesNotLogError()
        {
            var envMock = new Mock <IK8sEnvironment>();

            envMock.Setup(env => env.ContainerName).Returns("Hello RoleName");

            envMock.Setup(env => env.ContainerID).Returns("Cid");
            envMock.Setup(env => env.ContainerName).Returns("CName");
            envMock.Setup(env => env.PodID).Returns("Pid");
            envMock.Setup(env => env.PodName).Returns("PName");
            envMock.Setup(env => env.PodLabels).Returns("PLabels");
            // The following properties are optional.
            envMock.Setup(env => env.ReplicaSetUid).Returns <string>(null);
            envMock.Setup(env => env.ReplicaSetName).Returns <string>(null);
            envMock.Setup(env => env.DeploymentUid).Returns <string>(null);
            envMock.Setup(env => env.DeploymentName).Returns <string>(null);
            envMock.Setup(env => env.NodeUid).Returns("Nid");
            envMock.Setup(env => env.NodeName).Returns("NName");

            var envFactoryMock = new Mock <IK8sEnvironmentFactory>();

            envFactoryMock.Setup(f => f.CreateAsync(It.IsAny <DateTime>())).ReturnsAsync(() => envMock.Object);

            var loggerMock = new Mock <ILogger <KubernetesTelemetryInitializer> >();

            KubernetesTelemetryInitializer target = new KubernetesTelemetryInitializer(envFactoryMock.Object, TimeSpan.FromSeconds(1), SDKVersionUtils.Instance, loggerMock.Object);
            ITelemetry telemetry = new TraceTelemetry();

            target.Initialize(telemetry);
            loggerMock.Verify(l => l.Log(LogLevel.Error, 0, It.IsAny <FormattedLogValues>(), It.IsAny <Exception>(), It.IsAny <Func <object, Exception, string> >()), Times.Never());
        }
Example #3
0
        public void InitializeWillNotOverwriteExistingCustomDimension()
        {
            var envMock = new Mock <IK8sEnvironment>();

            envMock.Setup(env => env.ContainerName).Returns("Hello RoleName");
            envMock.Setup(env => env.ContainerID).Returns("Cid");

            var envFactoryMock = new Mock <IK8sEnvironmentFactory>();

            envFactoryMock.Setup(f => f.CreateAsync(It.IsAny <DateTime>())).ReturnsAsync(() => envMock.Object);

            Mock <ITelemetryKeyCache> keyCacheMock = new Mock <ITelemetryKeyCache>();

            keyCacheMock.Setup(c => c.GetProcessedKey(It.IsAny <string>())).Returns <string>(input => input);

            KubernetesTelemetryInitializer target = new KubernetesTelemetryInitializer(
                envFactoryMock.Object,
                GetOptions(TimeSpan.FromSeconds(1)),
                SDKVersionUtils.Instance,
                keyCacheMock.Object);
            ITelemetry         telemetry = new TraceTelemetry();
            ISupportProperties telemetryWithProperties = telemetry as ISupportProperties;

            telemetryWithProperties.Properties["K8s.Container.ID"] = "Existing Cid";
            target.Initialize(telemetry);

            Assert.Equal("Existing Cid", telemetryWithProperties.Properties["K8s.Container.ID"]);
        }
Example #4
0
        public void TimeoutGettingK8sEnvNoException()
        {
            var envMock = new Mock <IK8sEnvironment>();

            envMock.Setup(env => env.ContainerID).Returns("Cid");
            var envFactoryMock = new Mock <IK8sEnvironmentFactory>();

            envFactoryMock.Setup(f => f.CreateAsync(It.IsAny <DateTime>())).ReturnsAsync(envMock.Object, TimeSpan.FromMinutes(1));
            Mock <ITelemetryKeyCache> keyCacheMock = new Mock <ITelemetryKeyCache>();

            keyCacheMock.Setup(c => c.GetProcessedKey(It.IsAny <string>())).Returns <string>(input => input);

            KubernetesTelemetryInitializer target = new KubernetesTelemetryInitializer(
                envFactoryMock.Object,
                GetOptions(TimeSpan.FromSeconds(1)),
                SDKVersionUtils.Instance,
                keyCacheMock.Object);

            ITelemetry         telemetry = new TraceTelemetry();
            ISupportProperties telemetryWithProperties = telemetry as ISupportProperties;

            telemetryWithProperties.Properties["K8s.Container.ID"] = "No Crash";
            target.Initialize(telemetry);

            Assert.Equal("No Crash", telemetryWithProperties.Properties["K8s.Container.ID"]);
        }
Example #5
0
        public void InitializeSetsCustomDimensions()
        {
            var envMock = new Mock <IK8sEnvironment>();

            envMock.Setup(env => env.ContainerName).Returns("Hello RoleName");

            envMock.Setup(env => env.ContainerID).Returns("Cid");
            envMock.Setup(env => env.ContainerName).Returns("CName");
            envMock.Setup(env => env.PodID).Returns("Pid");
            envMock.Setup(env => env.PodName).Returns("PName");
            envMock.Setup(env => env.PodLabels).Returns("PLabels");
            envMock.Setup(env => env.ReplicaSetUid).Returns("Rid");
            envMock.Setup(env => env.ReplicaSetName).Returns("RName");
            envMock.Setup(env => env.DeploymentUid).Returns("Did");
            envMock.Setup(env => env.DeploymentName).Returns("DName");
            envMock.Setup(env => env.NodeUid).Returns("Nid");
            envMock.Setup(env => env.NodeName).Returns("NName");
            envMock.Setup(env => env.PodNamespace).Returns("PNS");


            var envFactoryMock = new Mock <IK8sEnvironmentFactory>();

            envFactoryMock.Setup(f => f.CreateAsync(It.IsAny <DateTime>())).ReturnsAsync(() => envMock.Object);

            Mock <ITelemetryKeyCache> keyCacheMock = new Mock <ITelemetryKeyCache>();

            keyCacheMock.Setup(c => c.GetProcessedKey(It.IsAny <string>())).Returns <string>(input => input);

            KubernetesTelemetryInitializer target = new KubernetesTelemetryInitializer(
                envFactoryMock.Object,
                GetOptions(TimeSpan.FromSeconds(1)),
                SDKVersionUtils.Instance,
                keyCacheMock.Object);
            ITelemetry telemetry = new TraceTelemetry();

            target.Initialize(telemetry);

            ISupportProperties telemetryWithProperties = telemetry as ISupportProperties;

            Assert.Equal("Cid", telemetryWithProperties.Properties["Kubernetes.Container.ID"]);
            Assert.Equal("CName", telemetryWithProperties.Properties["Kubernetes.Container.Name"]);

            Assert.Equal("Pid", telemetryWithProperties.Properties["Kubernetes.Pod.ID"]);
            Assert.Equal("PName", telemetryWithProperties.Properties["Kubernetes.Pod.Name"]);
            Assert.Equal("PLabels", telemetryWithProperties.Properties["Kubernetes.Pod.Labels"]);
            Assert.Equal("PNS", telemetryWithProperties.Properties["Kubernetes.Pod.Namespace"]);

            Assert.Equal("RName", telemetryWithProperties.Properties["Kubernetes.ReplicaSet.Name"]);

            Assert.Equal("DName", telemetryWithProperties.Properties["Kubernetes.Deployment.Name"]);

            Assert.Equal("Nid", telemetryWithProperties.Properties["Kubernetes.Node.ID"]);
            Assert.Equal("NName", telemetryWithProperties.Properties["Kubernetes.Node.Name"]);
        }
Example #6
0
        public void InitializeSetsRoleName()
        {
            var envMock = new Mock <IK8sEnvironment>();

            envMock.Setup(env => env.ContainerName).Returns("Hello RoleName");
            KubernetesTelemetryInitializer target = new KubernetesTelemetryInitializer(null, envMock.Object);
            ITelemetry telemetry = new TraceTelemetry();

            target.Initialize(telemetry);

            Assert.Equal("Hello RoleName", telemetry.Context.Cloud.RoleName);
        }
        public void InitializeShouldNotOverwriteExistingRoleName()
        {
            var envMock = new Mock <IK8sEnvironment>();

            envMock.Setup(env => env.ContainerName).Returns("New RoleName");
            KubernetesTelemetryInitializer target = new KubernetesTelemetryInitializer(envMock.Object, GetLogger());
            ITelemetry telemetry = new TraceTelemetry();

            telemetry.Context.Cloud.RoleName = "Existing RoleName";
            target.Initialize(telemetry);

            Assert.Equal("Existing RoleName", telemetry.Context.Cloud.RoleName);
        }
        public void InitializeSetsCustomDimensions()
        {
            var envMock = new Mock <IK8sEnvironment>();

            envMock.Setup(env => env.ContainerName).Returns("Hello RoleName");

            envMock.Setup(env => env.ContainerID).Returns("Cid");
            envMock.Setup(env => env.ContainerName).Returns("CName");
            envMock.Setup(env => env.PodID).Returns("Pid");
            envMock.Setup(env => env.PodName).Returns("PName");
            envMock.Setup(env => env.PodLabels).Returns("PLabels");
            envMock.Setup(env => env.ReplicaSetUid).Returns("Rid");
            envMock.Setup(env => env.ReplicaSetName).Returns("RName");
            envMock.Setup(env => env.DeploymentUid).Returns("Did");
            envMock.Setup(env => env.DeploymentName).Returns("DName");
            envMock.Setup(env => env.NodeUid).Returns("Nid");
            envMock.Setup(env => env.NodeName).Returns("NName");

            var envFactoryMock = new Mock <IK8sEnvironmentFactory>();

            envFactoryMock.Setup(f => f.CreateAsync(It.IsAny <DateTime>())).ReturnsAsync(() => envMock.Object);

            KubernetesTelemetryInitializer target = new KubernetesTelemetryInitializer(
                envFactoryMock.Object,
                GetOptions(TimeSpan.FromSeconds(1)),
                SDKVersionUtils.Instance);
            ITelemetry telemetry = new TraceTelemetry();

            target.Initialize(telemetry);

            ISupportProperties telemetryWithProperties = telemetry as ISupportProperties;

            Assert.Equal("Cid", telemetryWithProperties.Properties["Kubernetes.Container.ID"]);
            Assert.Equal("CName", telemetryWithProperties.Properties["Kubernetes.Container.Name"]);

            Assert.Equal("Pid", telemetryWithProperties.Properties["Kubernetes.Pod.ID"]);
            Assert.Equal("PName", telemetryWithProperties.Properties["Kubernetes.Pod.Name"]);
            Assert.Equal("PLabels", telemetryWithProperties.Properties["Kubernetes.Pod.Labels"]);

            Assert.Equal("RName", telemetryWithProperties.Properties["Kubernetes.ReplicaSet.Name"]);

            Assert.Equal("DName", telemetryWithProperties.Properties["Kubernetes.Deployment.Name"]);

            Assert.Equal("Nid", telemetryWithProperties.Properties["Kubernetes.Node.ID"]);
            Assert.Equal("NName", telemetryWithProperties.Properties["Kubernetes.Node.Name"]);

#if !NETSTANDARD1_3 && !NETSTANDARD1_6
            Assert.NotNull(telemetryWithProperties.Properties["Process.CPU(%)"]);
            Assert.NotNull(telemetryWithProperties.Properties["Process.Memory"]);
#endif
        }
Example #9
0
        public void InitializeSetsRoleName()
        {
            var envMock = new Mock <IK8sEnvironment>();

            envMock.Setup(env => env.ContainerName).Returns("Hello RoleName");
            var envFactoryMock = new Mock <IK8sEnvironmentFactory>();

            envFactoryMock.Setup(f => f.CreateAsync(It.IsAny <DateTime>())).ReturnsAsync(() => envMock.Object);

            KubernetesTelemetryInitializer target = new KubernetesTelemetryInitializer(envFactoryMock.Object, TimeSpan.FromSeconds(1), SDKVersionUtils.Instance, GetLogger());
            ITelemetry telemetry = new TraceTelemetry();

            target.Initialize(telemetry);

            Assert.Equal("Hello RoleName", telemetry.Context.Cloud.RoleName);
        }
Example #10
0
        public void InitializeWillNotOverwriteExistingCustomDimension()
        {
            var envMock = new Mock <IK8sEnvironment>();

            envMock.Setup(env => env.ContainerName).Returns("Hello RoleName");

            envMock.Setup(env => env.ContainerID).Returns("Cid");

            KubernetesTelemetryInitializer target = new KubernetesTelemetryInitializer(null, envMock.Object);
            ITelemetry telemetry = new TraceTelemetry();

            telemetry.Context.Properties["K8s.Container.ID"] = "Existing Cid";
            target.Initialize(telemetry);

            Assert.Equal("Existing Cid", telemetry.Context.Properties["K8s.Container.ID"]);
        }
Example #11
0
        public void TimeoutGettingK8sEnvNoException()
        {
            var envMock = new Mock <IK8sEnvironment>();

            envMock.Setup(env => env.ContainerID).Returns("Cid");
            var envFactoryMock = new Mock <IK8sEnvironmentFactory>();

            envFactoryMock.Setup(f => f.CreateAsync(It.IsAny <DateTime>())).ReturnsAsync(envMock.Object, TimeSpan.FromMinutes(1));
            KubernetesTelemetryInitializer target = new KubernetesTelemetryInitializer(envFactoryMock.Object, TimeSpan.FromSeconds(1), SDKVersionUtils.Instance, GetLogger());

            ITelemetry telemetry = new TraceTelemetry();

            telemetry.Context.Properties["K8s.Container.ID"] = "No Crash";
            target.Initialize(telemetry);

            Assert.Equal("No Crash", telemetry.Context.Properties["K8s.Container.ID"]);
        }
Example #12
0
        public void InitializeWithEmptyForOptionalPropertyDoesNotLogError()
        {
            var listener = new TestDiagnosticSourceObserver();

            ApplicationInsightsKubernetesDiagnosticSource.Instance.Observable.SubscribeWithAdapter(listener);

            var envMock = new Mock <IK8sEnvironment>();

            envMock.Setup(env => env.ContainerName).Returns("Hello RoleName");

            envMock.Setup(env => env.ContainerID).Returns("Cid");
            envMock.Setup(env => env.ContainerName).Returns("CName");
            envMock.Setup(env => env.PodID).Returns("Pid");
            envMock.Setup(env => env.PodName).Returns("PName");
            envMock.Setup(env => env.PodLabels).Returns("PLabels");

            // The following properties are optional.
            envMock.Setup(env => env.ReplicaSetUid).Returns <string>(null);
            envMock.Setup(env => env.ReplicaSetName).Returns <string>(null);
            envMock.Setup(env => env.DeploymentUid).Returns <string>(null);
            envMock.Setup(env => env.DeploymentName).Returns <string>(null);
            envMock.Setup(env => env.PodNamespace).Returns <string>(null);
            envMock.Setup(env => env.NodeUid).Returns("Nid");
            envMock.Setup(env => env.NodeName).Returns("NName");

            var envFactoryMock = new Mock <IK8sEnvironmentFactory>();

            envFactoryMock.Setup(f => f.CreateAsync(It.IsAny <DateTime>())).ReturnsAsync(() => envMock.Object);

            Mock <ITelemetryKeyCache> keyCacheMock = new Mock <ITelemetryKeyCache>();

            keyCacheMock.Setup(c => c.GetProcessedKey(It.IsAny <string>())).Returns <string>(input => input);

            KubernetesTelemetryInitializer target = new KubernetesTelemetryInitializer(
                envFactoryMock.Object,
                GetOptions(TimeSpan.FromSeconds(1)),
                SDKVersionUtils.Instance,
                keyCacheMock.Object);
            ITelemetry telemetry = new TraceTelemetry();

            target.Initialize(telemetry);

            Assert.Equal(0, listener.GetCount(DiagnosticLogLevel.Error));
        }
Example #13
0
        public void InitializeWillNotOverwriteExistingCustomDimension()
        {
            var envMock = new Mock <IK8sEnvironment>();

            envMock.Setup(env => env.ContainerName).Returns("Hello RoleName");
            envMock.Setup(env => env.ContainerID).Returns("Cid");

            var envFactoryMock = new Mock <IK8sEnvironmentFactory>();

            envFactoryMock.Setup(f => f.CreateAsync(It.IsAny <DateTime>())).ReturnsAsync(() => envMock.Object);

            KubernetesTelemetryInitializer target = new KubernetesTelemetryInitializer(envFactoryMock.Object, TimeSpan.FromSeconds(1), SDKVersionUtils.Instance, GetLogger());
            ITelemetry telemetry = new TraceTelemetry();

            telemetry.Context.Properties["K8s.Container.ID"] = "Existing Cid";
            target.Initialize(telemetry);

            Assert.Equal("Existing Cid", telemetry.Context.Properties["K8s.Container.ID"]);
        }
        public void InitializeSetsCustomDimensions()
        {
            var envMock = new Mock <IK8sEnvironment>();

            envMock.Setup(env => env.ContainerName).Returns("Hello RoleName");

            envMock.Setup(env => env.ContainerID).Returns("Cid");
            envMock.Setup(env => env.ContainerName).Returns("CName");
            envMock.Setup(env => env.PodID).Returns("Pid");
            envMock.Setup(env => env.PodName).Returns("PName");
            envMock.Setup(env => env.PodLabels).Returns("PLabels");
            envMock.Setup(env => env.ReplicaSetUid).Returns("Rid");
            envMock.Setup(env => env.ReplicaSetName).Returns("RName");
            envMock.Setup(env => env.DeploymentUid).Returns("Did");
            envMock.Setup(env => env.DeploymentName).Returns("DName");
            envMock.Setup(env => env.NodeUid).Returns("Nid");
            envMock.Setup(env => env.NodeName).Returns("NName");

            KubernetesTelemetryInitializer target = new KubernetesTelemetryInitializer(envMock.Object, GetLogger());
            ITelemetry telemetry = new TraceTelemetry();

            target.Initialize(telemetry);

            Assert.Equal("Cid", telemetry.Context.Properties["Kubernetes.Container.ID"]);
            Assert.Equal("CName", telemetry.Context.Properties["Kubernetes.Container.Name"]);

            Assert.Equal("Pid", telemetry.Context.Properties["Kubernetes.Pod.ID"]);
            Assert.Equal("PName", telemetry.Context.Properties["Kubernetes.Pod.Name"]);
            Assert.Equal("PLabels", telemetry.Context.Properties["Kubernetes.Pod.Labels"]);

            Assert.Equal("RName", telemetry.Context.Properties["Kubernetes.ReplicaSet.Name"]);

            Assert.Equal("DName", telemetry.Context.Properties["Kubernetes.Deployment.Name"]);

            Assert.Equal("Nid", telemetry.Context.Properties["Kubernetes.Node.ID"]);
            Assert.Equal("NName", telemetry.Context.Properties["Kubernetes.Node.Name"]);

#if !NETSTANDARD1_3 && !NETSTANDARD1_6
            Assert.NotNull(telemetry.Context.Properties["Process.CPU(%)"]);
            Assert.NotNull(telemetry.Context.Properties["Process.Memory"]);
#endif
        }
Example #15
0
        public void InitializeSetsRoleName()
        {
            var envMock = new Mock <IK8sEnvironment>();

            envMock.Setup(env => env.ContainerName).Returns("Hello RoleName");
            var envFactoryMock = new Mock <IK8sEnvironmentFactory>();

            envFactoryMock.Setup(f => f.CreateAsync(It.IsAny <DateTime>())).ReturnsAsync(() => envMock.Object);
            Mock <ITelemetryKeyCache> keyCacheMock = new Mock <ITelemetryKeyCache>();

            keyCacheMock.Setup(c => c.GetProcessedKey(It.IsAny <string>())).Returns <string>(input => input);

            KubernetesTelemetryInitializer target = new KubernetesTelemetryInitializer(envFactoryMock.Object,
                                                                                       GetOptions(TimeSpan.FromSeconds(1)),
                                                                                       SDKVersionUtils.Instance,
                                                                                       keyCacheMock.Object);
            ITelemetry telemetry = new TraceTelemetry();

            target.Initialize(telemetry);

            Assert.Equal("Hello RoleName", telemetry.Context.Cloud.RoleName);
        }