-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathEgressHelper.cs
248 lines (204 loc) · 10.2 KB
/
EgressHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.CommandLine;
using System.Globalization;
using System.IO;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading;
using System.Threading.Tasks;
namespace Microsoft.Diagnostics.Monitoring.Extension.Common
{
public sealed class EgressHelper
{
private static Stream StdInStream;
private static CancellationTokenSource CancelSource = new CancellationTokenSource();
private const int ExpectedPayloadProtocolVersion = 1;
public static CliCommand CreateEgressCommand<TProvider, TOptions>(Action<IServiceCollection> configureServices = null)
where TProvider : EgressProvider<TOptions>
where TOptions : class, new()
{
CliCommand executeCommand = new CliCommand("Execute", "Execute is for egressing an artifact.");
executeCommand.SetAction((result, token) => Egress<TProvider, TOptions>(configureServices, token));
CliCommand validateCommand = new CliCommand("Validate", "Validate is for validating an extension's options on configuration change.");
validateCommand.SetAction((result, token) => Validate<TProvider, TOptions>(configureServices, token));
CliCommand egressCommand = new CliCommand("Egress", "The class of extension being invoked.")
{
executeCommand,
validateCommand
};
return egressCommand;
}
private static async Task<int> Egress<TProvider, TOptions>(Action<IServiceCollection> configureServices, CancellationToken token)
where TProvider : EgressProvider<TOptions>
where TOptions : class, new()
{
EgressArtifactResult result = new();
try
{
ExtensionEgressPayload configPayload = await GetPayload(token);
await using var serviceProvider = BuildServiceProvider<TProvider, TOptions>(configureServices, configPayload);
EgressProvider<TOptions> provider = serviceProvider.GetRequiredService<EgressProvider<TOptions>>();
TOptions options = serviceProvider.GetRequiredService<IOptionsSnapshot<TOptions>>().Get(configPayload.ProviderName);
Console.CancelKeyPress += Console_CancelKeyPress;
result.ArtifactPath = await provider.EgressAsync(
options,
GetStream,
configPayload.Settings,
token);
result.Succeeded = true;
}
catch (Exception ex)
{
result.Succeeded = false;
result.FailureMessage = ex.Message;
}
return ProcessEgressResult(result);
}
private static async Task<int> Validate<TProvider, TOptions>(Action<IServiceCollection> configureServices, CancellationToken token)
where TProvider : EgressProvider<TOptions>
where TOptions : class, new()
{
EgressArtifactResult result = new();
try
{
ExtensionEgressPayload configPayload = await GetPayload(token);
await using var serviceProvider = BuildServiceProvider<TProvider, TOptions>(configureServices, configPayload);
TOptions options = serviceProvider.GetRequiredService<IOptionsSnapshot<TOptions>>().Get(configPayload.ProviderName);
result.ArtifactPath = string.Empty;
result.Succeeded = true;
}
catch (OptionsValidationException ex)
{
result.Succeeded = false;
result.FailureMessage = ex.Message;
}
return ProcessEgressResult(result);
}
private static int ProcessEgressResult(EgressArtifactResult result)
{
string jsonBlob = JsonSerializer.Serialize(result);
Console.Write(jsonBlob);
// return non-zero exit code when failed
return result.Succeeded ? 0 : 1;
}
private static ServiceProvider BuildServiceProvider<TProvider, TOptions>(Action<IServiceCollection> configureServices, ExtensionEgressPayload configPayload)
where TProvider : EgressProvider<TOptions>
where TOptions : class, new()
{
// Design Points:
// - The serialization model is separate from the parts that interact directly with the egress implementation.
// For example, the egress implementations are not able to access the ExtensionEgressPayload instance. This
// allows changing the serialization format and structure without affecting the egress implementations.
// - The use of dependency injection allows the caller to contribute their custom services that are unknown
// to the egress infrastructure; it further eliminates the need for this method to understand how to validate
// egress options (barring the generic data annotation validator), how to further configure egress options
// (if anything additional is necessary), creating and passing optional services such as loggers, etc.
ServiceCollection services = CreateServices<TOptions>(configPayload, configureServices);
// Attempt to register the egress provider if not already registered; this allows the service configuration
// callback to register the egress provider if it has additional requirements that cannot be fulfilled by
// dependency injection.
services.TryAddSingleton<EgressProvider<TOptions>, TProvider>();
#if NET7_0_OR_GREATER
services.MakeReadOnly();
#endif
ServiceProvider serviceProvider = services.BuildServiceProvider();
return serviceProvider;
}
internal static async Task<ExtensionEgressPayload> GetPayload(CancellationToken token)
{
StdInStream = Console.OpenStandardInput();
int dotnetMonitorPayloadProtocolVersion;
long payloadLengthBuffer;
byte[] payloadBuffer;
using (BinaryReader reader = new BinaryReader(StdInStream, Encoding.UTF8, leaveOpen: true))
{
dotnetMonitorPayloadProtocolVersion = reader.ReadInt32();
if (dotnetMonitorPayloadProtocolVersion != ExpectedPayloadProtocolVersion)
{
throw new EgressException(string.Format(CultureInfo.CurrentCulture, Strings.ErrorMessage_IncorrectPayloadVersion, dotnetMonitorPayloadProtocolVersion, ExpectedPayloadProtocolVersion));
}
payloadLengthBuffer = reader.ReadInt64();
if (payloadLengthBuffer < 0)
{
throw new ArgumentOutOfRangeException(nameof(payloadLengthBuffer));
}
}
payloadBuffer = new byte[payloadLengthBuffer];
await ReadExactlyAsync(payloadBuffer, token);
ExtensionEgressPayload configPayload = JsonSerializer.Deserialize<ExtensionEgressPayload>(payloadBuffer);
return configPayload;
}
private static ServiceCollection CreateServices<TOptions>(ExtensionEgressPayload payload, Action<IServiceCollection> configureServices)
where TOptions : class, new()
{
ServiceCollection services = new();
// Logging
services.AddLogging(builder =>
{
builder.SetMinimumLevel(payload.LogLevel);
builder.AddConsole();
});
// Options configuration, validation, etc
services.AddOptions<TOptions>(payload.ProviderName)
.Configure(options =>
{
IConfigurationBuilder builder = new ConfigurationBuilder();
builder
.AddInMemoryCollection(payload.Configuration)
.Build()
.Bind(options);
});
services.AddSingleton<IValidateOptions<TOptions>, DataAnnotationValidateOptions<TOptions>>();
services.AddSingleton(new EgressProperties(payload.Properties));
// Optionally allow additional services; this allows the caller to participate in dependency injection
// and fulfillment of services that the common egress infrastructure has no knowledge about.
configureServices?.Invoke(services);
return services;
}
private static void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
{
CancelSource.Cancel();
StdInStream.Close();
}
private static async Task GetStream(Stream outputStream, CancellationToken cancellationToken)
{
const int DefaultBufferSize = 0x10000;
await StdInStream.CopyToAsync(outputStream, DefaultBufferSize, cancellationToken);
}
private static async Task ReadExactlyAsync(Memory<byte> buffer, CancellationToken token)
{
#if NET7_0_OR_GREATER
await StdInStream.ReadExactlyAsync(buffer, token);
#else
int totalRead = 0;
while (totalRead < buffer.Length)
{
int read = await StdInStream.ReadAsync(buffer.Slice(totalRead), token).ConfigureAwait(false);
if (read == 0)
{
throw new EndOfStreamException();
}
totalRead += read;
}
#endif
}
}
internal sealed class ExtensionEgressPayload
{
public EgressArtifactSettings Settings { get; set; }
public Dictionary<string, string> Properties { get; set; }
public Dictionary<string, string> Configuration { get; set; }
public string ProviderName { get; set; }
[JsonConverter(typeof(JsonStringEnumConverter))]
public LogLevel LogLevel { get; set; }
}
}