From 3ae4adb041b4b0ebe114be9ce60e76701e4335a5 Mon Sep 17 00:00:00 2001 From: sixwaaaay Date: Sat, 13 Jan 2024 16:05:52 +0800 Subject: [PATCH] feat: add open telemetry tracing, prometheus metric --- .github/chore/compose.content.yaml | 5 ++++ sharp/content/Program.cs | 37 +++++++++++++++++++++++++++++- sharp/content/content.csproj | 4 ++++ 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/.github/chore/compose.content.yaml b/.github/chore/compose.content.yaml index b10f0d13..0ab3622c 100644 --- a/.github/chore/compose.content.yaml +++ b/.github/chore/compose.content.yaml @@ -68,6 +68,11 @@ services: test: [ "CMD", "mysqladmin", "ping", "-h", "localhost" ] timeout: 20s retries: 10 + jaeger: + image: jaegertracing/all-in-one:1.52 + ports: + - 4317:4317 + - 16686:16686 volumes: mysql-data: diff --git a/sharp/content/Program.cs b/sharp/content/Program.cs index 5a27c016..13cd2ae9 100644 --- a/sharp/content/Program.cs +++ b/sharp/content/Program.cs @@ -9,8 +9,9 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. - * + * */ + using System.Text; using System.Text.Json; using System.Text.Json.Serialization; @@ -20,6 +21,10 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.IdentityModel.Tokens; using MySqlConnector; +using OpenTelemetry.Exporter; +using OpenTelemetry.Metrics; +using OpenTelemetry.Resources; +using OpenTelemetry.Trace; var builder = WebApplication.CreateSlimBuilder(args); @@ -41,6 +46,35 @@ } ); +const string serviceName = "sharing.content"; +var otelEndpoint = builder.Configuration.GetConnectionString("Otel_GrpcEndpoint"); +builder.Services.AddOpenTelemetry().WithTracing(tcb => +{ + tcb + .AddSource(serviceName) + .SetResourceBuilder(ResourceBuilder.CreateDefault().AddService(serviceName: serviceName)) + .AddAspNetCoreInstrumentation() + .AddOtlpExporter(o => + { + if (!string.IsNullOrEmpty(otelEndpoint)) + { + o.Endpoint = new Uri(otelEndpoint); + } + }); +}).WithMetrics(mtb => +{ + mtb + .AddMeter("Microsoft.AspNetCore.Hosting", "Microsoft.AspNetCore.Server.Kestrel") + .AddView("http.server.request.duration", + new ExplicitBucketHistogramConfiguration + { + Boundaries = [0, 0.005, 0.01, 0.025, 0.05, 0.075, 0.1, 0.25, 0.5, 0.75, 1, 2.5, 5, 7.5, 10] + }) + .AddPrometheusExporter(); +}); + +builder.Services.AddSingleton(TracerProvider.Default.GetTracer(serviceName)); + builder.Services.AddAuthorization().AddProbe(); builder.Services.AddProblemDetails().AddResponseCompression(); @@ -63,6 +97,7 @@ app.UseToken(); +app.MapPrometheusScrapingEndpoint(); app.MapEndpoints(); app.Run(); diff --git a/sharp/content/content.csproj b/sharp/content/content.csproj index 1f237608..ccd5d60e 100644 --- a/sharp/content/content.csproj +++ b/sharp/content/content.csproj @@ -35,6 +35,10 @@ + + + +