Skip to content

Commit 5d0812f

Browse files
authored
More OpenTelemetry changes (#18246)
* 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
1 parent 7866d66 commit 5d0812f

File tree

8 files changed

+66
-15
lines changed

8 files changed

+66
-15
lines changed

src/Compiler/Facilities/DiagnosticsLogger.fs

+11-11
Original file line numberDiff line numberDiff line change
@@ -882,17 +882,17 @@ type StackGuard(maxDepth: int, name: string) =
882882
[<CallerFilePath; Optional; DefaultParameterValue("")>] path: string,
883883
[<CallerLineNumber; Optional; DefaultParameterValue(0)>] line: int
884884
) =
885-
use _ =
886-
Activity.start
887-
"DiagnosticsLogger.StackGuard.Guard"
888-
[|
889-
Activity.Tags.stackGuardName, name
890-
Activity.Tags.stackGuardCurrentDepth, string depth
891-
Activity.Tags.stackGuardMaxDepth, string maxDepth
892-
Activity.Tags.callerMemberName, memberName
893-
Activity.Tags.callerFilePath, path
894-
Activity.Tags.callerLineNumber, string line
895-
|]
885+
886+
Activity.addEventWithTags
887+
"DiagnosticsLogger.StackGuard.Guard"
888+
(seq {
889+
Activity.Tags.stackGuardName, box name
890+
Activity.Tags.stackGuardCurrentDepth, depth
891+
Activity.Tags.stackGuardMaxDepth, maxDepth
892+
Activity.Tags.callerMemberName, memberName
893+
Activity.Tags.callerFilePath, path
894+
Activity.Tags.callerLineNumber, line
895+
})
896896

897897
depth <- depth + 1
898898

src/Compiler/Utilities/Activity.fs

+8-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ open System.Diagnostics
77
open System.IO
88
open System.Text
99
open Internal.Utilities.Library
10+
open System.Collections.Generic
1011

1112

1213
module ActivityNames =
@@ -102,12 +103,17 @@ module internal Activity =
102103

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

105-
let addEvent name =
106+
let addEventWithTags name (tags: (string * objnull) seq) =
106107
match Activity.Current with
107108
| null -> ()
108-
| activity when activity.Source = activitySource -> activity.AddEvent(ActivityEvent name) |> ignore
109+
| activity when activity.Source = activitySource ->
110+
let collection = tags |> Seq.map KeyValuePair |> ActivityTagsCollection
111+
let event = new ActivityEvent(name, tags = collection)
112+
activity.AddEvent event |> ignore
109113
| _ -> ()
110114

115+
let addEvent name = addEventWithTags name Seq.empty
116+
111117
module Profiling =
112118

113119
module Tags =

src/Compiler/Utilities/Activity.fsi

+2
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ module internal Activity =
4646

4747
val addEvent: name: string -> unit
4848

49+
val addEventWithTags: name: string -> tags: (string * objnull) seq -> unit
50+
4951
module Profiling =
5052
val startAndMeasureEnvironmentStats: name: string -> IDisposable MaybeNull
5153
val addConsoleListener: unit -> IDisposable

tests/FSharp.Test.Utilities/XunitHelpers.fs

+10-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,16 @@ type FSharpXunitFramework(sink: IMessageSink) =
168168
cleanUpTemporaryDirectoryOfThisTestRun ()
169169
traceProvider.ForceFlush() |> ignore
170170
traceProvider.Dispose()
171-
base.Dispose()
171+
base.Dispose()
172+
173+
// Group test run under single activity, to make traces more readable.
174+
// Otherwise this overriden method is not necessary and can be removed.
175+
override this.CreateExecutor (assemblyName) =
176+
{ new XunitTestFrameworkExecutor(assemblyName, this.SourceInformationProvider, this.DiagnosticMessageSink) with
177+
override _.RunTestCases(testCases, executionMessageSink, executionOptions) =
178+
use _ = Activity.start $"{assemblyName.Name} {Runtime.InteropServices.RuntimeInformation.FrameworkDescription}" []
179+
base.RunTestCases(testCases, executionMessageSink, executionOptions)
180+
}
172181

