Example #1
0
        public void HandleStopEvent_RecordsStats()
        {
            var options  = new MetricsEndpointOptions();
            var stats    = new OpenCensusStats();
            var tags     = new OpenCensusTags();
            var observer = new HttpClientDesktopObserver(options, stats, tags, null);

            var req = GetHttpRequestMessage();

            Activity act = new Activity("Test");

            act.Start();
            Thread.Sleep(1000);
            act.SetEndTime(DateTime.UtcNow);

            observer.HandleStopEvent(act, req, HttpStatusCode.InternalServerError);
            observer.HandleStopEvent(act, req, HttpStatusCode.OK);

            var reqData  = stats.ViewManager.GetView(ViewName.Create("http.desktop.client.request.time"));
            var aggData1 = MetricsHelpers.SumWithTags(reqData) as IDistributionData;

            Assert.InRange(aggData1.Mean, 990.0, 1025.0);
            Assert.InRange(aggData1.Max, 990.0, 1025.0);

            reqData = stats.ViewManager.GetView(ViewName.Create("http.desktop.client.request.count"));
            var aggData2 = MetricsHelpers.SumWithTags(reqData) as ISumDataLong;

            Assert.Equal(2, aggData2.Sum);

            act.Stop();
        }
Example #2
0
        public void HandleThreadsEvent_RecordsValues()
        {
            var options  = new MetricsEndpointOptions();
            var stats    = new OpenCensusStats();
            var tags     = new OpenCensusTags();
            var observer = new CLRRuntimeObserver(options, stats, tags, null);

            CLRRuntimeSource.ThreadMetrics metrics = new CLRRuntimeSource.ThreadMetrics(100, 100, 200, 200);
            observer.HandleThreadsEvent(metrics);

            var live    = stats.ViewManager.GetView(ViewName.Create("clr.threadpool.active"));
            var aggData = MetricsHelpers.SumWithTags(live) as IMeanData;

            Assert.Equal(100, aggData.Mean);
            Assert.Equal(100, aggData.Min);
            Assert.Equal(100, aggData.Max);

            aggData = MetricsHelpers.SumWithTags(live, new List <ITagValue>()
            {
                TagValue.Create("worker")
            }) as IMeanData;
            Assert.Equal(100, aggData.Mean);
            Assert.Equal(100, aggData.Min);
            Assert.Equal(100, aggData.Max);

            aggData = MetricsHelpers.SumWithTags(live, new List <ITagValue>()
            {
                TagValue.Create("completionPort")
            }) as IMeanData;
            Assert.Equal(100, aggData.Mean);
            Assert.Equal(100, aggData.Min);
            Assert.Equal(100, aggData.Max);

            var avail = stats.ViewManager.GetView(ViewName.Create("clr.threadpool.avail"));

            aggData = MetricsHelpers.SumWithTags(avail) as IMeanData;
            Assert.Equal(100, aggData.Mean);
            Assert.Equal(100, aggData.Min);
            Assert.Equal(100, aggData.Max);

            aggData = MetricsHelpers.SumWithTags(avail, new List <ITagValue>()
            {
                TagValue.Create("worker")
            }) as IMeanData;
            Assert.Equal(100, aggData.Mean);
            Assert.Equal(100, aggData.Min);
            Assert.Equal(100, aggData.Max);

            aggData = MetricsHelpers.SumWithTags(avail, new List <ITagValue>()
            {
                TagValue.Create("completionPort")
            }) as IMeanData;
            Assert.Equal(100, aggData.Mean);
            Assert.Equal(100, aggData.Min);
            Assert.Equal(100, aggData.Max);
        }
