예제 #1
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddSerilog();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseSerilogRequestLogging();
            app.UseRouting();

            // Configures request pipeline to collect Prometheus metrics
            app.UseHttpMetrics();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapGrpcService <UserHandlerV1>();

                // Starts a Prometheus metrics exporter using endpoint routing. The default URL is /metrics,
                endpoints.MapMetrics();

                endpoints.MapGet("/", async context =>
                {
                    // Track test metrics
                    IMetricsRegistry metricsRegistry = app.ApplicationServices.GetRequiredService <IMetricsRegistry>();

                    using (metricsRegistry.HistogramGrpcCallsDuration())
                    {
                        metricsRegistry.CountGrpcCalls("GET /", "OK");

                        var random = new Random();
                        if (random.Next(0, 2) == 0)
                        {
                            metricsRegistry.CountFailedGrpcCalls("GET /");
                        }

                        var delayMilliseconds = random.Next(0, 2000);
                        await Task.Delay(delayMilliseconds);

                        await context.Response.WriteAsync(
                            "Users service is up and running!\n" +
                            "Communication with gRPC endpoints must be made through a gRPC client. To learn how to create a client, visit: https://go.microsoft.com/fwlink/?linkid=2086909");
                    }
                });
            });
        }