Skip to content

Commit 41fa06a

Browse files
authored
Merge pull request #2 from RedTTGMoss/feature/metrics
Feature/metrics
2 parents c6fc827 + b4c8593 commit 41fa06a

File tree

7 files changed

+117
-9
lines changed

7 files changed

+117
-9
lines changed

src/Moss.NET.Sdk/Core/Extensions.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Diagnostics.Metrics;
23
using System.Text.Json;
34
using System.Text.Json.Serialization.Metadata;
45
using Extism;
@@ -57,4 +58,15 @@ public static dynamic Get(this HoconRoot root, string path)
5758
{
5859
return new Options(root.GetObject(path));
5960
}
61+
62+
public static void Measure(string name, Action method)
63+
{
64+
var meter = MossExtension.Instance!.Meter;
65+
var methodDuration = meter.CreateHistogram<double>(name + "_duration", "ms", "Method duration in milliseconds");
66+
67+
var stopwatch = System.Diagnostics.Stopwatch.StartNew();
68+
method();
69+
stopwatch.Stop();
70+
methodDuration.Record(stopwatch.Elapsed.TotalMilliseconds);
71+
}
6072
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System.Collections.Generic;
2+
3+
namespace Moss.NET.Sdk.Core.Instrumentation;
4+
5+
using System;
6+
using System.Diagnostics.Metrics;
7+
using System.IO;
8+
9+
internal class FileMeterListener : MeterListener<double>
10+
{
11+
private static string _filePath;
12+
private static MeterListener _listener;
13+
14+
public FileMeterListener(string filePath)
15+
{
16+
_filePath = filePath;
17+
18+
if (!File.Exists(_filePath))
19+
{
20+
File.Create(filePath);
21+
}
22+
}
23+
24+
protected override void OnMeasurementRecorded(Instrument instrument, double measurement, ReadOnlySpan<KeyValuePair<string, object?>> tags, object? state)
25+
{
26+
var tagList = new List<string>();
27+
foreach (var tag in tags)
28+
{
29+
tagList.Add($"{tag.Key}: {tag.Value}");
30+
}
31+
32+
using var writer = new StreamWriter(_filePath, append: true);
33+
writer.WriteLine($"{DateTime.UtcNow:O} - {instrument.Name}, Measurement: {measurement}{instrument.Unit}, Tags: {string.Join(", ", tagList)}");
34+
}
35+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics.Metrics;
4+
5+
namespace Moss.NET.Sdk.Core.Instrumentation;
6+
7+
public interface IMeterListener
8+
{
9+
internal void Init();
10+
}
11+
12+
public abstract class MeterListener<T> : IMeterListener
13+
where T : struct
14+
{
15+
private readonly MeterListener _listener = new();
16+
17+
protected abstract void OnMeasurementRecorded(Instrument instrument, T measurement, ReadOnlySpan<KeyValuePair<string, object?>> tags,
18+
object? state);
19+
20+
public void Init()
21+
{
22+
_listener.InstrumentPublished = (instrument, listener) => listener.EnableMeasurementEvents(instrument);
23+
_listener.SetMeasurementEventCallback<T>(OnMeasurementRecorded);
24+
_listener.Start();
25+
}
26+
}

src/Moss.NET.Sdk/MossExtension.cs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
using System;
2+
using System.Diagnostics.Metrics;
23
using System.Runtime.CompilerServices;
34
using System.Text.Json;
45
using Extism;
56
using Hocon;
7+
using Moss.NET.Sdk.Core;
8+
using Moss.NET.Sdk.Core.Instrumentation;
69
using Moss.NET.Sdk.FFI;
710
using Moss.NET.Sdk.Scheduler;
811

@@ -12,6 +15,9 @@ public class MossExtension
1215
{
1316
private static MossExtension? _instance;
1417

18+
public Meter Meter;
19+
public IMeterListener MeterListener;
20+
1521
internal static MossExtension? Instance
1622
{
1723
get
@@ -38,13 +44,17 @@ public virtual void ExtensionLoop(MossState state)
3844

3945
public static void PreInitExtension()
4046
{
41-
var configSource = System.IO.File.ReadAllText("extension/automation.conf");
42-
Config = HoconParser.Parse(configSource);
47+
var configPath = "extension/automation.conf";
48+
49+
if (System.IO.File.Exists(configPath)) {
50+
var configSource = System.IO.File.ReadAllText(configPath);
51+
Config = HoconParser.Parse(configSource);
52+
}
4353

4454
TaskScheduler.Init();
4555
}
4656

47-
public static HoconRoot Config { get; set; }
57+
public static HoconRoot? Config { get; set; }
4858

4959
public static void Init<T>() where T : MossExtension, new()
5060
{
@@ -54,7 +64,21 @@ public static void PreInitExtension()
5464
_instance = Activator.CreateInstance<T>();
5565

5666
var input = Pdk.GetInputJson(JsonContext.Default.MossState);
57-
_instance!.Register(input!);
67+
68+
#if DEBUG
69+
_instance.Meter = new Meter("Moss.NET.Sdk");
70+
_instance.MeterListener = new FileMeterListener("extension/instrumentation.txt");
71+
_instance.MeterListener.Init();
72+
73+
Extensions.Measure("extension.register", () =>
74+
{
75+
#endif
76+
_instance!.Register(input!);
77+
#if DEBUG
78+
});
79+
#endif
80+
81+
5882
var extensionInfo = new ExtensionInfo
5983
{
6084
Files = Assets.Expose()

src/Moss.NET.Sdk/Scheduler/TaskScheduler.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@ public static void Init()
6868
// ignored
6969
}
7070

71+
ReadJobConfig(json);
72+
}
73+
74+
private static void ReadJobConfig(string json)
75+
{
76+
if (MossExtension.Config is null)
77+
{
78+
return;
79+
}
80+
7181
_config = MossExtension.Config.GetObject("scheduler");
7282

7383
foreach (var jobInfo in _config.GetObject("jobs"))

src/SdkTesterPlugin/SampleExtension.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public override void ExtensionLoop(MossState state)
6060

6161
GUI.InvertIcon("swap", "swap_inverted");
6262

63-
RunOnce.Execute("test", async () =>
63+
RunOnce.Execute("test", void () =>
6464
{
6565
cm.Open(10, 10);
6666

@@ -89,11 +89,11 @@ public override void ExtensionLoop(MossState state)
8989
var epub = new EpubNotebook("test ebook", "extension/Assets/test.epub");
9090
InternalFunctions.NewContentEpub();
9191

92-
await pdf.UploadAsync();
92+
pdf.Upload();
9393

9494
InternalFunctions.ExportDocument("0ba3df9c-8ca0-4347-8d7c-07471101baad");
9595

96-
await quickSheets.EnsureDownloadAsync();
96+
quickSheets.EnsureDownload();
9797
quickSheets.LoadFilesFromCache();
9898
quickSheets.UnloadFiles();
9999

@@ -115,14 +115,14 @@ public override void ExtensionLoop(MossState state)
115115
Uuid = quickSheets.Metadata.Accessor.Uuid,
116116
});*/
117117

118-
await duplicate.EnsureDownloadAsync();
118+
duplicate.EnsureDownload();
119119
duplicate.Metadata.Get<string>("visible_name");
120120

121121
duplicate.Metadata.Set("visible_name", "Duplicated QuickSheet");
122122
duplicate.Metadata.Set("parent", "4dba1a54-93b8-4992-886f-08c0b17f93da");
123123

124124
var k = duplicate.Duplicate();
125-
await duplicate.UploadAsync();
125+
duplicate.Upload();
126126
k.Delete(() => _logger.Info("Deleted"));
127127

128128
var docs = Enumerable.Repeat(0, 2)

src/SdkTesterPlugin/SdkTesterPlugin.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11+
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="9.0.3" />
1112
<ProjectReference Include="..\Moss.NET.Sdk\Moss.NET.Sdk.csproj"/>
1213
<PackageReference Include="Extism.runtime.win-x64" Version="1.9.1"/>
1314
<PackageReference Include="Extism.Pdk" Version="1.1.1" />

0 commit comments

Comments
 (0)