Example #3
0
        /// <summary>
        /// Configure Services.
        /// </summary>
        /// <param name="services"></param>
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddOptions();
            services.Configure <ApplicationOptions>(Configuration);
            services.Configure <Common.Configuration.ApplicationOptions>(Configuration);
            services.AddSingleton(Configuration);
            services.AddMemoryCache();
            services.AddSingleton <CronJobServiceHealthCheck>();

            var config = Configuration.Get <ApplicationOptions>();

            services.DisplayConfiguration(Configuration, HostingEnvironment);

            var commonConfig = Configuration.Get <MicroService.Common.Configuration.ApplicationOptions>();

            services.AddCustomApiVersioning();

            services.AddOpenTelemetryTracing(
                builder =>
            {
                _ = builder
                    .SetResourceBuilder(ResourceBuilder.CreateDefault()
                                        .AddService(HostingEnvironment.ApplicationName))
                    .AddSource(nameof(FeatureServiceController))
                    .AddAspNetCoreInstrumentation()
                    .AddHttpClientInstrumentation();

                if (commonConfig.JaegerConfiguration.Enabled)
                {
                    builder.AddJaegerExporter(o =>
                    {
                        o.AgentHost = commonConfig.JaegerConfiguration.Host;
                        o.AgentPort = commonConfig.JaegerConfiguration.Port;
                    });
                }

                if (HostingEnvironment.IsDevelopment())
                {
                    builder.AddConsoleExporter(options => options.Targets = ConsoleExporterOutputTargets.Console);
                }
            });

            var metrics = AppMetrics.CreateDefaultBuilder()
                          .OutputMetrics
                          .AsPrometheusPlainText()
                          .Build();

            MetricsHelpers.SetMetricsCustomTag(metrics, "OSDescription", System.Runtime.InteropServices.RuntimeInformation.OSDescription);
            MetricsHelpers.SetMetricsCustomTag(metrics, "instance", Dns.GetHostName());

            metrics.Options.ReportingEnabled = true;
            services.AddMetrics(metrics);
            services.AddAppMetricsHealthPublishing();
            services.AddAppMetricsCollectors();

            services.AddTransient <IConfigureOptions <SwaggerGenOptions>, ConfigureSwaggerOptions>();
            services.AddSwaggerConfiguration();
            services.AddCorsConfiguration();

            // Repositories
            services.AddScoped <ITestDataRepository>(x => new TestDataRepository(config.ConnectionStrings.PostgreSql));

            // Services
            services.AddScoped <ICalculationService, CalculationService>();

            services.AddScoped <ShapefileDataReaderResolver>(serviceProvider => key =>
            {
                ShapeAttributes shapeProperties = null;
                if (key == nameof(ShapeProperties.BoroughBoundaries))
                {
                    shapeProperties = ShapeProperties.BoroughBoundaries.GetAttribute <ShapeAttributes>();
                }
                else if (key == nameof(ShapeProperties.CommunityDistricts))
                {
                    shapeProperties = ShapeProperties.CommunityDistricts.GetAttribute <ShapeAttributes>();
                }
                else if (key == nameof(ShapeProperties.DSNYDistricts))
                {
                    shapeProperties = ShapeProperties.DSNYDistricts.GetAttribute <ShapeAttributes>();
                }
                else if (key == nameof(ShapeProperties.HistoricDistricts))
                {
                    shapeProperties = ShapeProperties.HistoricDistricts.GetAttribute <ShapeAttributes>();
                }
                else if (key == nameof(ShapeProperties.Neighborhoods))
                {
                    shapeProperties = ShapeProperties.Neighborhoods.GetAttribute <ShapeAttributes>();
                }
                else if (key == nameof(ShapeProperties.NeighborhoodTabulationAreas))
                {
                    shapeProperties = ShapeProperties.NeighborhoodTabulationAreas.GetAttribute <ShapeAttributes>();
                }
                else if (key == nameof(ShapeProperties.NypdPolicePrecincts))
                {
                    shapeProperties = ShapeProperties.NypdPolicePrecincts.GetAttribute <ShapeAttributes>();
                }
                else if (key == nameof(ShapeProperties.NypdSectors))
                {
                    shapeProperties = ShapeProperties.NypdSectors.GetAttribute <ShapeAttributes>();
                }
                else if (key == nameof(ShapeProperties.NychaDevelopments))
                {
                    shapeProperties = ShapeProperties.NychaDevelopments.GetAttribute <ShapeAttributes>();
                }
                else if (key == nameof(ShapeProperties.Parks))
                {
                    shapeProperties = ShapeProperties.Parks.GetAttribute <ShapeAttributes>();
                }
                else if (key == nameof(ShapeProperties.Subway))
                {
                    shapeProperties = ShapeProperties.Subway.GetAttribute <ShapeAttributes>();
                }
                else if (key == nameof(ShapeProperties.ZipCodes))
                {
                    shapeProperties = ShapeProperties.ZipCodes.GetAttribute <ShapeAttributes>();
                }
                else
                {
                    throw new KeyNotFoundException(key);
                }

                var options = serviceProvider.GetService <IOptions <ApplicationOptions> >();
                var cache   = serviceProvider.GetService <IMemoryCache>();

                var rootDirectory        = options?.Value?.ShapeConfiguration.ShapeSystemRootDirectory;
                string shapeFileNamePath = Path.Combine(rootDirectory, shapeProperties.Directory, shapeProperties.FileName);

                return(new CachedShapefileDataReader(cache, key, shapeFileNamePath));
            });

            // Feature Service Lookups
            services.AddScoped <BoroughBoundariesService>();
            services.AddScoped <CommunityDistrictsService>();
            services.AddScoped <DSNYDistrictsService>();
            services.AddScoped <HistoricDistrictService>();
            services.AddScoped <NeighborhoodsService <NeighborhoodShape> >();
            services.AddScoped <NeighborhoodTabulationAreasService>();
            services.AddScoped <NypdPolicePrecinctService>();
            services.AddScoped <NypdSectorsService <NypdSectorShape> >();
            services.AddScoped <NychaDevelopmentService <NychaDevelopmentShape> >();
            services.AddScoped <ParkService <ParkShape> >();
            services.AddScoped <SubwayService <SubwayShape> >();
            services.AddScoped <ZipCodeService <ZipCodeShape> >();

            services.AddScoped <ShapeServiceResolver>(serviceProvider => key =>
            {
                return(key switch
                {
                    nameof(ShapeProperties.BoroughBoundaries) => serviceProvider.GetService <BoroughBoundariesService>(),
                    nameof(ShapeProperties.CommunityDistricts) => serviceProvider.GetService <CommunityDistrictsService>(),
                    nameof(ShapeProperties.DSNYDistricts) => serviceProvider.GetService <DSNYDistrictsService>(),
                    nameof(ShapeProperties.HistoricDistricts) => serviceProvider.GetService <HistoricDistrictService>(),
                    nameof(ShapeProperties.Neighborhoods) => serviceProvider.GetService <NeighborhoodsService <NeighborhoodShape> >(),
                    nameof(ShapeProperties.NeighborhoodTabulationAreas) => serviceProvider.GetService <NeighborhoodTabulationAreasService>(),
                    nameof(ShapeProperties.NypdPolicePrecincts) => serviceProvider.GetService <NypdPolicePrecinctService>(),
                    nameof(ShapeProperties.NypdSectors) => serviceProvider.GetService <NypdSectorsService <NypdSectorShape> >(),
                    nameof(ShapeProperties.NychaDevelopments) => serviceProvider.GetService <NychaDevelopmentService <NychaDevelopmentShape> >(),
                    nameof(ShapeProperties.Parks) => serviceProvider.GetService <ParkService <ParkShape> >(),
                    nameof(ShapeProperties.Subway) => serviceProvider.GetService <SubwayService <SubwayShape> >(),
                    nameof(ShapeProperties.ZipCodes) => serviceProvider.GetService <ZipCodeService <ZipCodeShape> >(),
                    _ => throw new KeyNotFoundException(key)
                });
            });
