Skip to content

Commit

Permalink
More OpenTelemetry changes (#18246)
Browse files Browse the repository at this point in the history
* trace test run activity

* declutter StackGuard.Guard traces

* add otel to debug vsix

* ref otel in debug only

* delay tags allocation

* fix opens

* format

* better naming
  • Loading branch information
majocha authored Feb 10, 2025
1 parent 7866d66 commit 5d0812f
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 15 deletions.
22 changes: 11 additions & 11 deletions src/Compiler/Facilities/DiagnosticsLogger.fs
Original file line number Diff line number Diff line change
Expand Up @@ -882,17 +882,17 @@ type StackGuard(maxDepth: int, name: string) =
[<CallerFilePath; Optional; DefaultParameterValue("")>] path: string,
[<CallerLineNumber; Optional; DefaultParameterValue(0)>] line: int
) =
use _ =
Activity.start
"DiagnosticsLogger.StackGuard.Guard"
[|
Activity.Tags.stackGuardName, name
Activity.Tags.stackGuardCurrentDepth, string depth
Activity.Tags.stackGuardMaxDepth, string maxDepth
Activity.Tags.callerMemberName, memberName
Activity.Tags.callerFilePath, path
Activity.Tags.callerLineNumber, string line
|]

Activity.addEventWithTags
"DiagnosticsLogger.StackGuard.Guard"
(seq {
Activity.Tags.stackGuardName, box name
Activity.Tags.stackGuardCurrentDepth, depth
Activity.Tags.stackGuardMaxDepth, maxDepth
Activity.Tags.callerMemberName, memberName
Activity.Tags.callerFilePath, path
Activity.Tags.callerLineNumber, line
})

depth <- depth + 1

Expand Down
10 changes: 8 additions & 2 deletions src/Compiler/Utilities/Activity.fs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ open System.Diagnostics
open System.IO
open System.Text
open Internal.Utilities.Library
open System.Collections.Generic


module ActivityNames =
Expand Down Expand Up @@ -102,12 +103,17 @@ module internal Activity =

let startNoTags (name: string) : IDisposable MaybeNull = activitySource.StartActivity name

let addEvent name =
let addEventWithTags name (tags: (string * objnull) seq) =
match Activity.Current with
| null -> ()
| activity when activity.Source = activitySource -> activity.AddEvent(ActivityEvent name) |> ignore
| activity when activity.Source = activitySource ->
let collection = tags |> Seq.map KeyValuePair |> ActivityTagsCollection
let event = new ActivityEvent(name, tags = collection)
activity.AddEvent event |> ignore
| _ -> ()

let addEvent name = addEventWithTags name Seq.empty

module Profiling =

module Tags =
Expand Down
2 changes: 2 additions & 0 deletions src/Compiler/Utilities/Activity.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ module internal Activity =

val addEvent: name: string -> unit

val addEventWithTags: name: string -> tags: (string * objnull) seq -> unit

module Profiling =
val startAndMeasureEnvironmentStats: name: string -> IDisposable MaybeNull
val addConsoleListener: unit -> IDisposable
Expand Down
11 changes: 10 additions & 1 deletion tests/FSharp.Test.Utilities/XunitHelpers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,16 @@ type FSharpXunitFramework(sink: IMessageSink) =
cleanUpTemporaryDirectoryOfThisTestRun ()
traceProvider.ForceFlush() |> ignore
traceProvider.Dispose()
base.Dispose()
base.Dispose()

// Group test run under single activity, to make traces more readable.
// Otherwise this overriden method is not necessary and can be removed.
override this.CreateExecutor (assemblyName) =
{ new XunitTestFrameworkExecutor(assemblyName, this.SourceInformationProvider, this.DiagnosticMessageSink) with
override _.RunTestCases(testCases, executionMessageSink, executionOptions) =
use _ = Activity.start $"{assemblyName.Name} {Runtime.InteropServices.RuntimeInformation.FrameworkDescription}" []
base.RunTestCases(testCases, executionMessageSink, executionOptions)
}

