From cfd9b9afb3a8d14f0023b3060a7e384be2ff57cf Mon Sep 17 00:00:00 2001 From: Jimmy Byrd Date: Sun, 5 May 2024 18:52:31 -0400 Subject: [PATCH] Adds basic OTel Metric support to fsautocomplete --- paket.dependencies | 1 + paket.lock | 2 ++ src/FsAutoComplete/Parser.fs | 25 ++++++++++++++++++++----- src/FsAutoComplete/paket.references | 1 + 4 files changed, 24 insertions(+), 5 deletions(-) diff --git a/paket.dependencies b/paket.dependencies index 31cbcdaf1..ec3b96670 100644 --- a/paket.dependencies +++ b/paket.dependencies @@ -56,6 +56,7 @@ nuget Ionide.LanguageServerProtocol >= 0.4.23 nuget Microsoft.Extensions.Caching.Memory nuget OpenTelemetry.Api >= 1.3.2 nuget OpenTelemetry.Exporter.OpenTelemetryProtocol >= 1.3.2 # 1.4 bumps to 7.0 versions of System.Diagnostics libs, so can't use it +nuget OpenTelemetry.Instrumentation.Runtime nuget LinkDotNet.StringBuilder 1.18.0 nuget CommunityToolkit.HighPerformance nuget System.Security.Cryptography.Pkcs 6.0.4 diff --git a/paket.lock b/paket.lock index 19c5cc39f..0f8ccb7af 100644 --- a/paket.lock +++ b/paket.lock @@ -378,6 +378,8 @@ NUGET Grpc (>= 2.44 < 3.0) - restriction: || (&& (== net6.0) (>= net462)) (&& (== net6.0) (< netstandard2.1)) (&& (== net7.0) (>= net462)) (&& (== net7.0) (< netstandard2.1)) (&& (== net8.0) (>= net462)) (&& (== net8.0) (< netstandard2.1)) (== netstandard2.0) (&& (== netstandard2.1) (>= net462)) Grpc.Net.Client (>= 2.43 < 3.0) - restriction: || (== net6.0) (== net7.0) (== net8.0) (&& (== netstandard2.0) (>= netstandard2.1)) (== netstandard2.1) OpenTelemetry (>= 1.3.2) + OpenTelemetry.Instrumentation.Runtime (1.0) + OpenTelemetry.Api (>= 1.3 < 2.0) Perfolizer (0.2.1) System.Memory (>= 4.5.3) runtime.debian.8-x64.runtime.native.System.Security.Cryptography.OpenSsl (4.3.2) diff --git a/src/FsAutoComplete/Parser.fs b/src/FsAutoComplete/Parser.fs index 5ecee6b4f..1c85a7240 100644 --- a/src/FsAutoComplete/Parser.fs +++ b/src/FsAutoComplete/Parser.fs @@ -14,12 +14,14 @@ open FsAutoComplete.Lsp open OpenTelemetry open OpenTelemetry.Resources open OpenTelemetry.Trace +open OpenTelemetry.Metrics module Parser = open FsAutoComplete.Core open System.Diagnostics let mutable tracerProvider = Unchecked.defaultof<_> + let mutable meterProvider = Unchecked.defaultof<_> [] type Pos = { Line: int; Column: int } @@ -194,19 +196,32 @@ module Parser = let configureOTel = Invocation.InvocationMiddleware(fun ctx next -> + + if ctx.ParseResult.GetValueForOption otelTracingOption then + let serviceName = FsAutoComplete.Utils.Tracing.serviceName let version = FsAutoComplete.Utils.Version.info().Version + let resourceBuilder = + ResourceBuilder + .CreateDefault() + .AddService(serviceName = serviceName, serviceVersion = version) + + + meterProvider <- + Sdk + .CreateMeterProviderBuilder() + .AddMeter() + .AddRuntimeInstrumentation() + .SetResourceBuilder(resourceBuilder) + .AddOtlpExporter() + .Build() tracerProvider <- Sdk .CreateTracerProviderBuilder() .AddSource(serviceName, Tracing.fscServiceName) - .SetResourceBuilder( - ResourceBuilder - .CreateDefault() - .AddService(serviceName = serviceName, serviceVersion = version) - ) + .SetResourceBuilder(resourceBuilder) .AddOtlpExporter() .Build() diff --git a/src/FsAutoComplete/paket.references b/src/FsAutoComplete/paket.references index 831027c3d..87af7001a 100644 --- a/src/FsAutoComplete/paket.references +++ b/src/FsAutoComplete/paket.references @@ -18,6 +18,7 @@ System.CommandLine FSharp.Data.Adaptive Microsoft.Extensions.Caching.Memory OpenTelemetry.Exporter.OpenTelemetryProtocol +OpenTelemetry.Instrumentation.Runtime Microsoft.CodeAnalysis LinkDotNet.StringBuilder CommunityToolkit.HighPerformance