Example #4
0
        public void HandleHeapEvent_RecordsValues()
        {
            var options  = new MetricsEndpointOptions();
            var stats    = new OpenCensusStats();
            var tags     = new OpenCensusTags();
            var observer = new CLRRuntimeObserver(options, stats, tags, null);

            CLRRuntimeSource.HeapMetrics metrics = new CLRRuntimeSource.HeapMetrics(1000, new List <long>()
            {
                10, 20, 30
            });
            observer.HandleHeapEvent(metrics);

            var memUsedViewData = stats.ViewManager.GetView(ViewName.Create("clr.memory.used"));
            var aggData         = MetricsHelpers.SumWithTags(memUsedViewData) as IMeanData;

            Assert.Equal(1000, aggData.Mean);
            Assert.Equal(1000, aggData.Max);
            Assert.Equal(1000, aggData.Min);

            var gcViewData = stats.ViewManager.GetView(ViewName.Create("clr.gc.collections"));
            var aggData2   = MetricsHelpers.SumWithTags(gcViewData) as ISumDataLong;

            Assert.Equal(60, aggData2.Sum);

            aggData2 = MetricsHelpers.SumWithTags(gcViewData, new List <ITagValue>()
            {
                TagValue.Create("gen0")
            }) as ISumDataLong;
            Assert.Equal(10, aggData2.Sum);

            aggData2 = MetricsHelpers.SumWithTags(gcViewData, new List <ITagValue>()
            {
                TagValue.Create("gen1")
            }) as ISumDataLong;
            Assert.Equal(20, aggData2.Sum);

            aggData2 = MetricsHelpers.SumWithTags(gcViewData, new List <ITagValue>()
            {
                TagValue.Create("gen2")
            }) as ISumDataLong;
            Assert.Equal(30, aggData2.Sum);

            metrics = new CLRRuntimeSource.HeapMetrics(5000, new List <long>()
            {
                15, 25, 30
            });
            observer.HandleHeapEvent(metrics);

            memUsedViewData = stats.ViewManager.GetView(ViewName.Create("clr.memory.used"));
            aggData         = MetricsHelpers.SumWithTags(memUsedViewData) as IMeanData;
            Assert.Equal((5000 + 1000) / 2, aggData.Mean);
            Assert.Equal(5000, aggData.Max);
            Assert.Equal(1000, aggData.Min);

            gcViewData = stats.ViewManager.GetView(ViewName.Create("clr.gc.collections"));
            aggData2   = MetricsHelpers.SumWithTags(gcViewData) as ISumDataLong;
            Assert.Equal(70, aggData2.Sum);

            aggData2 = MetricsHelpers.SumWithTags(gcViewData, new List <ITagValue>()
            {
                TagValue.Create("gen0")
            }) as ISumDataLong;
            Assert.Equal(15, aggData2.Sum);

            aggData2 = MetricsHelpers.SumWithTags(gcViewData, new List <ITagValue>()
            {
                TagValue.Create("gen1")
            }) as ISumDataLong;
            Assert.Equal(25, aggData2.Sum);

            aggData2 = MetricsHelpers.SumWithTags(gcViewData, new List <ITagValue>()
            {
                TagValue.Create("gen2")
            }) as ISumDataLong;
            Assert.Equal(30, aggData2.Sum);
        }