173182
override this.CreateDiscoverer (assemblyInfo) =
174183
{ new XunitTestFrameworkDiscoverer(assemblyInfo, this.SourceInformationProvider, this.DiagnosticMessageSink) with

vsintegration/Vsix/VisualFSharpFull/VisualFSharp.Core.targets

+13-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,19 @@
110110
<Private>False</Private>
111111
</ProjectReference>
112112

113-
<ProjectReference Include="$(FSharpSourcesRoot)\..\vsintegration\src\FSharp.Editor\FSharp.Editor.fsproj">
113+
<!--Include OTel dlls in DEBUG to allow trace collection-->
114+
<ProjectReference Include="$(FSharpSourcesRoot)\..\vsintegration\src\FSharp.Editor\FSharp.Editor.fsproj" Condition="'$(Configuration)'=='Debug'">
115+
<Project>{65e0e82a-eace-4787-8994-888674c2fe87}</Project>
116+
<Name>FSharp.Editor</Name>
117+
<IncludeOutputGroupsInVSIX>ReferenceCopyLocalPathsOutputGroup%3bBuiltProjectOutputGroup%3bGetCopyToOutputDirectoryItems%3bPkgDefProjectOutputGroup%3bSatelliteDllsProjectOutputGroup%3b</IncludeOutputGroupsInVSIX>
118+
<IncludeOutputGroupsInVSIXLocalOnly>DebugSymbolsProjectOutputGroup%3b</IncludeOutputGroupsInVSIXLocalOnly>
119+
<Ngen>true</Ngen>
120+
<NgenArchitecture>All</NgenArchitecture>
121+
<NgenPriority>2</NgenPriority>
122+
<Private>True</Private>
123+
</ProjectReference>
124+
125+
<ProjectReference Include="$(FSharpSourcesRoot)\..\vsintegration\src\FSharp.Editor\FSharp.Editor.fsproj" Condition="'$(Configuration)'=='Release'">
114126
<Project>{65e0e82a-eace-4787-8994-888674c2fe87}</Project>
115127
<Name>FSharp.Editor</Name>
116128
<IncludeOutputGroupsInVSIX>BuiltProjectOutputGroup%3bGetCopyToOutputDirectoryItems%3bPkgDefProjectOutputGroup%3bSatelliteDllsProjectOutputGroup%3b</IncludeOutputGroupsInVSIX>

vsintegration/src/FSharp.Editor/Common/Logging.fs

+20
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ open Microsoft.VisualStudio.Shell
77
open Microsoft.VisualStudio.Shell.Interop
88
open Microsoft.VisualStudio.FSharp.Editor
99

10+
open FSharp.Compiler.Diagnostics
11+
1012
[<RequireQualifiedAccess>]
1113
type LogType =
1214
| Info
@@ -116,7 +118,12 @@ module Logging =
116118
let logExceptionWithContext (ex: Exception, context) =
117119
logErrorf "Context: %s\nException Message: %s\nStack Trace: %s" context ex.Message ex.StackTrace
118120

121+
#if DEBUG
119122
module Activity =
123+
124+
open OpenTelemetry.Resources
125+
open OpenTelemetry.Trace
126+
120127
let listen filter =
121128
let indent (activity: Activity) =
122129
let rec loop (activity: Activity) n =
@@ -145,4 +152,17 @@ module Activity =
145152

146153
ActivitySource.AddActivityListener(listener)
147154

155+
let export () =
156+
OpenTelemetry.Sdk
157+
.CreateTracerProviderBuilder()
158+
.AddSource(ActivityNames.FscSourceName)
159+
.SetResourceBuilder(
160+
ResourceBuilder
161+
.CreateDefault()
162+
.AddService(serviceName = "F#", serviceVersion = "1.0.0")
163+
)
164+
.AddOtlpExporter()
165+
.Build()
166+
148167
let listenToAll () = listen ""
168+
#endif

vsintegration/src/FSharp.Editor/FSharp.Editor.fsproj

+1
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@
194194
<PackageReference Include="Microsoft.VisualStudio.Text.UI.Wpf" Version="$(MicrosoftVisualStudioTextUIWpfVersion)" PrivateAssets="all" ExcludeAssets="runtime;contentFiles;build;analyzers;native" />
195195
<PackageReference Include="Microsoft.VisualStudio.Threading" Version="$(MicrosoftVisualStudioThreadingVersion)" PrivateAssets="all" ExcludeAssets="runtime;contentFiles;build;analyzers;native" />
196196
<PackageReference Include="Microsoft.VisualStudio.Validation" Version="$(MicrosoftVisualStudioValidationVersion)" PrivateAssets="all" ExcludeAssets="runtime;contentFiles;build;analyzers;native" />
197+
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.9.0" Condition="'$(Configuration)' == 'Debug'" />
197198
<PackageReference Include="Newtonsoft.Json" Version="$(NewtonsoftJsonVersion)" PrivateAssets="all" ExcludeAssets="runtime;contentFiles;build;analyzers;native" />
198199
<PackageReference Include="StreamJsonRpc" Version="$(StreamJsonRpcVersion)" />
199200
<PackageReference Include="Nerdbank.Streams" Version="$(NerdbankStreamsVersion)" />

vsintegration/src/FSharp.Editor/LanguageService/LanguageService.fs

+1
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ type internal FSharpPackage() as this =
341341
let mutable solutionEventsOpt = None
342342

343343
#if DEBUG
344+
let _traceProvider = Logging.Activity.export ()
344345
let _logger = Logging.Activity.listenToAll ()
345346
// Logging.Activity.listen "IncrementalBuild"
346347
#endif

0 commit comments

Comments
 (0)