Skip to content

Commit 52dc7f8

Browse files
committed
quick hack to start the dashboard on Ionide startup if otel is requested
1 parent f8392a8 commit 52dc7f8

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed

src/Components/OTel.fs

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
namespace Ionide.VSCode.FSharp
2+
3+
module OTel =
4+
open Fable.Import.VSCode.Vscode
5+
open Node.Api
6+
open Node.Base
7+
8+
let private logger =
9+
ConsoleAndOutputChannelLogger(Some "OTel", Level.DEBUG, None, Some Level.DEBUG)
10+
11+
let private settingsString = "FSharp.openTelemetry"
12+
let mutable private isOTelEnabled = false
13+
14+
let mutable private hasDocker = false
15+
16+
let mutable private runningContainerId: string = null
17+
18+
type SpawnResult =
19+
abstract pid: int
20+
abstract output: string[][]
21+
abstract stdout: string
22+
abstract stderr: string
23+
abstract status: int option
24+
abstract signal: string option
25+
abstract error: Error option
26+
27+
let private runCommand command (args: string[]) =
28+
let options =
29+
{| cwd = None
30+
env = None
31+
encoding = "utf8"
32+
shell = true |}
33+
34+
let result =
35+
childProcess.spawnSync (command, ResizeArray args, options) :?> SpawnResult
36+
37+
match result.error with
38+
| Some err ->
39+
logger.Error("Error running command %s: %s", command, err.message)
40+
failwithf "%A" err
41+
| None -> result
42+
43+
let private startContainer () =
44+
let result =
45+
runCommand
46+
"docker"
47+
[| "run"
48+
"-d"
49+
"--rm"
50+
"-it"
51+
"-p"
52+
"18888:18888"
53+
"-p"
54+
"4317:18889"
55+
"mcr.microsoft.com/dotnet/nightly/aspire-dashboard:8.0.0-preview.4" |]
56+
57+
logger.Info("Container started with result %j", result)
58+
runningContainerId <- result.stdout
59+
isOTelEnabled <- true
60+
61+
let enableOTelListener () =
62+
isOTelEnabled <- true
63+
64+
if hasDocker then
65+
startContainer ()
66+
logger.Info("OpenTelemetry listener enabled")
67+
()
68+
else
69+
logger.Warn("OpenTelemetry listener requires Docker to be installed") |> ignore
70+
71+
()
72+
73+
let stopContainer (containerId: string) =
74+
runCommand "docker" [| "stop"; containerId |] |> ignore
75+
76+
let disableOTelListener () =
77+
isOTelEnabled <- false
78+
79+
if hasDocker && not (System.String.IsNullOrEmpty runningContainerId) then
80+
stopContainer runningContainerId |> ignore
81+
logger.Info("OpenTelemetry listener disabled")
82+
()
83+
84+
let detectDocker () =
85+
logger.Info("detecting presence of docker")
86+
hasDocker <- true
87+
88+
let activate (ctx: ExtensionContext) =
89+
detectDocker ()
90+
91+
let settings = workspace.getConfiguration settingsString
92+
let newEnablement = settings.get<bool> ("enabled")
93+
94+
logger.Info("Configuration value is %j", newEnablement)
95+
96+
match isOTelEnabled, newEnablement with
97+
| true, (Some false | None) -> disableOTelListener ()
98+
| false, Some true -> enableOTelListener ()
99+
| _, _ -> ()

src/Ionide.FSharp.fsproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
<Compile Include="Components/SolutionExplorer.fs" />
5252
<Compile Include="Components/TestExplorer.fs" />
5353
<Compile Include="Components/InlayHints.fs" />
54+
<Compile Include="Components/OTel.fs" />
5455
<Compile Include="fsharp.fs" />
5556
</ItemGroup>
5657
<Import Project="..\.paket\Paket.Restore.targets" />

src/fsharp.fs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ let activate (context: ExtensionContext) : JS.Promise<Api> =
107107
tryActivate "testExplorer" TestExplorer.activate context
108108
tryActivate "inlayhints" InlayHints.activate context
109109
tryActivate "languageservice" activateLanguageServiceRestart context
110+
tryActivate "otel" OTel.activate context
110111

111112
let buildProject project =
112113
promise {

0 commit comments

Comments
 (0)