override this.CreateDiscoverer (assemblyInfo) =
{ new XunitTestFrameworkDiscoverer(assemblyInfo, this.SourceInformationProvider, this.DiagnosticMessageSink) with
Expand Down
14 changes: 13 additions & 1 deletion vsintegration/Vsix/VisualFSharpFull/VisualFSharp.Core.targets
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,19 @@
<Private>False</Private>
</ProjectReference>

<ProjectReference Include="$(FSharpSourcesRoot)\..\vsintegration\src\FSharp.Editor\FSharp.Editor.fsproj">
<!--Include OTel dlls in DEBUG to allow trace collection-->
<ProjectReference Include="$(FSharpSourcesRoot)\..\vsintegration\src\FSharp.Editor\FSharp.Editor.fsproj" Condition="'$(Configuration)'=='Debug'">
<Project>{65e0e82a-eace-4787-8994-888674c2fe87}</Project>
<Name>FSharp.Editor</Name>
<IncludeOutputGroupsInVSIX>ReferenceCopyLocalPathsOutputGroup%3bBuiltProjectOutputGroup%3bGetCopyToOutputDirectoryItems%3bPkgDefProjectOutputGroup%3bSatelliteDllsProjectOutputGroup%3b</IncludeOutputGroupsInVSIX>
<IncludeOutputGroupsInVSIXLocalOnly>DebugSymbolsProjectOutputGroup%3b</IncludeOutputGroupsInVSIXLocalOnly>
<Ngen>true</Ngen>
<NgenArchitecture>All</NgenArchitecture>
<NgenPriority>2</NgenPriority>
<Private>True</Private>
</ProjectReference>

<ProjectReference Include="$(FSharpSourcesRoot)\..\vsintegration\src\FSharp.Editor\FSharp.Editor.fsproj" Condition="'$(Configuration)'=='Release'">
<Project>{65e0e82a-eace-4787-8994-888674c2fe87}</Project>
<Name>FSharp.Editor</Name>
<IncludeOutputGroupsInVSIX>BuiltProjectOutputGroup%3bGetCopyToOutputDirectoryItems%3bPkgDefProjectOutputGroup%3bSatelliteDllsProjectOutputGroup%3b</IncludeOutputGroupsInVSIX>
Expand Down
20 changes: 20 additions & 0 deletions vsintegration/src/FSharp.Editor/Common/Logging.fs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ open Microsoft.VisualStudio.Shell
open Microsoft.VisualStudio.Shell.Interop
open Microsoft.VisualStudio.FSharp.Editor

open FSharp.Compiler.Diagnostics

[<RequireQualifiedAccess>]
type LogType =
| Info
Expand Down Expand Up @@ -116,7 +118,12 @@ module Logging =
let logExceptionWithContext (ex: Exception, context) =
logErrorf "Context: %s\nException Message: %s\nStack Trace: %s" context ex.Message ex.StackTrace

#if DEBUG
module Activity =

open OpenTelemetry.Resources
open OpenTelemetry.Trace

let listen filter =
let indent (activity: Activity) =
let rec loop (activity: Activity) n =
Expand Down Expand Up @@ -145,4 +152,17 @@ module Activity =

ActivitySource.AddActivityListener(listener)

let export () =
OpenTelemetry.Sdk
.CreateTracerProviderBuilder()
.AddSource(ActivityNames.FscSourceName)
.SetResourceBuilder(
ResourceBuilder
.CreateDefault()
.AddService(serviceName = "F#", serviceVersion = "1.0.0")
)
.AddOtlpExporter()
.Build()

let listenToAll () = listen ""
#endif
1 change: 1 addition & 0 deletions vsintegration/src/FSharp.Editor/FSharp.Editor.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@
<PackageReference Include="Microsoft.VisualStudio.Text.UI.Wpf" Version="$(MicrosoftVisualStudioTextUIWpfVersion)" PrivateAssets="all" ExcludeAssets="runtime;contentFiles;build;analyzers;native" />
<PackageReference Include="Microsoft.VisualStudio.Threading" Version="$(MicrosoftVisualStudioThreadingVersion)" PrivateAssets="all" ExcludeAssets="runtime;contentFiles;build;analyzers;native" />
<PackageReference Include="Microsoft.VisualStudio.Validation" Version="$(MicrosoftVisualStudioValidationVersion)" PrivateAssets="all" ExcludeAssets="runtime;contentFiles;build;analyzers;native" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.9.0" Condition="'$(Configuration)' == 'Debug'" />
<PackageReference Include="Newtonsoft.Json" Version="$(NewtonsoftJsonVersion)" PrivateAssets="all" ExcludeAssets="runtime;contentFiles;build;analyzers;native" />
<PackageReference Include="StreamJsonRpc" Version="$(StreamJsonRpcVersion)" />
<PackageReference Include="Nerdbank.Streams" Version="$(NerdbankStreamsVersion)" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ type internal FSharpPackage() as this =
let mutable solutionEventsOpt = None

#if DEBUG
let _traceProvider = Logging.Activity.export ()
let _logger = Logging.Activity.listenToAll ()
// Logging.Activity.listen "IncrementalBuild"
#endif
Expand Down

0 comments on commit 5d0812f

Please sign in to comment.