mirror of https://github.com/dotnet/tye.git
55 changed files with 4923 additions and 0 deletions
@ -0,0 +1,74 @@ |
|||
using Microsoft.Extensions.Configuration; |
|||
using Microsoft.Extensions.Logging; |
|||
|
|||
namespace Micronetes.Hosting.Diagnostics |
|||
{ |
|||
public class DiagnosticOptions |
|||
{ |
|||
public (string Key, string Value) LoggingProvider { get; set; } |
|||
public (string Key, string Value) DistributedTraceProvider { get; set; } |
|||
public (string Key, string Value) MetricsProvider { get; set; } |
|||
|
|||
public static DiagnosticOptions FromConfiguration(IConfiguration configuration) |
|||
{ |
|||
return new DiagnosticOptions |
|||
{ |
|||
LoggingProvider = GetProvider(configuration, "logs"), |
|||
DistributedTraceProvider = GetProvider(configuration, "dtrace"), |
|||
MetricsProvider = GetProvider(configuration, "metrics") |
|||
}; |
|||
} |
|||
|
|||
private static (string, string) GetProvider(IConfiguration configuration, string providerName) |
|||
{ |
|||
var providerString = configuration[providerName]; |
|||
|
|||
if (string.IsNullOrEmpty(providerString)) |
|||
{ |
|||
return (null, null); |
|||
} |
|||
|
|||
var pair = providerString.Split('='); |
|||
|
|||
if (pair.Length < 2) |
|||
{ |
|||
return (pair[0].Trim(), null); |
|||
} |
|||
|
|||
return (pair[0].Trim(), pair[1].Trim()); |
|||
} |
|||
|
|||
public void DumpDiagnostics(ILogger logger) |
|||
{ |
|||
var (logProviderKey, logProviderValue) = LoggingProvider; |
|||
var (dTraceProviderKey, dTraceProviderValue) = DistributedTraceProvider; |
|||
|
|||
switch (logProviderKey?.ToLowerInvariant()) |
|||
{ |
|||
case "elastic": |
|||
logger.LogInformation("logs: Using ElasticSearch at {URL}", logProviderValue); |
|||
break; |
|||
case "ai": |
|||
logger.LogInformation("logs: Using ApplicationInsights instrumentation key {InstrumentationKey}", logProviderValue); |
|||
break; |
|||
case "console": |
|||
logger.LogInformation("logs: Using console logs"); |
|||
break; |
|||
case "seq": |
|||
logger.LogInformation("logs: Using Seq at {URL}", logProviderValue); |
|||
break; |
|||
default: |
|||
break; |
|||
} |
|||
|
|||
switch (dTraceProviderKey?.ToLowerInvariant()) |
|||
{ |
|||
case "zipkin": |
|||
logger.LogInformation("dtrace: Using Zipkin at URL {URL}", dTraceProviderValue); |
|||
break; |
|||
default: |
|||
break; |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,853 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Diagnostics; |
|||
using System.Diagnostics.Tracing; |
|||
using System.IO; |
|||
using System.Linq; |
|||
using System.Text.Json; |
|||
using System.Threading; |
|||
using System.Threading.Tasks; |
|||
using Micronetes.Hosting.Diagnostics.Logging; |
|||
using Micronetes.Hosting.Diagnostics.Metrics; |
|||
using Microsoft.Diagnostics.NETCore.Client; |
|||
using Microsoft.Diagnostics.Tracing; |
|||
using Microsoft.Diagnostics.Tracing.Parsers; |
|||
using Microsoft.Extensions.Logging; |
|||
using OpenTelemetry.Exporter.Zipkin; |
|||
using OpenTelemetry.Trace; |
|||
using OpenTelemetry.Trace.Export; |
|||
using Serilog; |
|||
|
|||
namespace Micronetes.Hosting.Diagnostics |
|||
{ |
|||
public class DiagnosticsCollector |
|||
{ |
|||
// This list of event sources needs to be extensible
|
|||
private static readonly string MicrosoftExtensionsLoggingProviderName = "Microsoft-Extensions-Logging"; |
|||
private static readonly string SystemRuntimeEventSourceName = "System.Runtime"; |
|||
private static readonly string MicrosoftAspNetCoreHostingEventSourceName = "Microsoft.AspNetCore.Hosting"; |
|||
private static readonly string GrpcAspNetCoreServer = "Grpc.AspNetCore.Server"; |
|||
private static readonly string DiagnosticSourceEventSource = "Microsoft-Diagnostics-DiagnosticSource"; |
|||
private static readonly string TplEventSource = "System.Threading.Tasks.TplEventSource"; |
|||
|
|||
// This is the list of events for distributed tracing
|
|||
private static readonly string DiagnosticFilterString = "\"" + |
|||
"Microsoft.AspNetCore/Microsoft.AspNetCore.Hosting.HttpRequestIn.Start@Activity1Start:-" + |
|||
"Request.Scheme" + |
|||
";Request.Host" + |
|||
";Request.PathBase" + |
|||
";Request.QueryString" + |
|||
";Request.Path" + |
|||
";Request.Method" + |
|||
";ActivityStartTime=*Activity.StartTimeUtc.Ticks" + |
|||
";ActivityParentId=*Activity.ParentId" + |
|||
";ActivityId=*Activity.Id" + |
|||
";ActivitySpanId=*Activity.SpanId" + |
|||
";ActivityTraceId=*Activity.TraceId" + |
|||
";ActivityParentSpanId=*Activity.ParentSpanId" + |
|||
";ActivityIdFormat=*Activity.IdFormat" + |
|||
"\r\n" + |
|||
"Microsoft.AspNetCore/Microsoft.AspNetCore.Hosting.HttpRequestIn.Stop@Activity1Stop:-" + |
|||
"Response.StatusCode" + |
|||
";ActivityDuration=*Activity.Duration.Ticks" + |
|||
";ActivityId=*Activity.Id" + |
|||
"\r\n" + |
|||
"HttpHandlerDiagnosticListener/System.Net.Http.HttpRequestOut@Event:-" + |
|||
"\r\n" + |
|||
"HttpHandlerDiagnosticListener/System.Net.Http.HttpRequestOut.Start@Activity2Start:-" + |
|||
"Request.RequestUri" + |
|||
";Request.Method" + |
|||
";Request.RequestUri.Host" + |
|||
";Request.RequestUri.Port" + |
|||
";ActivityStartTime=*Activity.StartTimeUtc.Ticks" + |
|||
";ActivityId=*Activity.Id" + |
|||
";ActivitySpanId=*Activity.SpanId" + |
|||
";ActivityTraceId=*Activity.TraceId" + |
|||
";ActivityParentSpanId=*Activity.ParentSpanId" + |
|||
";ActivityIdFormat=*Activity.IdFormat" + |
|||
";ActivityId=*Activity.Id" + |
|||
"\r\n" + |
|||
"HttpHandlerDiagnosticListener/System.Net.Http.HttpRequestOut.Stop@Activity2Stop:-" + |
|||
";ActivityDuration=*Activity.Duration.Ticks" + |
|||
";ActivityId=*Activity.Id" + |
|||
"\r\n" + |
|||
|
|||
"\""; |
|||
|
|||
private readonly Microsoft.Extensions.Logging.ILogger _logger; |
|||
private readonly DiagnosticOptions _options; |
|||
|
|||
public DiagnosticsCollector(Microsoft.Extensions.Logging.ILogger logger, DiagnosticOptions options) |
|||
{ |
|||
_logger = logger; |
|||
_options = options; |
|||
} |
|||
|
|||
public void ProcessEvents(string applicationName, |
|||
string serviceName, |
|||
int processId, |
|||
string replicaName, |
|||
IDictionary<string, string> metrics, |
|||
CancellationToken cancellationToken) |
|||
{ |
|||
var hasEventPipe = false; |
|||
|
|||
for (int i = 0; i < 10; ++i) |
|||
{ |
|||
if (DiagnosticsClient.GetPublishedProcesses().Contains(processId)) |
|||
{ |
|||
hasEventPipe = true; |
|||
break; |
|||
} |
|||
|
|||
if (cancellationToken.IsCancellationRequested) |
|||
{ |
|||
return; |
|||
} |
|||
|
|||
Thread.Sleep(500); |
|||
} |
|||
|
|||
if (!hasEventPipe) |
|||
{ |
|||
_logger.LogInformation("Process id {PID}, does not support event pipe", processId); |
|||
return; |
|||
} |
|||
|
|||
_logger.LogInformation("Listening for event pipe events for {ServiceName} on process id {PID}", replicaName, processId); |
|||
|
|||
// Create the logger factory for this replica
|
|||
using var loggerFactory = LoggerFactory.Create(builder => ConfigureLogging(serviceName, replicaName, builder)); |
|||
|
|||
var processor = new SimpleSpanProcessor(CreateSpanExporter(serviceName, replicaName)); |
|||
|
|||
var providers = new List<EventPipeProvider>() |
|||
{ |
|||
// Runtime Metrics
|
|||
new EventPipeProvider( |
|||
SystemRuntimeEventSourceName, |
|||
EventLevel.Informational, |
|||
(long)ClrTraceEventParser.Keywords.None, |
|||
new Dictionary<string, string>() { |
|||
{ "EventCounterIntervalSec", "1" } |
|||
} |
|||
), |
|||
new EventPipeProvider( |
|||
MicrosoftAspNetCoreHostingEventSourceName, |
|||
EventLevel.Informational, |
|||
(long)ClrTraceEventParser.Keywords.None, |
|||
new Dictionary<string, string>() { |
|||
{ "EventCounterIntervalSec", "1" } |
|||
} |
|||
), |
|||
new EventPipeProvider( |
|||
GrpcAspNetCoreServer, |
|||
EventLevel.Informational, |
|||
(long)ClrTraceEventParser.Keywords.None, |
|||
new Dictionary<string, string>() { |
|||
{ "EventCounterIntervalSec", "1" } |
|||
} |
|||
), |
|||
|
|||
// Application Metrics
|
|||
new EventPipeProvider( |
|||
applicationName, |
|||
EventLevel.Informational, |
|||
(long)ClrTraceEventParser.Keywords.None, |
|||
new Dictionary<string, string>() { |
|||
{ "EventCounterIntervalSec", "1" } |
|||
} |
|||
), |
|||
|
|||
// Logging
|
|||
new EventPipeProvider( |
|||
MicrosoftExtensionsLoggingProviderName, |
|||
EventLevel.LogAlways, |
|||
(long)(LoggingEventSource.Keywords.JsonMessage | LoggingEventSource.Keywords.FormattedMessage) |
|||
), |
|||
|
|||
// Distributed Tracing
|
|||
|
|||
// Activity correlation
|
|||
new EventPipeProvider(TplEventSource, |
|||
keywords: 0x80, |
|||
eventLevel: EventLevel.LogAlways), |
|||
|
|||
// Diagnostic source events
|
|||
new EventPipeProvider(DiagnosticSourceEventSource, |
|||
keywords: 0x1 | 0x2, |
|||
eventLevel: EventLevel.Verbose, |
|||
arguments: new Dictionary<string,string> |
|||
{ |
|||
{ "FilterAndPayloadSpecs", DiagnosticFilterString } |
|||
}) |
|||
}; |
|||
|
|||
while (!cancellationToken.IsCancellationRequested) |
|||
{ |
|||
EventPipeSession session = null; |
|||
var client = new DiagnosticsClient(processId); |
|||
|
|||
try |
|||
{ |
|||
session = client.StartEventPipeSession(providers); |
|||
} |
|||
catch (EndOfStreamException) |
|||
{ |
|||
break; |
|||
} |
|||
catch (Exception ex) |
|||
{ |
|||
if (!cancellationToken.IsCancellationRequested) |
|||
{ |
|||
_logger.LogDebug(0, ex, "Failed to start the event pipe session"); |
|||
} |
|||
|
|||
// We can't even start the session, wait until the process boots up again to start another metrics thread
|
|||
break; |
|||
} |
|||
|
|||
void StopSession() |
|||
{ |
|||
try |
|||
{ |
|||
session.Stop(); |
|||
} |
|||
catch (EndOfStreamException) |
|||
{ |
|||
// If the app we're monitoring exits abruptly, this may throw in which case we just swallow the exception and exit gracefully.
|
|||
} |
|||
// We may time out if the process ended before we sent StopTracing command. We can just exit in that case.
|
|||
catch (TimeoutException) |
|||
{ |
|||
} |
|||
// On Unix platforms, we may actually get a PNSE since the pipe is gone with the process, and Runtime Client Library
|
|||
// does not know how to distinguish a situation where there is no pipe to begin with, or where the process has exited
|
|||
// before dotnet-counters and got rid of a pipe that once existed.
|
|||
// Since we are catching this in StopMonitor() we know that the pipe once existed (otherwise the exception would've
|
|||
// been thrown in StartMonitor directly)
|
|||
catch (PlatformNotSupportedException) |
|||
{ |
|||
} |
|||
} |
|||
|
|||
using var _ = cancellationToken.Register(() => StopSession()); |
|||
|
|||
try |
|||
{ |
|||
var source = new EventPipeEventSource(session.EventStream); |
|||
|
|||
// Distribued Tracing
|
|||
HandleDistributedTracingEvents(source, processor); |
|||
|
|||
// Metrics
|
|||
HandleEventCounters(source, metrics); |
|||
|
|||
// Logging
|
|||
HandleLoggingEvents(source, loggerFactory, replicaName); |
|||
|
|||
source.Process(); |
|||
} |
|||
catch (DiagnosticsClientException ex) |
|||
{ |
|||
_logger.LogDebug(0, ex, "Failed to start the event pipe session"); |
|||
} |
|||
catch (Exception) |
|||
{ |
|||
// This fails if stop is called or if the process dies
|
|||
} |
|||
finally |
|||
{ |
|||
session?.Dispose(); |
|||
} |
|||
} |
|||
|
|||
_logger.LogInformation("Event pipe collection completed for {ServiceName} on process id {PID}", replicaName, processId); |
|||
} |
|||
|
|||
private void HandleLoggingEvents(EventPipeEventSource source, ILoggerFactory loggerFactory, string replicaName) |
|||
{ |
|||
string lastFormattedMessage = ""; |
|||
|
|||
var logActivities = new Dictionary<Guid, LogActivityItem>(); |
|||
var stack = new Stack<Guid>(); |
|||
|
|||
source.Dynamic.AddCallbackForProviderEvent(MicrosoftExtensionsLoggingProviderName, "ActivityJsonStart/Start", (traceEvent) => |
|||
{ |
|||
var factoryId = (int)traceEvent.PayloadByName("FactoryID"); |
|||
var categoryName = (string)traceEvent.PayloadByName("LoggerName"); |
|||
var argsJson = (string)traceEvent.PayloadByName("ArgumentsJson"); |
|||
|
|||
// TODO: Store this information by logger factory id
|
|||
var item = new LogActivityItem |
|||
{ |
|||
ActivityID = traceEvent.ActivityID, |
|||
ScopedObject = new LogObject(JsonDocument.Parse(argsJson).RootElement), |
|||
}; |
|||
|
|||
if (stack.TryPeek(out var parentId) && logActivities.TryGetValue(parentId, out var parentItem)) |
|||
{ |
|||
item.Parent = parentItem; |
|||
} |
|||
|
|||
stack.Push(traceEvent.ActivityID); |
|||
logActivities[traceEvent.ActivityID] = item; |
|||
}); |
|||
|
|||
source.Dynamic.AddCallbackForProviderEvent(MicrosoftExtensionsLoggingProviderName, "ActivityJsonStop/Stop", (traceEvent) => |
|||
{ |
|||
var factoryId = (int)traceEvent.PayloadByName("FactoryID"); |
|||
var categoryName = (string)traceEvent.PayloadByName("LoggerName"); |
|||
|
|||
stack.Pop(); |
|||
logActivities.Remove(traceEvent.ActivityID); |
|||
}); |
|||
|
|||
source.Dynamic.AddCallbackForProviderEvent(MicrosoftExtensionsLoggingProviderName, "MessageJson", (traceEvent) => |
|||
{ |
|||
// Level, FactoryID, LoggerName, EventID, EventName, ExceptionJson, ArgumentsJson
|
|||
var logLevel = (LogLevel)traceEvent.PayloadByName("Level"); |
|||
var factoryId = (int)traceEvent.PayloadByName("FactoryID"); |
|||
var categoryName = (string)traceEvent.PayloadByName("LoggerName"); |
|||
var eventId = (int)traceEvent.PayloadByName("EventId"); |
|||
var eventName = (string)traceEvent.PayloadByName("EventName"); |
|||
var exceptionJson = (string)traceEvent.PayloadByName("ExceptionJson"); |
|||
var argsJson = (string)traceEvent.PayloadByName("ArgumentsJson"); |
|||
|
|||
// There's a bug that causes some of the columns to get mixed up
|
|||
if (eventName.StartsWith("{")) |
|||
{ |
|||
argsJson = exceptionJson; |
|||
exceptionJson = eventName; |
|||
eventName = null; |
|||
} |
|||
|
|||
if (string.IsNullOrEmpty(argsJson)) |
|||
{ |
|||
return; |
|||
} |
|||
|
|||
Exception exception = null; |
|||
|
|||
var logger = loggerFactory.CreateLogger(categoryName); |
|||
|
|||
var scopes = new List<IDisposable>(); |
|||
|
|||
if (logActivities.TryGetValue(traceEvent.ActivityID, out var logActivityItem)) |
|||
{ |
|||
// REVIEW: Does order matter here? We're combining everything anyways.
|
|||
while (logActivityItem != null) |
|||
{ |
|||
scopes.Add(logger.BeginScope(logActivityItem.ScopedObject)); |
|||
|
|||
logActivityItem = logActivityItem.Parent; |
|||
} |
|||
} |
|||
|
|||
try |
|||
{ |
|||
if (exceptionJson != "{}") |
|||
{ |
|||
var exceptionMessage = JsonSerializer.Deserialize<JsonElement>(exceptionJson); |
|||
exception = new LoggerException(exceptionMessage); |
|||
} |
|||
|
|||
var message = JsonSerializer.Deserialize<JsonElement>(argsJson); |
|||
if (message.TryGetProperty("{OriginalFormat}", out var formatElement)) |
|||
{ |
|||
var formatString = formatElement.GetString(); |
|||
var formatter = new LogValuesFormatter(formatString); |
|||
object[] args = new object[formatter.ValueNames.Count]; |
|||
for (int i = 0; i < args.Length; i++) |
|||
{ |
|||
args[i] = message.GetProperty(formatter.ValueNames[i]).GetString(); |
|||
} |
|||
|
|||
logger.Log(logLevel, new EventId(eventId, eventName), exception, formatString, args); |
|||
} |
|||
else |
|||
{ |
|||
var obj = new LogObject(message, lastFormattedMessage); |
|||
logger.Log(logLevel, new EventId(eventId, eventName), obj, exception, LogObject.Callback); |
|||
} |
|||
} |
|||
catch (Exception ex) |
|||
{ |
|||
_logger.LogDebug(ex, "Error processing log entry for {ServiceName}", replicaName); |
|||
} |
|||
finally |
|||
{ |
|||
scopes.ForEach(d => d.Dispose()); |
|||
} |
|||
}); |
|||
|
|||
source.Dynamic.AddCallbackForProviderEvent(MicrosoftExtensionsLoggingProviderName, "FormattedMessage", (traceEvent) => |
|||
{ |
|||
// Level, FactoryID, LoggerName, EventID, EventName, FormattedMessage
|
|||
var logLevel = (LogLevel)traceEvent.PayloadByName("Level"); |
|||
var factoryId = (int)traceEvent.PayloadByName("FactoryID"); |
|||
var categoryName = (string)traceEvent.PayloadByName("LoggerName"); |
|||
var eventId = (int)traceEvent.PayloadByName("EventId"); |
|||
var eventName = (string)traceEvent.PayloadByName("EventName"); |
|||
var formattedMessage = (string)traceEvent.PayloadByName("FormattedMessage"); |
|||
|
|||
if (string.IsNullOrEmpty(formattedMessage)) |
|||
{ |
|||
formattedMessage = eventName; |
|||
eventName = ""; |
|||
} |
|||
|
|||
lastFormattedMessage = formattedMessage; |
|||
}); |
|||
} |
|||
|
|||
private void HandleEventCounters(EventPipeEventSource source, IDictionary<string, string> metrics) |
|||
{ |
|||
source.Dynamic.All += traceEvent => |
|||
{ |
|||
try |
|||
{ |
|||
// Metrics
|
|||
if (traceEvent.EventName.Equals("EventCounters")) |
|||
{ |
|||
var payloadVal = (IDictionary<string, object>)traceEvent.PayloadValue(0); |
|||
var eventPayload = (IDictionary<string, object>)payloadVal["Payload"]; |
|||
|
|||
ICounterPayload payload = CounterPayload.FromPayload(eventPayload); |
|||
|
|||
metrics[traceEvent.ProviderName + "/" + payload.Name] = payload.Value; |
|||
} |
|||
} |
|||
catch (Exception ex) |
|||
{ |
|||
_logger.LogError(ex, "Error processing counter for {ProviderName}:{EventName}", traceEvent.ProviderName, traceEvent.EventName); |
|||
} |
|||
}; |
|||
} |
|||
|
|||
private static void HandleDistributedTracingEvents(EventPipeEventSource source, SpanProcessor processor) |
|||
{ |
|||
var activities = new Dictionary<string, ActivityItem>(); |
|||
|
|||
source.Dynamic.All += traceEvent => |
|||
{ |
|||
|
|||
if (traceEvent.EventName == "Activity1Start/Start") |
|||
{ |
|||
var listenerEventName = (string)traceEvent.PayloadByName("EventName"); |
|||
|
|||
if (traceEvent.PayloadByName("Arguments") is IDictionary<string, object>[] arguments) |
|||
{ |
|||
if (TryCreateActivity(arguments, out var item)) |
|||
{ |
|||
string method = null; |
|||
string path = null; |
|||
string host = null; |
|||
string pathBase = null; |
|||
string queryString = null; |
|||
string scheme = null; |
|||
|
|||
foreach (var arg in arguments) |
|||
{ |
|||
var key = (string)arg["Key"]; |
|||
var value = (string)arg["Value"]; |
|||
|
|||
if (key == "Path") |
|||
{ |
|||
path = value; |
|||
} |
|||
else if (key == "Method") |
|||
{ |
|||
method = value; |
|||
} |
|||
else if (key == "Host") |
|||
{ |
|||
host = value; |
|||
} |
|||
else if (key == "PathBase") |
|||
{ |
|||
pathBase = value; |
|||
} |
|||
else if (key == "Scheme") |
|||
{ |
|||
scheme = value; |
|||
} |
|||
else if (key == "QueryString") |
|||
{ |
|||
queryString = value; |
|||
} |
|||
} |
|||
|
|||
item.Name = path; |
|||
item.Kind = SpanKind.Server; |
|||
item.Attributes[SpanAttributeConstants.HttpUrlKey] = scheme + "://" + host + pathBase + path + queryString; |
|||
item.Attributes[SpanAttributeConstants.HttpMethodKey] = method; |
|||
item.Attributes[SpanAttributeConstants.HttpPathKey] = path; |
|||
|
|||
activities[item.Id] = item; |
|||
} |
|||
} |
|||
} |
|||
|
|||
if (traceEvent.EventName == "Activity1Stop/Stop") |
|||
{ |
|||
var listenerEventName = (string)traceEvent.PayloadByName("EventName"); |
|||
|
|||
if (traceEvent.PayloadByName("Arguments") is IDictionary<string, object>[] arguments) |
|||
{ |
|||
var (activityId, duration) = GetActivityStop(arguments); |
|||
|
|||
int statusCode = 0; |
|||
|
|||
foreach (var arg in arguments) |
|||
{ |
|||
var key = (string)arg["Key"]; |
|||
var value = (string)arg["Value"]; |
|||
|
|||
if (key == "StatusCode") |
|||
{ |
|||
statusCode = int.Parse(value); |
|||
} |
|||
} |
|||
|
|||
if (activityId != null && activities.TryGetValue(activityId, out var item)) |
|||
{ |
|||
item.Attributes[SpanAttributeConstants.HttpStatusCodeKey] = statusCode; |
|||
|
|||
item.EndTime = item.StartTime + duration; |
|||
|
|||
var spanData = new SpanData(item.Name, |
|||
new SpanContext(item.TraceId, item.SpanId, ActivityTraceFlags.Recorded), |
|||
item.ParentSpanId, |
|||
item.Kind, |
|||
item.StartTime, |
|||
item.Attributes, |
|||
Enumerable.Empty<Event>(), |
|||
Enumerable.Empty<Link>(), |
|||
null, |
|||
Status.Ok, |
|||
item.EndTime); |
|||
|
|||
processor.OnEnd(spanData); |
|||
|
|||
activities.Remove(activityId); |
|||
} |
|||
} |
|||
} |
|||
if (traceEvent.EventName == "Activity2Start/Start") |
|||
{ |
|||
var listenerEventName = (string)traceEvent.PayloadByName("EventName"); |
|||
|
|||
if (traceEvent.PayloadByName("Arguments") is IDictionary<string, object>[] arguments) |
|||
{ |
|||
string uri = null; |
|||
string method = null; |
|||
|
|||
foreach (var arg in arguments) |
|||
{ |
|||
var key = (string)arg["Key"]; |
|||
var value = (string)arg["Value"]; |
|||
|
|||
if (key == "RequestUri") |
|||
{ |
|||
uri = value; |
|||
} |
|||
else if (key == "Method") |
|||
{ |
|||
method = value; |
|||
} |
|||
} |
|||
|
|||
if (TryCreateActivity(arguments, out var item)) |
|||
{ |
|||
item.Name = uri; |
|||
item.Kind = SpanKind.Client; |
|||
|
|||
item.Attributes[SpanAttributeConstants.HttpUrlKey] = uri; |
|||
item.Attributes[SpanAttributeConstants.HttpMethodKey] = method; |
|||
|
|||
activities[item.Id] = item; |
|||
} |
|||
} |
|||
} |
|||
if (traceEvent.EventName == "Activity2Stop/Stop") |
|||
{ |
|||
var listenerEventName = (string)traceEvent.PayloadByName("EventName"); |
|||
|
|||
if (traceEvent.PayloadByName("Arguments") is IDictionary<string, object>[] arguments) |
|||
{ |
|||
var (activityId, duration) = GetActivityStop(arguments); |
|||
|
|||
if (activityId != null && activities.TryGetValue(activityId, out var item)) |
|||
{ |
|||
item.EndTime = item.StartTime + duration; |
|||
|
|||
var spanData = new SpanData(item.Name, |
|||
new SpanContext(item.TraceId, item.SpanId, ActivityTraceFlags.Recorded), |
|||
item.ParentSpanId, |
|||
item.Kind, |
|||
item.StartTime, |
|||
item.Attributes, |
|||
Enumerable.Empty<Event>(), |
|||
Enumerable.Empty<Link>(), |
|||
null, |
|||
Status.Ok, |
|||
item.EndTime); |
|||
|
|||
processor.OnEnd(spanData); |
|||
|
|||
activities.Remove(activityId); |
|||
} |
|||
} |
|||
} |
|||
}; |
|||
} |
|||
|
|||
private static (string ActivityId, TimeSpan Duration) GetActivityStop(IDictionary<string, object>[] arguments) |
|||
{ |
|||
var activityId = default(string); |
|||
var duration = default(TimeSpan); |
|||
|
|||
foreach (var arg in arguments) |
|||
{ |
|||
var key = (string)arg["Key"]; |
|||
var value = (string)arg["Value"]; |
|||
|
|||
if (key == "ActivityId") |
|||
{ |
|||
activityId = value; |
|||
} |
|||
else if (key == "ActivityDuration") |
|||
{ |
|||
duration = new TimeSpan(long.Parse(value)); |
|||
} |
|||
} |
|||
|
|||
return (activityId, duration); |
|||
} |
|||
|
|||
private static bool TryCreateActivity(IDictionary<string, object>[] arguments, out ActivityItem item) |
|||
{ |
|||
string activityId = null; |
|||
string operationName = null; |
|||
string spanId = null; |
|||
string parentSpanId = null; |
|||
string traceId = null; |
|||
DateTime startTime = default; |
|||
ActivityIdFormat idFormat = default; |
|||
|
|||
foreach (var arg in arguments) |
|||
{ |
|||
var key = (string)arg["Key"]; |
|||
var value = (string)arg["Value"]; |
|||
|
|||
if (key == "ActivityId") |
|||
{ |
|||
activityId = value; |
|||
} |
|||
else if (key == "ActivityOperationName") |
|||
{ |
|||
operationName = value; |
|||
} |
|||
else if (key == "ActivitySpanId") |
|||
{ |
|||
spanId = value; |
|||
} |
|||
else if (key == "ActivityTraceId") |
|||
{ |
|||
traceId = value; |
|||
} |
|||
else if (key == "ActivityParentSpanId") |
|||
{ |
|||
parentSpanId = value; |
|||
} |
|||
else if (key == "ActivityStartTime") |
|||
{ |
|||
startTime = new DateTime(long.Parse(value), DateTimeKind.Utc); |
|||
} |
|||
else if (key == "ActivityIdFormat") |
|||
{ |
|||
idFormat = Enum.Parse<ActivityIdFormat>(value); |
|||
} |
|||
} |
|||
|
|||
if (string.IsNullOrEmpty(activityId)) |
|||
{ |
|||
item = null; |
|||
// Not a 3.1 application (we can detect this earlier)
|
|||
return false; |
|||
} |
|||
|
|||
if (idFormat == ActivityIdFormat.Hierarchical) |
|||
{ |
|||
// We need W3C to make it work
|
|||
item = null; |
|||
return false; |
|||
} |
|||
|
|||
// This is what open telemetry currently does
|
|||
// https://github.com/open-telemetry/opentelemetry-dotnet/blob/4ba732af062ddc2759c02aebbc91335aaa3f7173/src/OpenTelemetry.Collector.AspNetCore/Implementation/HttpInListener.cs#L65-L92
|
|||
|
|||
item = new ActivityItem() |
|||
{ |
|||
Id = activityId, |
|||
Name = operationName, |
|||
SpanId = ActivitySpanId.CreateFromString(spanId), |
|||
TraceId = ActivityTraceId.CreateFromString(traceId), |
|||
ParentSpanId = parentSpanId == "0000000000000000" ? default : ActivitySpanId.CreateFromString(parentSpanId), |
|||
StartTime = startTime, |
|||
}; |
|||
|
|||
return true; |
|||
} |
|||
|
|||
// This is the logger factory for application logs. It allows re-routing event pipe collected logs (structured logs)
|
|||
// to any of the supported sinks, currently (elastic search and app insights)
|
|||
private void ConfigureLogging(string serviceName, string replicaName, ILoggingBuilder builder) |
|||
{ |
|||
var logProviderKey = _options.LoggingProvider.Key; |
|||
var logProviderValue = _options.LoggingProvider.Value; |
|||
|
|||
if (string.Equals(logProviderKey, "elastic", StringComparison.OrdinalIgnoreCase) && |
|||
!string.IsNullOrEmpty(logProviderValue)) |
|||
{ |
|||
var loggerConfiguration = new LoggerConfiguration() |
|||
.Enrich.WithProperty("Application", serviceName) |
|||
.Enrich.WithProperty("Instance", replicaName) |
|||
.Enrich.FromLogContext() |
|||
.WriteTo.Elasticsearch(logProviderValue); |
|||
|
|||
builder.AddSerilog(loggerConfiguration.CreateLogger()); |
|||
} |
|||
|
|||
if (string.Equals(logProviderKey, "console", StringComparison.OrdinalIgnoreCase)) |
|||
{ |
|||
var loggerConfiguration = new LoggerConfiguration() |
|||
.Enrich.WithProperty("Application", serviceName) |
|||
.Enrich.WithProperty("Instance", replicaName) |
|||
.Enrich.FromLogContext() |
|||
.WriteTo.Console(outputTemplate: "[{Instance}]: [{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}"); |
|||
|
|||
builder.AddSerilog(loggerConfiguration.CreateLogger()); |
|||
} |
|||
|
|||
if (string.Equals(logProviderKey, "seq", StringComparison.OrdinalIgnoreCase)) |
|||
{ |
|||
var loggerConfiguration = new LoggerConfiguration() |
|||
.Enrich.WithProperty("Application", serviceName) |
|||
.Enrich.WithProperty("Instance", replicaName) |
|||
.Enrich.FromLogContext() |
|||
.WriteTo.Seq(logProviderValue); |
|||
|
|||
builder.AddSerilog(loggerConfiguration.CreateLogger()); |
|||
} |
|||
|
|||
if (string.Equals(logProviderKey, "ai", StringComparison.OrdinalIgnoreCase) && |
|||
!string.IsNullOrEmpty(logProviderValue)) |
|||
{ |
|||
builder.AddApplicationInsights(logProviderValue); |
|||
} |
|||
|
|||
// REVIEW: How are log levels controlled on the outside?
|
|||
builder.SetMinimumLevel(LogLevel.Information); |
|||
} |
|||
|
|||
|
|||
private SpanExporter CreateSpanExporter(string serviceName, string replicaName) |
|||
{ |
|||
if (string.Equals(_options.DistributedTraceProvider.Key, "zipkin", StringComparison.OrdinalIgnoreCase) && |
|||
!string.IsNullOrEmpty(_options.DistributedTraceProvider.Value)) |
|||
{ |
|||
var zipkin = new ZipkinTraceExporter(new ZipkinTraceExporterOptions |
|||
{ |
|||
ServiceName = serviceName, |
|||
Endpoint = new Uri($"{_options.DistributedTraceProvider.Value.TrimEnd('/')}/api/v2/spans") |
|||
}); |
|||
|
|||
return zipkin; |
|||
} |
|||
|
|||
// TODO: Support Jaegar
|
|||
// TODO: Support ApplicationInsights
|
|||
|
|||
return new NullExporter(); |
|||
} |
|||
|
|||
public class NullExporter : SpanExporter |
|||
{ |
|||
public override Task<ExportResult> ExportAsync(IEnumerable<SpanData> batch, CancellationToken cancellationToken) |
|||
{ |
|||
return Task.FromResult(ExportResult.Success); |
|||
} |
|||
|
|||
public override Task ShutdownAsync(CancellationToken cancellationToken) |
|||
{ |
|||
return Task.CompletedTask; |
|||
} |
|||
} |
|||
|
|||
private class ActivityItem |
|||
{ |
|||
public string Id { get; set; } |
|||
public string Name { get; set; } |
|||
public ActivityTraceId TraceId { get; set; } |
|||
public ActivitySpanId SpanId { get; set; } |
|||
public Dictionary<string, object> Attributes { get; } = new Dictionary<string, object>(); |
|||
public DateTime StartTime { get; set; } |
|||
public DateTime EndTime { get; set; } |
|||
public SpanKind Kind { get; set; } |
|||
|
|||
public ActivitySpanId ParentSpanId { get; set; } |
|||
} |
|||
|
|||
private class LogActivityItem |
|||
{ |
|||
public Guid ActivityID { get; set; } |
|||
|
|||
public LogObject ScopedObject { get; set; } |
|||
|
|||
public LogActivityItem Parent { get; set; } |
|||
} |
|||
|
|||
internal static class SpanAttributeConstants |
|||
{ |
|||
public static readonly string ComponentKey = "component"; |
|||
|
|||
public static readonly string HttpMethodKey = "http.method"; |
|||
public static readonly string HttpStatusCodeKey = "http.status_code"; |
|||
public static readonly string HttpUserAgentKey = "http.user_agent"; |
|||
public static readonly string HttpPathKey = "http.path"; |
|||
public static readonly string HttpHostKey = "http.host"; |
|||
public static readonly string HttpUrlKey = "http.url"; |
|||
public static readonly string HttpRouteKey = "http.route"; |
|||
public static readonly string HttpFlavorKey = "http.flavor"; |
|||
} |
|||
|
|||
internal sealed class LoggingEventSource |
|||
{ |
|||
/// <summary>
|
|||
/// This is public from an EventSource consumer point of view, but since these defintions
|
|||
/// are not needed outside this class
|
|||
/// </summary>
|
|||
public static class Keywords |
|||
{ |
|||
/// <summary>
|
|||
/// Meta events are events about the LoggingEventSource itself (that is they did not come from ILogger
|
|||
/// </summary>
|
|||
public const EventKeywords Meta = (EventKeywords)1; |
|||
/// <summary>
|
|||
/// Turns on the 'Message' event when ILogger.Log() is called. It gives the information in a programmatic (not formatted) way
|
|||
/// </summary>
|
|||
public const EventKeywords Message = (EventKeywords)2; |
|||
/// <summary>
|
|||
/// Turns on the 'FormatMessage' event when ILogger.Log() is called. It gives the formatted string version of the information.
|
|||
/// </summary>
|
|||
public const EventKeywords FormattedMessage = (EventKeywords)4; |
|||
/// <summary>
|
|||
/// Turns on the 'MessageJson' event when ILogger.Log() is called. It gives JSON representation of the Arguments.
|
|||
/// </summary>
|
|||
public const EventKeywords JsonMessage = (EventKeywords)8; |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,67 @@ |
|||
using System; |
|||
using System.Collections; |
|||
using System.Collections.Generic; |
|||
using System.Text.Json; |
|||
|
|||
namespace Micronetes.Hosting.Diagnostics.Logging |
|||
{ |
|||
internal class LogObject : IReadOnlyList<KeyValuePair<string, object>> |
|||
{ |
|||
internal static readonly Func<object, Exception, string> Callback = (state, exception) => ((LogObject)state).ToString(); |
|||
|
|||
private readonly string _formattedMessage; |
|||
private List<KeyValuePair<string, object>> _items = new List<KeyValuePair<string, object>>(); |
|||
|
|||
public LogObject(JsonElement element, string formattedMessage = null) |
|||
{ |
|||
foreach (var item in element.EnumerateObject()) |
|||
{ |
|||
switch (item.Value.ValueKind) |
|||
{ |
|||
case JsonValueKind.Undefined: |
|||
break; |
|||
case JsonValueKind.Object: |
|||
break; |
|||
case JsonValueKind.Array: |
|||
break; |
|||
case JsonValueKind.String: |
|||
_items.Add(new KeyValuePair<string, object>(item.Name, item.Value.GetString())); |
|||
break; |
|||
case JsonValueKind.Number: |
|||
_items.Add(new KeyValuePair<string, object>(item.Name, item.Value.GetInt32())); |
|||
break; |
|||
case JsonValueKind.False: |
|||
case JsonValueKind.True: |
|||
_items.Add(new KeyValuePair<string, object>(item.Name, item.Value.GetBoolean())); |
|||
break; |
|||
case JsonValueKind.Null: |
|||
_items.Add(new KeyValuePair<string, object>(item.Name, null)); |
|||
break; |
|||
default: |
|||
break; |
|||
} |
|||
} |
|||
|
|||
_formattedMessage = formattedMessage; |
|||
} |
|||
|
|||
public KeyValuePair<string, object> this[int index] => _items[index]; |
|||
|
|||
public int Count => _items.Count; |
|||
|
|||
public IEnumerator<KeyValuePair<string, object>> GetEnumerator() |
|||
{ |
|||
return _items.GetEnumerator(); |
|||
} |
|||
|
|||
IEnumerator IEnumerable.GetEnumerator() |
|||
{ |
|||
return GetEnumerator(); |
|||
} |
|||
|
|||
public override string ToString() |
|||
{ |
|||
return _formattedMessage ?? string.Empty; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,200 @@ |
|||
// Copyright (c) .NET Foundation. All rights reserved.
|
|||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
|||
|
|||
using System; |
|||
using System.Collections; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Globalization; |
|||
using System.Text; |
|||
|
|||
namespace Micronetes.Hosting.Diagnostics.Logging |
|||
{ |
|||
/// <summary>
|
|||
/// Formatter to convert the named format items like {NamedformatItem} to <see cref="M:string.Format"/> format.
|
|||
/// </summary>
|
|||
internal class LogValuesFormatter |
|||
{ |
|||
private const string NullValue = "(null)"; |
|||
private static readonly object[] EmptyArray = new object[0]; |
|||
private static readonly char[] FormatDelimiters = { ',', ':' }; |
|||
private readonly string _format; |
|||
private readonly List<string> _valueNames = new List<string>(); |
|||
|
|||
public LogValuesFormatter(string format) |
|||
{ |
|||
OriginalFormat = format; |
|||
|
|||
var sb = new StringBuilder(); |
|||
var scanIndex = 0; |
|||
var endIndex = format.Length; |
|||
|
|||
while (scanIndex < endIndex) |
|||
{ |
|||
var openBraceIndex = FindBraceIndex(format, '{', scanIndex, endIndex); |
|||
var closeBraceIndex = FindBraceIndex(format, '}', openBraceIndex, endIndex); |
|||
|
|||
if (closeBraceIndex == endIndex) |
|||
{ |
|||
sb.Append(format, scanIndex, endIndex - scanIndex); |
|||
scanIndex = endIndex; |
|||
} |
|||
else |
|||
{ |
|||
// Format item syntax : { index[,alignment][ :formatString] }.
|
|||
var formatDelimiterIndex = FindIndexOfAny(format, FormatDelimiters, openBraceIndex, closeBraceIndex); |
|||
|
|||
sb.Append(format, scanIndex, openBraceIndex - scanIndex + 1); |
|||
sb.Append(_valueNames.Count.ToString(CultureInfo.InvariantCulture)); |
|||
_valueNames.Add(format.Substring(openBraceIndex + 1, formatDelimiterIndex - openBraceIndex - 1)); |
|||
sb.Append(format, formatDelimiterIndex, closeBraceIndex - formatDelimiterIndex + 1); |
|||
|
|||
scanIndex = closeBraceIndex + 1; |
|||
} |
|||
} |
|||
|
|||
_format = sb.ToString(); |
|||
} |
|||
|
|||
public string OriginalFormat { get; private set; } |
|||
public List<string> ValueNames => _valueNames; |
|||
|
|||
private static int FindBraceIndex(string format, char brace, int startIndex, int endIndex) |
|||
{ |
|||
// Example: {{prefix{{{Argument}}}suffix}}.
|
|||
var braceIndex = endIndex; |
|||
var scanIndex = startIndex; |
|||
var braceOccurenceCount = 0; |
|||
|
|||
while (scanIndex < endIndex) |
|||
{ |
|||
if (braceOccurenceCount > 0 && format[scanIndex] != brace) |
|||
{ |
|||
if (braceOccurenceCount % 2 == 0) |
|||
{ |
|||
// Even number of '{' or '}' found. Proceed search with next occurence of '{' or '}'.
|
|||
braceOccurenceCount = 0; |
|||
braceIndex = endIndex; |
|||
} |
|||
else |
|||
{ |
|||
// An unescaped '{' or '}' found.
|
|||
break; |
|||
} |
|||
} |
|||
else if (format[scanIndex] == brace) |
|||
{ |
|||
if (brace == '}') |
|||
{ |
|||
if (braceOccurenceCount == 0) |
|||
{ |
|||
// For '}' pick the first occurence.
|
|||
braceIndex = scanIndex; |
|||
} |
|||
} |
|||
else |
|||
{ |
|||
// For '{' pick the last occurence.
|
|||
braceIndex = scanIndex; |
|||
} |
|||
|
|||
braceOccurenceCount++; |
|||
} |
|||
|
|||
scanIndex++; |
|||
} |
|||
|
|||
return braceIndex; |
|||
} |
|||
|
|||
private static int FindIndexOfAny(string format, char[] chars, int startIndex, int endIndex) |
|||
{ |
|||
var findIndex = format.IndexOfAny(chars, startIndex, endIndex - startIndex); |
|||
return findIndex == -1 ? endIndex : findIndex; |
|||
} |
|||
|
|||
public string Format(object[] values) |
|||
{ |
|||
if (values != null) |
|||
{ |
|||
for (int i = 0; i < values.Length; i++) |
|||
{ |
|||
values[i] = FormatArgument(values[i]); |
|||
} |
|||
} |
|||
|
|||
return string.Format(CultureInfo.InvariantCulture, _format, values ?? EmptyArray); |
|||
} |
|||
|
|||
internal string Format() |
|||
{ |
|||
return _format; |
|||
} |
|||
|
|||
internal string Format(object arg0) |
|||
{ |
|||
return string.Format(CultureInfo.InvariantCulture, _format, FormatArgument(arg0)); |
|||
} |
|||
|
|||
internal string Format(object arg0, object arg1) |
|||
{ |
|||
return string.Format(CultureInfo.InvariantCulture, _format, FormatArgument(arg0), FormatArgument(arg1)); |
|||
} |
|||
|
|||
internal string Format(object arg0, object arg1, object arg2) |
|||
{ |
|||
return string.Format(CultureInfo.InvariantCulture, _format, FormatArgument(arg0), FormatArgument(arg1), FormatArgument(arg2)); |
|||
} |
|||
|
|||
public KeyValuePair<string, object> GetValue(object[] values, int index) |
|||
{ |
|||
if (index < 0 || index > _valueNames.Count) |
|||
{ |
|||
throw new IndexOutOfRangeException(nameof(index)); |
|||
} |
|||
|
|||
if (_valueNames.Count > index) |
|||
{ |
|||
return new KeyValuePair<string, object>(_valueNames[index], values[index]); |
|||
} |
|||
|
|||
return new KeyValuePair<string, object>("{OriginalFormat}", OriginalFormat); |
|||
} |
|||
|
|||
public IEnumerable<KeyValuePair<string, object>> GetValues(object[] values) |
|||
{ |
|||
var valueArray = new KeyValuePair<string, object>[values.Length + 1]; |
|||
for (var index = 0; index != _valueNames.Count; ++index) |
|||
{ |
|||
valueArray[index] = new KeyValuePair<string, object>(_valueNames[index], values[index]); |
|||
} |
|||
|
|||
valueArray[valueArray.Length - 1] = new KeyValuePair<string, object>("{OriginalFormat}", OriginalFormat); |
|||
return valueArray; |
|||
} |
|||
|
|||
private object FormatArgument(object value) |
|||
{ |
|||
if (value == null) |
|||
{ |
|||
return NullValue; |
|||
} |
|||
|
|||
// since 'string' implements IEnumerable, special case it
|
|||
if (value is string) |
|||
{ |
|||
return value; |
|||
} |
|||
|
|||
// if the value implements IEnumerable, build a comma separated string.
|
|||
var enumerable = value as IEnumerable; |
|||
if (enumerable != null) |
|||
{ |
|||
return string.Join(", ", enumerable.Cast<object>().Select(o => o ?? NullValue)); |
|||
} |
|||
|
|||
return value; |
|||
} |
|||
|
|||
} |
|||
} |
|||
@ -0,0 +1,42 @@ |
|||
using System; |
|||
using System.Collections; |
|||
using System.Runtime.Serialization; |
|||
using System.Text.Json; |
|||
|
|||
namespace Micronetes.Hosting.Diagnostics.Logging |
|||
{ |
|||
internal class LoggerException : Exception |
|||
{ |
|||
private readonly JsonElement _exceptionMessage; |
|||
|
|||
public LoggerException(JsonElement exceptionMessage) |
|||
{ |
|||
_exceptionMessage = exceptionMessage; |
|||
} |
|||
|
|||
public override void GetObjectData(SerializationInfo info, StreamingContext context) |
|||
{ |
|||
info.AddValue("ClassName", _exceptionMessage.GetProperty("TypeName").GetString(), typeof(string)); // Do not rename (binary serialization)
|
|||
info.AddValue("Message", Message, typeof(string)); // Do not rename (binary serialization)
|
|||
info.AddValue("Data", Data, typeof(IDictionary)); // Do not rename (binary serialization)
|
|||
info.AddValue("InnerException", null, typeof(Exception)); // Do not rename (binary serialization)
|
|||
info.AddValue("HelpURL", null, typeof(string)); // Do not rename (binary serialization)
|
|||
info.AddValue("StackTraceString", StackTrace, typeof(string)); // Do not rename (binary serialization)
|
|||
info.AddValue("RemoteStackTraceString", StackTrace, typeof(string)); // Do not rename (binary serialization)
|
|||
info.AddValue("RemoteStackIndex", 0, typeof(int)); // Do not rename (binary serialization)
|
|||
info.AddValue("ExceptionMethod", null, typeof(string)); // Do not rename (binary serialization)
|
|||
info.AddValue("HResult", int.Parse(_exceptionMessage.GetProperty("HResult").GetString())); // Do not rename (binary serialization)
|
|||
info.AddValue("Source", Source, typeof(string)); // Do not rename (binary serialization
|
|||
info.AddValue("WatsonBuckets", null, typeof(byte[])); // Do not rename (binary serialization)
|
|||
} |
|||
|
|||
public override string Message => _exceptionMessage.GetProperty("Message").GetString(); |
|||
|
|||
public override string StackTrace => _exceptionMessage.GetProperty("VerboseMessage").GetString(); |
|||
|
|||
public override string ToString() |
|||
{ |
|||
return _exceptionMessage.GetProperty("VerboseMessage").GetString(); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,28 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Text; |
|||
|
|||
namespace Micronetes.Hosting.Diagnostics.Metrics |
|||
{ |
|||
internal class CounterPayload : ICounterPayload |
|||
{ |
|||
public CounterPayload(IDictionary<string, object> payloadFields) |
|||
{ |
|||
Name = payloadFields["Name"].ToString(); |
|||
Value = payloadFields["Mean"].ToString(); |
|||
} |
|||
|
|||
public string Name { get; } |
|||
public string Value { get; } |
|||
|
|||
public static ICounterPayload FromPayload(IDictionary<string, object> eventPayload) |
|||
{ |
|||
if (eventPayload.ContainsKey("CounterType")) |
|||
{ |
|||
return eventPayload["CounterType"].Equals("Sum") ? (ICounterPayload)new IncrementingCounterPayload(eventPayload) : (ICounterPayload)new CounterPayload(eventPayload); |
|||
} |
|||
|
|||
return eventPayload.Count == 6 ? (ICounterPayload)new IncrementingCounterPayload(eventPayload) : (ICounterPayload)new CounterPayload(eventPayload); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,12 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Text; |
|||
|
|||
namespace Micronetes.Hosting.Diagnostics.Metrics |
|||
{ |
|||
internal interface ICounterPayload |
|||
{ |
|||
public string Name { get; } |
|||
public string Value { get; } |
|||
} |
|||
} |
|||
@ -0,0 +1,18 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Text; |
|||
|
|||
namespace Micronetes.Hosting.Diagnostics.Metrics |
|||
{ |
|||
internal class IncrementingCounterPayload : ICounterPayload |
|||
{ |
|||
public IncrementingCounterPayload(IDictionary<string, object> payloadFields) |
|||
{ |
|||
Name = payloadFields["Name"].ToString(); |
|||
Value = payloadFields["Increment"].ToString(); |
|||
} |
|||
|
|||
public string Name { get; } |
|||
public string Value { get; } |
|||
} |
|||
} |
|||
@ -0,0 +1,22 @@ |
|||
<Project Sdk="Microsoft.NET.Sdk"> |
|||
|
|||
<PropertyGroup> |
|||
<TargetFramework>netcoreapp3.1</TargetFramework> |
|||
<Description>Diagnostics collector and exporter for .NET Core applications.</Description> |
|||
<Nullable>disable</Nullable> |
|||
</PropertyGroup> |
|||
|
|||
<ItemGroup> |
|||
<PackageReference Include="Microsoft.Extensions.Logging" Version="3.1.1" /> |
|||
<PackageReference Include="Microsoft.Extensions.Logging.ApplicationInsights" Version="2.12.0" /> |
|||
<PackageReference Include="Serilog.Extensions.Logging" Version="3.0.0" /> |
|||
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" /> |
|||
<PackageReference Include="Serilog.Sinks.Elasticsearch" Version="8.0.1" /> |
|||
<PackageReference Include="Serilog.Sinks.Seq" Version="4.0.0" /> |
|||
<PackageReference Include="Microsoft.Diagnostics.Tracing.TraceEvent" Version="2.0.49" GeneratePathProperty="true" /> |
|||
<PackageReference Include="Microsoft.Diagnostics.NETCore.Client" Version="0.2.61701" /> |
|||
<PackageReference Include="OpenTelemetry" Version="0.2.0-alpha.173" /> |
|||
<PackageReference Include="OpenTelemetry.Exporter.Zipkin" Version="0.2.0-alpha.173" /> |
|||
</ItemGroup> |
|||
|
|||
</Project> |
|||
@ -0,0 +1,11 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Text; |
|||
|
|||
namespace Micronetes.Hosting.Runtime |
|||
{ |
|||
// This exists solely as a way to locate this dll
|
|||
public class HostingRuntimeHelpers |
|||
{ |
|||
} |
|||
} |
|||
@ -0,0 +1,9 @@ |
|||
<Project Sdk="Microsoft.NET.Sdk"> |
|||
|
|||
<PropertyGroup> |
|||
<TargetFramework>netstandard2.0</TargetFramework> |
|||
<Description>Runtime helpers for m8s host platform</Description> |
|||
<Nullable>disable</Nullable> |
|||
</PropertyGroup> |
|||
|
|||
</Project> |
|||
@ -0,0 +1,24 @@ |
|||
using System; |
|||
using System.Diagnostics; |
|||
using System.Threading; |
|||
/// <summary>
|
|||
/// Startup hooks are pieces of code that will run before a users program main executes
|
|||
/// See: https://github.com/dotnet/core-setup/blob/master/Documentation/design-docs/host-startup-hook.md
|
|||
/// The type must be named StartupHook without any namespace, and should be internal.
|
|||
/// </summary>
|
|||
internal class StartupHook |
|||
{ |
|||
/// <summary>
|
|||
/// Startup hooks are pieces of code that will run before a users program main executes
|
|||
/// See: https://github.com/dotnet/core-setup/blob/master/Documentation/design-docs/host-startup-hook.md
|
|||
/// </summary>
|
|||
public static void Initialize() |
|||
{ |
|||
Console.WriteLine("Waiting for debugger to attach..."); |
|||
|
|||
while (!Debugger.IsAttached) |
|||
{ |
|||
Thread.Sleep(1000); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,34 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Threading.Tasks; |
|||
using Micronetes.Hosting.Model; |
|||
|
|||
namespace Micronetes.Hosting |
|||
{ |
|||
public class AggregateApplicationProcessor : IApplicationProcessor |
|||
{ |
|||
private readonly IEnumerable<IApplicationProcessor> _applicationProcessors; |
|||
public AggregateApplicationProcessor(IEnumerable<IApplicationProcessor> applicationProcessors) |
|||
{ |
|||
_applicationProcessors = applicationProcessors; |
|||
} |
|||
|
|||
public async Task StartAsync(Application application) |
|||
{ |
|||
foreach (var processor in _applicationProcessors) |
|||
{ |
|||
await processor.StartAsync(application); |
|||
} |
|||
} |
|||
|
|||
public async Task StopAsync(Application application) |
|||
{ |
|||
// Shutdown in the opposite order
|
|||
foreach (var processor in _applicationProcessors.Reverse()) |
|||
{ |
|||
await processor.StopAsync(application); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,10 @@ |
|||
<Router AppAssembly="@typeof(MicronetesHost).Assembly"> |
|||
<Found Context="routeData"> |
|||
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" /> |
|||
</Found> |
|||
<NotFound> |
|||
<LayoutView Layout="@typeof(MainLayout)"> |
|||
<p>Sorry, there's nothing at this address.</p> |
|||
</LayoutView> |
|||
</NotFound> |
|||
</Router> |
|||
@ -0,0 +1,104 @@ |
|||
@page "/" |
|||
@inject Application application |
|||
@implements IDisposable |
|||
|
|||
<h1>Services</h1> |
|||
<table class="table"> |
|||
<thead> |
|||
<tr> |
|||
<th>Name</th> |
|||
<th>Type</th> |
|||
<th>Source</th> |
|||
<th>Bindings</th> |
|||
<th>Replicas</th> |
|||
<th>Restarts</th> |
|||
<th>Logs</th> |
|||
</tr> |
|||
</thead> |
|||
<tbody> |
|||
@foreach (var service in application.Services.Values) |
|||
{ |
|||
var logsPath = $"logs/{service.Description.Name}"; |
|||
var servicePath = $"services/{service.Description.Name}"; |
|||
<tr @key="service.Description.Name"> |
|||
<td><a href="@servicePath">@service.Description.Name</a></td> |
|||
<td> |
|||
@service.ServiceType |
|||
</td> |
|||
<td> |
|||
@if (service.Description.Project != null) |
|||
{ |
|||
<p>@service.Description.Project</p> |
|||
} |
|||
else if (service.Description.DockerImage != null) |
|||
{ |
|||
<p>@service.Description.DockerImage</p> |
|||
} |
|||
</td> |
|||
<td> |
|||
@if (service.Description.Bindings.Any()) |
|||
{ |
|||
foreach (var b in service.Description.Bindings) |
|||
{ |
|||
if (b.Port != null) |
|||
{ |
|||
if (b.Protocol == null || b.Protocol == "http" || b.Protocol == "https") |
|||
{ |
|||
var url = GetUrl(b); |
|||
<span><a href="@url" target="_blank">@url</a></span> |
|||
} |
|||
else |
|||
{ |
|||
<span>@GetUrl(b)</span> |
|||
} |
|||
} |
|||
else |
|||
{ |
|||
<span>@b.ConnectionString</span> |
|||
} |
|||
} |
|||
} |
|||
else |
|||
{ |
|||
<p>none</p> |
|||
} |
|||
</td> |
|||
<td>@service.Replicas.Count/@service.Description.Replicas</td> |
|||
<td>@service.Restarts</td> |
|||
<td> |
|||
<NavLink href="@logsPath">View</NavLink> |
|||
</td> |
|||
</tr> |
|||
} |
|||
</tbody> |
|||
</table> |
|||
|
|||
@code { |
|||
|
|||
private List<IDisposable> _subscriptions = new List<IDisposable>(); |
|||
|
|||
string GetUrl(ServiceBinding b) |
|||
{ |
|||
return $"{(b.Protocol ?? "http")}://{b.Host ?? "localhost"}:{b.Port}"; |
|||
} |
|||
|
|||
protected override void OnInitialized() |
|||
{ |
|||
foreach (var a in application.Services.Values) |
|||
{ |
|||
_subscriptions.Add(a.ReplicaEvents.Subscribe(OnReplicaChanged)); |
|||
} |
|||
|
|||
base.OnInitialized(); |
|||
} |
|||
|
|||
private void OnReplicaChanged(ReplicaEvent replicaEvent) |
|||
{ |
|||
InvokeAsync(() => StateHasChanged()); |
|||
} |
|||
|
|||
void IDisposable.Dispose() |
|||
{ |
|||
_subscriptions.ForEach(d => d.Dispose()); |
|||
} |
|||
} |
|||
@ -0,0 +1,55 @@ |
|||
@page "/logs/{ServiceName}" |
|||
@inject Application application |
|||
@implements IDisposable |
|||
|
|||
<h3>Logs for @ServiceName</h3> |
|||
|
|||
@if (ApplicationLogs == null) |
|||
{ |
|||
<span>Unknown service @ServiceName</span> |
|||
} |
|||
else |
|||
{ |
|||
<div style="overflow-y: scroll;position: absolute;height: 84%; width:75%; color:white;background-color:black;padding:10px"> |
|||
@foreach (var log in ApplicationLogs) |
|||
{ |
|||
<div @key="@log.Id" style="width:100%">@log.Text</div> |
|||
} |
|||
</div> |
|||
} |
|||
|
|||
@code { |
|||
[Parameter] |
|||
public string ServiceName { get; set; } |
|||
|
|||
public List<(string Text, int Id)> ApplicationLogs { get; set; } |
|||
|
|||
private IDisposable _subscription; |
|||
|
|||
protected override void OnInitialized() |
|||
{ |
|||
// TODO: handle this returning false |
|||
if (application.Services.TryGetValue(ServiceName, out var service)) |
|||
{ |
|||
ApplicationLogs = service.CachedLogs.Select((item, index) => (item, index)).ToList(); |
|||
var count = ApplicationLogs.Count; |
|||
|
|||
_subscription = service.Logs.Subscribe(log => |
|||
{ |
|||
count++; |
|||
InvokeAsync(() => |
|||
{ |
|||
ApplicationLogs.Add((log, count)); |
|||
StateHasChanged(); |
|||
}); |
|||
}); |
|||
} |
|||
|
|||
base.OnInitialized(); |
|||
} |
|||
|
|||
void IDisposable.Dispose() |
|||
{ |
|||
_subscription?.Dispose(); |
|||
} |
|||
} |
|||
@ -0,0 +1,35 @@ |
|||
@page "/services/{ServiceName}" |
|||
@inject Application application |
|||
|
|||
@if (Service == null) |
|||
{ |
|||
<span>Unknown service @ServiceName</span> |
|||
} |
|||
else |
|||
{ |
|||
<h3>@Service.Description.Name</h3> |
|||
|
|||
switch (Service.ServiceType) |
|||
{ |
|||
case ServiceType.Container: |
|||
break; |
|||
case ServiceType.Executable: |
|||
break; |
|||
case ServiceType.Project: |
|||
break; |
|||
} |
|||
} |
|||
|
|||
@code { |
|||
private Service _service; |
|||
|
|||
[Parameter] |
|||
public string ServiceName { get; set; } |
|||
|
|||
public Service Service => _service; |
|||
|
|||
protected override void OnInitialized() |
|||
{ |
|||
application.Services.TryGetValue(ServiceName, out _service); |
|||
} |
|||
} |
|||
@ -0,0 +1,36 @@ |
|||
@page "/" |
|||
@namespace Micronetes.Hosting.Dashboard.Pages |
|||
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers |
|||
@{ |
|||
Layout = null; |
|||
} |
|||
|
|||
<!DOCTYPE html> |
|||
<html lang="en"> |
|||
<head> |
|||
<meta charset="utf-8" /> |
|||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> |
|||
<title>Micronetes Dashboard</title> |
|||
<base href="~/" /> |
|||
<link rel="stylesheet" href="css/bootstrap/bootstrap.min.css" /> |
|||
<link href="css/site.css" rel="stylesheet" /> |
|||
</head> |
|||
<body> |
|||
<app> |
|||
<component type="typeof(App)" render-mode="ServerPrerendered" /> |
|||
</app> |
|||
|
|||
<div id="blazor-error-ui"> |
|||
<environment include="Staging,Production"> |
|||
An error has occurred. This application may no longer respond until reloaded. |
|||
</environment> |
|||
<environment include="Development"> |
|||
An unhandled exception has occurred. See browser dev tools for details. |
|||
</environment> |
|||
<a href="" class="reload">Reload</a> |
|||
<a class="dismiss">🗙</a> |
|||
</div> |
|||
|
|||
<script src="_framework/blazor.server.js"></script> |
|||
</body> |
|||
</html> |
|||
@ -0,0 +1,15 @@ |
|||
@inherits LayoutComponentBase |
|||
|
|||
<div class="sidebar"> |
|||
<NavMenu /> |
|||
</div> |
|||
|
|||
<div class="main"> |
|||
<div class="top-row top-nav-item nav-item px-4"> |
|||
<a href="https://github.com/davidfowl/Micronetes" target="_blank">About</a> |
|||
</div> |
|||
|
|||
<div class="content px-4"> |
|||
@Body |
|||
</div> |
|||
</div> |
|||
@ -0,0 +1,30 @@ |
|||
<div class="top-row pl-4 navbar navbar-dark"> |
|||
<a class="navbar-brand" href=""> |
|||
<img src="img/circle-icons-shipwheel.svg" width="30" height="30" class="d-inline-block align-top" alt="Ship Wheel"> |
|||
Micronetes Dashboard |
|||
</a> |
|||
<button class="navbar-toggler" @onclick="ToggleNavMenu"> |
|||
<span class="navbar-toggler-icon"></span> |
|||
</button> |
|||
</div> |
|||
|
|||
<div class="@NavMenuCssClass" @onclick="ToggleNavMenu"> |
|||
<ul class="nav flex-column"> |
|||
<li class="nav-item px-3"> |
|||
<NavLink class="nav-link" href="" Match="NavLinkMatch.All"> |
|||
<span class="oi oi-home" aria-hidden="true"></span> Home |
|||
</NavLink> |
|||
</li> |
|||
</ul> |
|||
</div> |
|||
|
|||
@code { |
|||
private bool collapseNavMenu = true; |
|||
|
|||
private string NavMenuCssClass => collapseNavMenu ? "collapse" : null; |
|||
|
|||
private void ToggleNavMenu() |
|||
{ |
|||
collapseNavMenu = !collapseNavMenu; |
|||
} |
|||
} |
|||
@ -0,0 +1,11 @@ |
|||
@using System.Net.Http |
|||
@using Microsoft.AspNetCore.Authorization |
|||
@using Microsoft.AspNetCore.Components.Authorization |
|||
@using Microsoft.AspNetCore.Components.Forms |
|||
@using Microsoft.AspNetCore.Components.Routing |
|||
@using Microsoft.AspNetCore.Components.Web |
|||
@using Microsoft.JSInterop |
|||
@using Micronetes.Hosting |
|||
@using Micronetes.Hosting.Dashboard.Shared |
|||
@using Micronetes.Hosting.Dashboard.Pages |
|||
@using Micronetes.Hosting.Model |
|||
@ -0,0 +1,272 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Threading; |
|||
using System.Threading.Tasks; |
|||
using Micronetes.Hosting.Model; |
|||
using Microsoft.Extensions.Logging; |
|||
|
|||
namespace Micronetes.Hosting |
|||
{ |
|||
public class DockerRunner : IApplicationProcessor |
|||
{ |
|||
private readonly ILogger _logger; |
|||
private readonly Lazy<Task<bool>> _dockerInstalled = new Lazy<Task<bool>>(DetectDockerInstalled); |
|||
|
|||
public DockerRunner(ILogger logger) |
|||
{ |
|||
_logger = logger; |
|||
} |
|||
|
|||
public Task StartAsync(Application application) |
|||
{ |
|||
var tasks = new Task[application.Services.Count]; |
|||
var index = 0; |
|||
foreach (var s in application.Services) |
|||
{ |
|||
tasks[index++] = s.Value.Description.External ? Task.CompletedTask : StartContainerAsync(application, s.Value); |
|||
} |
|||
|
|||
return Task.WhenAll(tasks); |
|||
} |
|||
|
|||
public Task StopAsync(Application application) |
|||
{ |
|||
var services = application.Services; |
|||
|
|||
var index = 0; |
|||
var tasks = new Task[services.Count]; |
|||
foreach (var s in services.Values) |
|||
{ |
|||
var state = s; |
|||
tasks[index++] = StopContainerAsync(state); |
|||
} |
|||
|
|||
return Task.WhenAll(tasks); |
|||
} |
|||
|
|||
private async Task StartContainerAsync(Application application, Service service) |
|||
{ |
|||
if (service.Description.DockerImage == null) |
|||
{ |
|||
return; |
|||
} |
|||
|
|||
if (!await _dockerInstalled.Value) |
|||
{ |
|||
_logger.LogError("Unable to start docker container for service {ServiceName}, Docker is not installed.", service.Description.Name); |
|||
|
|||
service.Logs.OnNext($"Unable to start docker container for service {service.Description.Name}, Docker is not installed."); |
|||
return; |
|||
} |
|||
|
|||
var serviceDescription = service.Description; |
|||
var environmentArguments = ""; |
|||
|
|||
var dockerInfo = new DockerInformation() |
|||
{ |
|||
Tasks = new Task[service.Description.Replicas.Value] |
|||
}; |
|||
|
|||
async Task RunDockerContainer(IEnumerable<(int Port, int? InternalPort, int BindingPort, string Protocol)> ports) |
|||
{ |
|||
var hasPorts = ports.Any(); |
|||
|
|||
var replica = service.Description.Name.ToLower() + "_" + Guid.NewGuid().ToString().Substring(0, 10).ToLower(); |
|||
var status = new DockerStatus(service, replica); |
|||
service.Replicas[replica] = status; |
|||
|
|||
service.ReplicaEvents.OnNext(new ReplicaEvent(ReplicaState.Added, status)); |
|||
|
|||
var environment = new Dictionary<string, string> |
|||
{ |
|||
// Default to development environment
|
|||
["DOTNET_ENVIRONMENT"] = "Development", |
|||
// Remove the color codes from the console output
|
|||
["DOTNET_LOGGING__CONSOLE__DISABLECOLORS"] = "true" |
|||
}; |
|||
|
|||
var portString = ""; |
|||
|
|||
if (hasPorts) |
|||
{ |
|||
status.Ports = ports.Select(p => p.Port); |
|||
|
|||
portString = string.Join(" ", ports.Select(p => $"-p {p.Port}:{p.InternalPort ?? p.Port}")); |
|||
|
|||
foreach (var p in ports) |
|||
{ |
|||
environment[$"{p.Protocol?.ToUpper() ?? "HTTP"}_PORT"] = p.BindingPort.ToString(); |
|||
} |
|||
} |
|||
|
|||
application.PopulateEnvironment(service, (key, value) => environment[key] = value, "host.docker.internal"); |
|||
|
|||
environment["APP_INSTANCE"] = replica; |
|||
|
|||
foreach (var pair in environment) |
|||
{ |
|||
environmentArguments += $"-e {pair.Key}={pair.Value} "; |
|||
} |
|||
|
|||
var command = $"run -d {environmentArguments} {portString} --name {replica} --restart=unless-stopped {service.Description.DockerImage} {service.Description.Args ?? ""}"; |
|||
_logger.LogInformation("Running docker command {Command}", command); |
|||
|
|||
service.Logs.OnNext($"[{replica}]: {command}"); |
|||
|
|||
status.DockerCommand = command; |
|||
|
|||
var result = await ProcessUtil.RunAsync( |
|||
"docker", |
|||
command, |
|||
throwOnError: false, |
|||
cancellationToken: dockerInfo.StoppingTokenSource.Token, |
|||
outputDataReceived: data => service.Logs.OnNext($"[{replica}]: {data}")); |
|||
|
|||
if (result.ExitCode != 0) |
|||
{ |
|||
_logger.LogError("docker run failed for {ServiceName} with exit code {ExitCode}:" + result.StandardError, service.Description.Name, result.ExitCode); |
|||
service.Replicas.TryRemove(replica, out _); |
|||
service.ReplicaEvents.OnNext(new ReplicaEvent(ReplicaState.Removed, status)); |
|||
|
|||
PrintStdOutAndErr(service, replica, result); |
|||
return; |
|||
} |
|||
|
|||
var containerId = result.StandardOutput.Trim(); |
|||
|
|||
// There's a race condition that sometimes makes us miss the output
|
|||
// so keep trying to get the container id
|
|||
while (string.IsNullOrEmpty(containerId)) |
|||
{ |
|||
// Try to get the ID of the container
|
|||
result = await ProcessUtil.RunAsync("docker", $"ps --no-trunc -f name={replica} --format " + "{{.ID}}"); |
|||
|
|||
containerId = result.ExitCode == 0 ? result.StandardOutput.Trim() : null; |
|||
} |
|||
|
|||
var shortContainerId = containerId.Substring(0, Math.Min(12, containerId.Length)); |
|||
|
|||
status.ContainerId = shortContainerId; |
|||
|
|||
_logger.LogInformation("Running container {ContainerName} with ID {ContainerId}", replica, shortContainerId); |
|||
|
|||
service.ReplicaEvents.OnNext(new ReplicaEvent(ReplicaState.Started, status)); |
|||
|
|||
_logger.LogInformation("Collecting docker logs for {ContainerName}.", replica); |
|||
|
|||
await ProcessUtil.RunAsync("docker", $"logs -f {containerId}", |
|||
outputDataReceived: data => service.Logs.OnNext($"[{replica}]: {data}"), |
|||
onStart: pid => |
|||
{ |
|||
status.DockerLogsPid = pid; |
|||
}, |
|||
throwOnError: false, |
|||
cancellationToken: dockerInfo.StoppingTokenSource.Token); |
|||
|
|||
_logger.LogInformation("docker logs collection for {ContainerName} complete with exit code {ExitCode}", replica, result.ExitCode); |
|||
|
|||
// Docker has a tendency to hang so we're going to timeout this shutdown process
|
|||
var timeoutCts = new CancellationTokenSource(TimeSpan.FromSeconds(5)); |
|||
|
|||
_logger.LogInformation("Stopping container {ContainerName} with ID {ContainerId}", replica, shortContainerId); |
|||
|
|||
result = await ProcessUtil.RunAsync("docker", $"stop {containerId}", throwOnError: false, cancellationToken: timeoutCts.Token); |
|||
|
|||
PrintStdOutAndErr(service, replica, result); |
|||
|
|||
service.ReplicaEvents.OnNext(new ReplicaEvent(ReplicaState.Stopped, status)); |
|||
|
|||
_logger.LogInformation("Stopped container {ContainerName} with ID {ContainerId} exited with {ExitCode}", replica, shortContainerId, result.ExitCode); |
|||
|
|||
result = await ProcessUtil.RunAsync("docker", $"rm {containerId}", throwOnError: false, cancellationToken: timeoutCts.Token); |
|||
|
|||
PrintStdOutAndErr(service, replica, result); |
|||
|
|||
_logger.LogInformation("Removed container {ContainerName} with ID {ContainerId} exited with {ExitCode}", replica, shortContainerId, result.ExitCode); |
|||
|
|||
service.Replicas.TryRemove(replica, out _); |
|||
|
|||
service.ReplicaEvents.OnNext(new ReplicaEvent(ReplicaState.Removed, status)); |
|||
}; |
|||
|
|||
if (serviceDescription.Bindings.Count > 0) |
|||
{ |
|||
// Each replica is assigned a list of internal ports, one mapped to each external
|
|||
// port
|
|||
for (int i = 0; i < serviceDescription.Replicas; i++) |
|||
{ |
|||
var ports = new List<(int, int?, int, string)>(); |
|||
foreach (var binding in serviceDescription.Bindings) |
|||
{ |
|||
if (binding.Port == null) |
|||
{ |
|||
continue; |
|||
} |
|||
|
|||
ports.Add((service.PortMap[binding.Port.Value][i], binding.InternalPort, binding.Port.Value, binding.Protocol)); |
|||
} |
|||
|
|||
dockerInfo.Tasks[i] = RunDockerContainer(ports); |
|||
} |
|||
} |
|||
else |
|||
{ |
|||
for (int i = 0; i < service.Description.Replicas; i++) |
|||
{ |
|||
dockerInfo.Tasks[i] = RunDockerContainer(Enumerable.Empty<(int, int?, int, string)>()); |
|||
} |
|||
} |
|||
|
|||
service.Items[typeof(DockerInformation)] = dockerInfo; |
|||
} |
|||
|
|||
private static void PrintStdOutAndErr(Service service, string replica, ProcessResult result) |
|||
{ |
|||
if (result.ExitCode != 0) |
|||
{ |
|||
if (result.StandardOutput != null) |
|||
{ |
|||
service.Logs.OnNext($"[{replica}]: {result.StandardOutput}"); |
|||
} |
|||
|
|||
if (result.StandardError != null) |
|||
{ |
|||
service.Logs.OnNext($"[{replica}]: {result.StandardError}"); |
|||
} |
|||
} |
|||
} |
|||
|
|||
private async Task StopContainerAsync(Service service) |
|||
{ |
|||
if (service.Items.TryGetValue(typeof(DockerInformation), out var value) && value is DockerInformation di) |
|||
{ |
|||
di.StoppingTokenSource.Cancel(); |
|||
|
|||
await Task.WhenAll(di.Tasks); |
|||
} |
|||
} |
|||
|
|||
private static async Task<bool> DetectDockerInstalled() |
|||
{ |
|||
// Detect Docker installation
|
|||
try |
|||
{ |
|||
await ProcessUtil.RunAsync("docker", "version", throwOnError: false); |
|||
return true; |
|||
} |
|||
catch (Exception) |
|||
{ |
|||
// Unfortunately, process throws
|
|||
return false; |
|||
} |
|||
} |
|||
|
|||
|
|||
private class DockerInformation |
|||
{ |
|||
public Task[] Tasks { get; set; } |
|||
public CancellationTokenSource StoppingTokenSource { get; set; } = new CancellationTokenSource(); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,111 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.IO; |
|||
using System.Linq; |
|||
using System.Threading; |
|||
using System.Threading.Tasks; |
|||
using Micronetes.Hosting.Diagnostics; |
|||
using Micronetes.Hosting.Model; |
|||
using Microsoft.Extensions.Logging; |
|||
|
|||
namespace Micronetes.Hosting |
|||
{ |
|||
public class EventPipeDiagnosticsRunner : IApplicationProcessor |
|||
{ |
|||
private readonly ILogger _logger; |
|||
private readonly DiagnosticsCollector _diagnosticsCollector; |
|||
|
|||
public EventPipeDiagnosticsRunner(ILogger logger, DiagnosticsCollector diagnosticsCollector) |
|||
{ |
|||
_logger = logger; |
|||
_diagnosticsCollector = diagnosticsCollector; |
|||
} |
|||
|
|||
public Task StartAsync(Application application) |
|||
{ |
|||
foreach (var service in application.Services.Values) |
|||
{ |
|||
if (service.Description.External) |
|||
{ |
|||
continue; |
|||
} |
|||
|
|||
service.Items[typeof(Subscription)] = service.ReplicaEvents.Subscribe(OnReplicaChanged); |
|||
} |
|||
|
|||
return Task.CompletedTask; |
|||
} |
|||
|
|||
public Task StopAsync(Application application) |
|||
{ |
|||
foreach (var service in application.Services.Values) |
|||
{ |
|||
if (service.Items.TryGetValue(typeof(Subscription), out var item) && item is IDisposable disposable) |
|||
{ |
|||
disposable.Dispose(); |
|||
} |
|||
} |
|||
|
|||
return Task.CompletedTask; |
|||
} |
|||
|
|||
private void OnReplicaChanged(ReplicaEvent replicaEvent) |
|||
{ |
|||
var replica = replicaEvent.Replica; |
|||
|
|||
if (!(replica is ProcessStatus process)) |
|||
{ |
|||
// This temporarily only works for processes launched
|
|||
return; |
|||
} |
|||
|
|||
switch (replicaEvent.State) |
|||
{ |
|||
case ReplicaState.Started: |
|||
{ |
|||
var cts = new CancellationTokenSource(); |
|||
var state = new DiagnosticsState |
|||
{ |
|||
StoppingTokenSource = cts, |
|||
Thread = new Thread(() => |
|||
{ |
|||
// TODO: Finding the application name requires msbuild knowledge
|
|||
_diagnosticsCollector.ProcessEvents(Path.GetFileNameWithoutExtension(process.Service.Status.ProjectFilePath), |
|||
process.Service.Description.Name, |
|||
process.Pid.Value, |
|||
replica.Name, |
|||
replica.Metrics, |
|||
cts.Token); |
|||
}) |
|||
}; |
|||
|
|||
replica.Items[typeof(DiagnosticsState)] = state; |
|||
|
|||
state.Thread.Start(); |
|||
} |
|||
|
|||
break; |
|||
case ReplicaState.Stopped: |
|||
{ |
|||
if (replica.Items.TryGetValue(typeof(DiagnosticsState), out var item) && item is DiagnosticsState state) |
|||
{ |
|||
state.StoppingTokenSource.Cancel(); |
|||
|
|||
state.Thread.Join(); |
|||
} |
|||
} |
|||
break; |
|||
default: |
|||
break; |
|||
} |
|||
} |
|||
|
|||
// Used a lookup key for state
|
|||
private class Subscription { } |
|||
private class DiagnosticsState |
|||
{ |
|||
public Thread Thread { get; set; } |
|||
public CancellationTokenSource StoppingTokenSource { get; set; } = new CancellationTokenSource(); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,12 @@ |
|||
using System.Threading.Tasks; |
|||
using Micronetes.Hosting.Model; |
|||
|
|||
namespace Micronetes.Hosting |
|||
{ |
|||
public interface IApplicationProcessor |
|||
{ |
|||
Task StartAsync(Application application); |
|||
|
|||
Task StopAsync(Application application); |
|||
} |
|||
} |
|||
@ -0,0 +1,16 @@ |
|||
namespace Micronetes.Hosting |
|||
{ |
|||
internal class ProcessResult |
|||
{ |
|||
public ProcessResult(string standardOutput, string standardError, int exitCode) |
|||
{ |
|||
StandardOutput = standardOutput; |
|||
StandardError = standardError; |
|||
ExitCode = exitCode; |
|||
} |
|||
|
|||
public string StandardOutput { get; } |
|||
public string StandardError { get; } |
|||
public int ExitCode { get; } |
|||
} |
|||
} |
|||
@ -0,0 +1,141 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Diagnostics; |
|||
using System.Runtime.InteropServices; |
|||
using System.Text; |
|||
using System.Threading; |
|||
using System.Threading.Tasks; |
|||
using Mono.Unix.Native; |
|||
|
|||
namespace Micronetes.Hosting |
|||
{ |
|||
internal static class ProcessUtil |
|||
{ |
|||
public static async Task<ProcessResult> RunAsync( |
|||
string filename, |
|||
string arguments, |
|||
string workingDirectory = null, |
|||
bool throwOnError = true, |
|||
IDictionary<string, string> environmentVariables = null, |
|||
Action<string> outputDataReceived = null, |
|||
Action<string> errorDataReceived = null, |
|||
Action<int> onStart = null, |
|||
CancellationToken cancellationToken = default) |
|||
{ |
|||
using var process = new Process() |
|||
{ |
|||
StartInfo = |
|||
{ |
|||
FileName = filename, |
|||
Arguments = arguments, |
|||
RedirectStandardOutput = true, |
|||
RedirectStandardError = true, |
|||
UseShellExecute = false, |
|||
CreateNoWindow = true, |
|||
}, |
|||
EnableRaisingEvents = true |
|||
}; |
|||
|
|||
|
|||
if (workingDirectory != null) |
|||
{ |
|||
process.StartInfo.WorkingDirectory = workingDirectory; |
|||
} |
|||
|
|||
if (environmentVariables != null) |
|||
{ |
|||
foreach (var kvp in environmentVariables) |
|||
{ |
|||
process.StartInfo.Environment.Add(kvp); |
|||
} |
|||
} |
|||
|
|||
var outputBuilder = new StringBuilder(); |
|||
process.OutputDataReceived += (_, e) => |
|||
{ |
|||
if (e.Data != null) |
|||
{ |
|||
if (outputDataReceived != null) |
|||
{ |
|||
outputDataReceived.Invoke(e.Data); |
|||
} |
|||
else |
|||
{ |
|||
outputBuilder.AppendLine(e.Data); |
|||
} |
|||
} |
|||
}; |
|||
|
|||
var errorBuilder = new StringBuilder(); |
|||
process.ErrorDataReceived += (_, e) => |
|||
{ |
|||
if (e.Data != null) |
|||
{ |
|||
if (errorDataReceived != null) |
|||
{ |
|||
errorDataReceived.Invoke(e.Data); |
|||
} |
|||
else if (outputDataReceived != null) |
|||
{ |
|||
outputDataReceived.Invoke(e.Data); |
|||
} |
|||
else |
|||
{ |
|||
errorBuilder.AppendLine(e.Data); |
|||
} |
|||
} |
|||
}; |
|||
|
|||
var processLifetimeTask = new TaskCompletionSource<ProcessResult>(); |
|||
|
|||
process.Exited += (_, e) => |
|||
{ |
|||
if (throwOnError && process.ExitCode != 0) |
|||
{ |
|||
processLifetimeTask.TrySetException(new InvalidOperationException($"Command {filename} {arguments} returned exit code {process.ExitCode}")); |
|||
} |
|||
else |
|||
{ |
|||
processLifetimeTask.TrySetResult(new ProcessResult(outputBuilder.ToString(), errorBuilder.ToString(), process.ExitCode)); |
|||
} |
|||
}; |
|||
|
|||
process.Start(); |
|||
onStart?.Invoke(process.Id); |
|||
|
|||
process.BeginOutputReadLine(); |
|||
process.BeginErrorReadLine(); |
|||
|
|||
var cancelledTcs = new TaskCompletionSource<object>(); |
|||
using var _ = cancellationToken.Register(() => cancelledTcs.TrySetResult(null)); |
|||
|
|||
var result = await Task.WhenAny(processLifetimeTask.Task, cancelledTcs.Task); |
|||
|
|||
if (result == cancelledTcs.Task) |
|||
{ |
|||
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) |
|||
{ |
|||
Syscall.kill(process.Id, Signum.SIGINT); |
|||
|
|||
var cancel = new CancellationTokenSource(); |
|||
|
|||
await Task.WhenAny(processLifetimeTask.Task, Task.Delay(TimeSpan.FromSeconds(5), cancel.Token)); |
|||
|
|||
cancel.Cancel(); |
|||
} |
|||
|
|||
if (!process.HasExited) |
|||
{ |
|||
process.CloseMainWindow(); |
|||
|
|||
if (!process.HasExited) |
|||
{ |
|||
process.Kill(); |
|||
} |
|||
} |
|||
} |
|||
|
|||
return await processLifetimeTask.Task; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,33 @@ |
|||
<Project Sdk="Microsoft.NET.Sdk.Web"> |
|||
|
|||
<PropertyGroup> |
|||
<OutputType>Library</OutputType> |
|||
<TargetFramework>netcoreapp3.1</TargetFramework> |
|||
<Description>Orchestration host APIs.</Description> |
|||
<GenerateEmbeddedFilesManifest>true</GenerateEmbeddedFilesManifest> |
|||
<NoDefaultLaunchSettingsFile>true</NoDefaultLaunchSettingsFile> |
|||
<Nullable>disable</Nullable> |
|||
</PropertyGroup> |
|||
|
|||
<ItemGroup> |
|||
<EmbeddedResource Include="wwwroot\**" /> |
|||
</ItemGroup> |
|||
|
|||
<ItemGroup> |
|||
<PackageReference Include="Bedrock.Framework" Version="0.1.38-alpha.gd25d5b37ad" /> |
|||
<PackageReference Include="FeatherHttp" Version="0.1.42-alpha.gf06a8747e7" /> |
|||
<PackageReference Include="Microsoft.Extensions.FileProviders.Embedded" Version="3.1.0" /> |
|||
<PackageReference Include="Mono.Posix.NETStandard" Version="1.0.0" /> |
|||
<PackageReference Include="Serilog.Extensions.Hosting" Version="3.0.0" /> |
|||
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" /> |
|||
<PackageReference Include="Sln" Version="0.3.0" /> |
|||
<PackageReference Include="YamlDotNet" Version="8.0.0" /> |
|||
<PackageReference Include="System.Reactive" Version="4.3.2" /> |
|||
</ItemGroup> |
|||
|
|||
<ItemGroup> |
|||
<ProjectReference Include="..\Micronetes.Hosting.Diagnostics\Micronetes.Hosting.Diagnostics.csproj" /> |
|||
<ProjectReference Include="..\Micronetes.Hosting.Runtime\Micronetes.Hosting.Runtime.csproj" /> |
|||
</ItemGroup> |
|||
|
|||
</Project> |
|||
@ -0,0 +1,173 @@ |
|||
using System.Linq; |
|||
using System.Text; |
|||
using System.Text.Json; |
|||
using System.Threading.Tasks; |
|||
using Micronetes.Hosting.Model; |
|||
using Microsoft.AspNetCore.Builder; |
|||
using Microsoft.AspNetCore.Http; |
|||
using Microsoft.AspNetCore.Routing; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
|
|||
namespace Micronetes.Hosting |
|||
{ |
|||
public class MicronetesApi |
|||
{ |
|||
private readonly JsonSerializerOptions _options; |
|||
|
|||
public MicronetesApi() |
|||
{ |
|||
_options = new JsonSerializerOptions() |
|||
{ |
|||
PropertyNameCaseInsensitive = true, |
|||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase, |
|||
WriteIndented = true, |
|||
}; |
|||
|
|||
_options.Converters.Add(ReplicaStatus.JsonConverter); |
|||
} |
|||
|
|||
public void MapRoutes(IEndpointRouteBuilder endpoints) |
|||
{ |
|||
endpoints.MapGet("/api/v1", ServiceIndex); |
|||
endpoints.MapGet("/api/v1/services", Services); |
|||
endpoints.MapGet("/api/v1/services/{name}", Service); |
|||
endpoints.MapGet("/api/v1/logs/{name}", Logs); |
|||
endpoints.MapGet("/api/v1/metrics", AllMetrics); |
|||
endpoints.MapGet("/api/v1/metrics/{name}", Metics); |
|||
} |
|||
|
|||
private Task ServiceIndex(HttpContext context) |
|||
{ |
|||
context.Response.ContentType = "application/json"; |
|||
return JsonSerializer.SerializeAsync(context.Response.Body, new[] |
|||
{ |
|||
$"{context.Request.Scheme}://{context.Request.Host}/api/v1/services", |
|||
$"{context.Request.Scheme}://{context.Request.Host}/api/v1/logs/{{service}}", |
|||
$"{context.Request.Scheme}://{context.Request.Host}/api/v1/metrics", |
|||
$"{context.Request.Scheme}://{context.Request.Host}/api/v1/metrics/{{service}}", |
|||
}, |
|||
_options); |
|||
} |
|||
|
|||
private async Task Services(HttpContext context) |
|||
{ |
|||
var app = context.RequestServices.GetRequiredService<Application>(); |
|||
|
|||
context.Response.ContentType = "application/json"; |
|||
|
|||
var services = app.Services.OrderBy(s => s.Key).Select(s => s.Value); |
|||
|
|||
await JsonSerializer.SerializeAsync(context.Response.Body, services, _options); |
|||
} |
|||
|
|||
private async Task Service(HttpContext context) |
|||
{ |
|||
var app = context.RequestServices.GetRequiredService<Application>(); |
|||
|
|||
var name = (string)context.Request.RouteValues["name"]; |
|||
context.Response.ContentType = "application/json"; |
|||
|
|||
if (!app.Services.TryGetValue(name, out var service)) |
|||
{ |
|||
context.Response.StatusCode = 404; |
|||
await JsonSerializer.SerializeAsync(context.Response.Body, new |
|||
{ |
|||
message = $"Unknown service {name}" |
|||
}, |
|||
_options); |
|||
|
|||
return; |
|||
} |
|||
|
|||
await JsonSerializer.SerializeAsync(context.Response.Body, service, _options); |
|||
} |
|||
|
|||
private async Task Logs(HttpContext context) |
|||
{ |
|||
var app = context.RequestServices.GetRequiredService<Application>(); |
|||
|
|||
var name = (string)context.Request.RouteValues["name"]; |
|||
context.Response.ContentType = "application/json"; |
|||
|
|||
if (!app.Services.TryGetValue(name, out var service)) |
|||
{ |
|||
context.Response.StatusCode = 404; |
|||
await JsonSerializer.SerializeAsync(context.Response.Body, new |
|||
{ |
|||
message = $"Unknown service {name}" |
|||
}, |
|||
_options); |
|||
|
|||
return; |
|||
} |
|||
|
|||
await JsonSerializer.SerializeAsync(context.Response.Body, service.CachedLogs, _options); |
|||
} |
|||
|
|||
private async Task AllMetrics(HttpContext context) |
|||
{ |
|||
var app = context.RequestServices.GetRequiredService<Application>(); |
|||
|
|||
var sb = new StringBuilder(); |
|||
foreach (var s in app.Services.OrderBy(s => s.Key)) |
|||
{ |
|||
sb.AppendLine($"# {s.Key}"); |
|||
foreach (var replica in s.Value.Replicas) |
|||
{ |
|||
foreach (var metric in replica.Value.Metrics) |
|||
{ |
|||
sb.Append(metric.Key); |
|||
sb.Append("{"); |
|||
sb.Append($"service=\"{s.Key}\","); |
|||
sb.Append($"instance=\"{replica.Key}\""); |
|||
sb.Append("}"); |
|||
sb.Append(" "); |
|||
sb.Append(metric.Value); |
|||
sb.AppendLine(); |
|||
} |
|||
} |
|||
sb.AppendLine(); |
|||
} |
|||
|
|||
await context.Response.WriteAsync(sb.ToString()); |
|||
} |
|||
|
|||
private async Task Metics(HttpContext context) |
|||
{ |
|||
var app = context.RequestServices.GetRequiredService<Application>(); |
|||
|
|||
var sb = new StringBuilder(); |
|||
|
|||
var name = (string)context.Request.RouteValues["name"]; |
|||
context.Response.ContentType = "application/json"; |
|||
|
|||
if (!app.Services.TryGetValue(name, out var service)) |
|||
{ |
|||
context.Response.StatusCode = 404; |
|||
await JsonSerializer.SerializeAsync(context.Response.Body, new |
|||
{ |
|||
message = $"Unknown service {name}" |
|||
}, |
|||
_options); |
|||
|
|||
return; |
|||
} |
|||
|
|||
foreach (var replica in service.Replicas) |
|||
{ |
|||
foreach (var metric in replica.Value.Metrics) |
|||
{ |
|||
sb.Append(metric.Key); |
|||
sb.Append("{"); |
|||
sb.Append($"instance=\"{replica.Key}\""); |
|||
sb.Append("}"); |
|||
sb.Append(" "); |
|||
sb.Append(metric.Value); |
|||
sb.AppendLine(); |
|||
} |
|||
} |
|||
|
|||
await context.Response.WriteAsync(sb.ToString()); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,116 @@ |
|||
using System; |
|||
using System.Linq; |
|||
using System.Threading.Tasks; |
|||
using Micronetes.Hosting.Diagnostics; |
|||
using Micronetes.Hosting.Model; |
|||
using Microsoft.AspNetCore.Builder; |
|||
using Microsoft.Extensions.DependencyInjection; |
|||
using Microsoft.Extensions.FileProviders; |
|||
using Microsoft.Extensions.Logging; |
|||
using Serilog; |
|||
using Serilog.Filters; |
|||
|
|||
namespace Micronetes.Hosting |
|||
{ |
|||
public class MicronetesHost |
|||
{ |
|||
public static async Task RunAsync(Application application, string[] args) |
|||
{ |
|||
var builder = WebApplication.CreateBuilder(args); |
|||
|
|||
// Logging for this application
|
|||
builder.Host.UseSerilog((context, configuration) => configuration |
|||
.MinimumLevel.Verbose() |
|||
.Filter.ByExcluding(Matching.FromSource("Microsoft")) |
|||
.Enrich |
|||
.FromLogContext() |
|||
.WriteTo |
|||
.Console() |
|||
); |
|||
|
|||
builder.Services.AddRazorPages(o => o.RootDirectory = "/Dashboard/Pages"); |
|||
|
|||
builder.Services.AddServerSideBlazor(); |
|||
|
|||
builder.Services.AddOptions<StaticFileOptions>() |
|||
.PostConfigure(o => |
|||
{ |
|||
var fileProvider = new ManifestEmbeddedFileProvider(typeof(MicronetesHost).Assembly, "wwwroot"); |
|||
|
|||
// Make sure we don't remove the existing file providers (blazor needs this)
|
|||
o.FileProvider = new CompositeFileProvider(o.FileProvider, fileProvider); |
|||
}); |
|||
|
|||
builder.Services.AddSingleton(application); |
|||
|
|||
using var app = builder.Build(); |
|||
|
|||
var port = app.Configuration["port"] ?? "0"; |
|||
|
|||
app.Listen($"http://127.0.0.1:{port}"); |
|||
|
|||
app.UseDeveloperExceptionPage(); |
|||
|
|||
app.UseStaticFiles(); |
|||
|
|||
app.UseRouting(); |
|||
|
|||
var api = new MicronetesApi(); |
|||
|
|||
api.MapRoutes(app); |
|||
|
|||
app.MapBlazorHub(); |
|||
app.MapFallbackToPage("/_Host"); |
|||
|
|||
var logger = app.Logger; |
|||
|
|||
logger.LogInformation("Executing application from {Source}", application.Source); |
|||
|
|||
var lifetime = app.ApplicationLifetime; |
|||
var configuration = app.Configuration; |
|||
|
|||
var diagnosticOptions = DiagnosticOptions.FromConfiguration(configuration); |
|||
var diagnosticsCollector = new DiagnosticsCollector(logger, diagnosticOptions); |
|||
|
|||
// Print out what providers were selected and their values
|
|||
diagnosticOptions.DumpDiagnostics(logger); |
|||
|
|||
var processor = new AggregateApplicationProcessor(new IApplicationProcessor[] { |
|||
new EventPipeDiagnosticsRunner(logger, diagnosticsCollector), |
|||
new ProxyService(logger), |
|||
new DockerRunner(logger), |
|||
new ProcessRunner(logger, ProcessRunnerOptions.FromArgs(args)), |
|||
}); |
|||
|
|||
await app.StartAsync(); |
|||
|
|||
logger.LogInformation("Dashboard running on {Address}", app.Addresses.First()); |
|||
|
|||
try |
|||
{ |
|||
await processor.StartAsync(application); |
|||
} |
|||
catch (Exception ex) |
|||
{ |
|||
logger.LogError(0, ex, "Failed to launch application"); |
|||
} |
|||
|
|||
var waitForStop = new TaskCompletionSource<object>(TaskCreationOptions.RunContinuationsAsynchronously); |
|||
lifetime.ApplicationStopping.Register(obj => waitForStop.TrySetResult(null), null); |
|||
|
|||
await waitForStop.Task; |
|||
|
|||
logger.LogInformation("Shutting down..."); |
|||
|
|||
try |
|||
{ |
|||
await processor.StopAsync(application); |
|||
} |
|||
finally |
|||
{ |
|||
// Stop the host after everything else has been shutdown
|
|||
await app.StopAsync(); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,258 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.IO; |
|||
using System.Text.Json; |
|||
using Microsoft.Build.Construction; |
|||
using YamlDotNet.Serialization; |
|||
using YamlDotNet.Serialization.NamingConventions; |
|||
|
|||
namespace Micronetes.Hosting.Model |
|||
{ |
|||
public class Application |
|||
{ |
|||
public string ContextDirectory { get; set; } = Directory.GetCurrentDirectory(); |
|||
|
|||
public string Source { get; set; } |
|||
|
|||
public Application(IEnumerable<ServiceDescription> services) |
|||
{ |
|||
var map = new Dictionary<string, Service>(); |
|||
|
|||
// TODO: Do validation here
|
|||
foreach (var s in services) |
|||
{ |
|||
s.Replicas ??= 1; |
|||
map[s.Name] = new Service { Description = s }; |
|||
} |
|||
|
|||
Services = map; |
|||
} |
|||
|
|||
public static Application FromYaml(string path) |
|||
{ |
|||
var fullPath = Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), path)); |
|||
|
|||
var deserializer = new DeserializerBuilder() |
|||
.WithNamingConvention(CamelCaseNamingConvention.Instance) |
|||
.Build(); |
|||
|
|||
var descriptions = deserializer.Deserialize<ServiceDescription[]>(new StringReader(File.ReadAllText(path))); |
|||
|
|||
var contextDirectory = Path.GetDirectoryName(fullPath); |
|||
|
|||
foreach (var d in descriptions) |
|||
{ |
|||
if (d.Project == null) |
|||
{ |
|||
continue; |
|||
} |
|||
|
|||
// Try to populate more from launch settings
|
|||
var projectFilePath = Path.GetFullPath(Path.Combine(contextDirectory, d.Project)); |
|||
|
|||
if (!TryGetLaunchSettings(projectFilePath, out var projectSettings)) |
|||
{ |
|||
continue; |
|||
} |
|||
|
|||
PopulateFromLaunchSettings(d, projectSettings); |
|||
} |
|||
|
|||
return new Application(descriptions) |
|||
{ |
|||
Source = fullPath, |
|||
// Use the file location as the context when loading from a file
|
|||
ContextDirectory = contextDirectory |
|||
}; |
|||
} |
|||
|
|||
public static Application FromProject(string path) |
|||
{ |
|||
var fullPath = Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), path)); |
|||
|
|||
var projectDescription = CreateDescriptionFromProject(fullPath); |
|||
|
|||
return new Application(projectDescription == null ? new ServiceDescription[0] : new ServiceDescription[] { projectDescription }) |
|||
{ |
|||
Source = fullPath, |
|||
ContextDirectory = Path.GetDirectoryName(fullPath) |
|||
}; |
|||
} |
|||
|
|||
private static ServiceDescription CreateDescriptionFromProject(string fullPath) |
|||
{ |
|||
if (!TryGetLaunchSettings(fullPath, out var projectSettings)) |
|||
{ |
|||
return null; |
|||
} |
|||
|
|||
var projectDescription = new ServiceDescription |
|||
{ |
|||
Name = Path.GetFileNameWithoutExtension(fullPath).ToLower(), |
|||
Project = fullPath |
|||
}; |
|||
|
|||
PopulateFromLaunchSettings(projectDescription, projectSettings); |
|||
|
|||
return projectDescription; |
|||
} |
|||
|
|||
private static void PopulateFromLaunchSettings(ServiceDescription projectDescription, JsonElement projectSettings) |
|||
{ |
|||
if (projectDescription.Bindings.Count == 0 && projectSettings.TryGetProperty("applicationUrl", out var applicationUrls)) |
|||
{ |
|||
var addresses = applicationUrls.GetString()?.Split(';'); |
|||
|
|||
foreach (var address in addresses) |
|||
{ |
|||
var uri = new Uri(address); |
|||
|
|||
projectDescription.Bindings.Add(new ServiceBinding |
|||
{ |
|||
Port = uri.Port, |
|||
Protocol = uri.Scheme |
|||
}); |
|||
} |
|||
} |
|||
|
|||
if (projectDescription.Configuration.Count == 0 && projectSettings.TryGetProperty("environmentVariables", out var environmentVariables)) |
|||
{ |
|||
foreach (var envVar in environmentVariables.EnumerateObject()) |
|||
{ |
|||
projectDescription.Configuration.Add(new ConfigurationSource |
|||
{ |
|||
Name = envVar.Name, |
|||
Value = envVar.Value.GetString() |
|||
}); |
|||
} |
|||
} |
|||
|
|||
if (projectDescription.Replicas == null && projectSettings.TryGetProperty("replicas", out var replicasElement)) |
|||
{ |
|||
projectDescription.Replicas = replicasElement.GetInt32(); |
|||
} |
|||
} |
|||
|
|||
public static Application FromSolution(string path) |
|||
{ |
|||
var fullPath = Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory(), path)); |
|||
|
|||
var solution = SolutionFile.Parse(fullPath); |
|||
|
|||
var descriptions = new List<ServiceDescription>(); |
|||
|
|||
foreach (var project in solution.ProjectsInOrder) |
|||
{ |
|||
if (project.ProjectType != SolutionProjectType.KnownToBeMSBuildFormat) |
|||
{ |
|||
continue; |
|||
} |
|||
|
|||
var projectFilePath = project.AbsolutePath.Replace('\\', Path.DirectorySeparatorChar); |
|||
|
|||
var extension = Path.GetExtension(projectFilePath).ToLower(); |
|||
switch (extension) |
|||
{ |
|||
case ".csproj": |
|||
case ".fsproj": |
|||
break; |
|||
default: |
|||
continue; |
|||
} |
|||
|
|||
var description = CreateDescriptionFromProject(projectFilePath); |
|||
|
|||
if (description != null) |
|||
{ |
|||
descriptions.Add(description); |
|||
} |
|||
} |
|||
|
|||
return new Application(descriptions) |
|||
{ |
|||
Source = fullPath, |
|||
ContextDirectory = Path.GetDirectoryName(fullPath) |
|||
}; |
|||
} |
|||
|
|||
public Dictionary<string, Service> Services { get; } |
|||
|
|||
internal void PopulateEnvironment(Service service, Action<string, string> set, string defaultHost = "localhost") |
|||
{ |
|||
if (service.Description.Configuration != null) |
|||
{ |
|||
// Inject normal configuration
|
|||
foreach (var pair in service.Description.Configuration) |
|||
{ |
|||
set(pair.Name, pair.Value); |
|||
} |
|||
} |
|||
|
|||
void SetBinding(string serviceName, ServiceBinding b) |
|||
{ |
|||
var configName = ""; |
|||
var envName = ""; |
|||
|
|||
if (string.IsNullOrEmpty(b.Name)) |
|||
{ |
|||
configName = serviceName; |
|||
envName = serviceName; |
|||
} |
|||
else |
|||
{ |
|||
configName = $"{serviceName.ToUpper()}__{b.Name.ToUpper()}"; |
|||
envName = $"{serviceName.ToUpper()}_{b.Name.ToUpper()}"; |
|||
} |
|||
|
|||
if (!string.IsNullOrEmpty(b.ConnectionString)) |
|||
{ |
|||
// Special case for connection strings
|
|||
set($"CONNECTIONSTRING__{configName}", b.ConnectionString); |
|||
} |
|||
|
|||
if (!string.IsNullOrEmpty(b.Protocol)) |
|||
{ |
|||
// IConfiguration specific (double underscore ends up telling the configuration provider to use it as a separator)
|
|||
set($"SERVICE__{configName}__PROTOCOL", b.Protocol); |
|||
set($"{envName}_SERVICE_PROTOCOL", b.Protocol); |
|||
} |
|||
|
|||
if (b.Port != null) |
|||
{ |
|||
set($"SERVICE__{configName}__PORT", b.Port.ToString()); |
|||
set($"{envName}_SERVICE_PORT", b.Port.ToString()); |
|||
} |
|||
|
|||
set($"SERVICE__{configName}__HOST", b.Host ?? defaultHost); |
|||
set($"{envName}_SERVICE_HOST", b.Host ?? defaultHost); |
|||
} |
|||
|
|||
// Inject dependency information
|
|||
foreach (var s in Services.Values) |
|||
{ |
|||
foreach (var b in s.Description.Bindings) |
|||
{ |
|||
SetBinding(s.Description.Name.ToUpper(), b); |
|||
} |
|||
} |
|||
} |
|||
|
|||
private static bool TryGetLaunchSettings(string projectFilePath, out JsonElement projectSettings) |
|||
{ |
|||
var projectDirectory = Path.GetDirectoryName(projectFilePath); |
|||
var launchSettingsPath = Path.Combine(projectDirectory, "Properties", "launchSettings.json"); |
|||
|
|||
if (!File.Exists(launchSettingsPath)) |
|||
{ |
|||
projectSettings = default; |
|||
return false; |
|||
} |
|||
|
|||
// If there's a launchSettings.json, then use it to get addresses
|
|||
var root = JsonSerializer.Deserialize<JsonElement>(File.ReadAllText(launchSettingsPath)); |
|||
var key = Path.GetFileNameWithoutExtension(projectFilePath); |
|||
var profiles = root.GetProperty("profiles"); |
|||
return profiles.TryGetProperty(key, out projectSettings); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,169 @@ |
|||
using System; |
|||
using System.Collections.Concurrent; |
|||
using System.Collections.Generic; |
|||
using System.Reactive.Subjects; |
|||
using System.Text.Json; |
|||
using System.Text.Json.Serialization; |
|||
|
|||
namespace Micronetes.Hosting.Model |
|||
{ |
|||
public class Service |
|||
{ |
|||
public Service() |
|||
{ |
|||
Logs.Subscribe(entry => |
|||
{ |
|||
if (CachedLogs.Count > 5000) |
|||
{ |
|||
CachedLogs.Dequeue(); |
|||
} |
|||
|
|||
CachedLogs.Enqueue(entry); |
|||
}); |
|||
} |
|||
|
|||
public ServiceDescription Description { get; set; } |
|||
|
|||
public int Restarts { get; set; } |
|||
|
|||
public ServiceType ServiceType |
|||
{ |
|||
get |
|||
{ |
|||
if (Description.DockerImage != null) |
|||
{ |
|||
return ServiceType.Container; |
|||
} |
|||
|
|||
if (Description.Project != null) |
|||
{ |
|||
return ServiceType.Project; |
|||
} |
|||
|
|||
return ServiceType.Executable; |
|||
} |
|||
} |
|||
|
|||
public ServiceStatus Status { get; set; } = new ServiceStatus(); |
|||
|
|||
public ConcurrentDictionary<string, ReplicaStatus> Replicas { get; set; } = new ConcurrentDictionary<string, ReplicaStatus>(); |
|||
|
|||
[JsonIgnore] |
|||
public Dictionary<int, List<int>> PortMap { get; set; } = new Dictionary<int, List<int>>(); |
|||
|
|||
[JsonIgnore] |
|||
public Dictionary<object, object> Items { get; } = new Dictionary<object, object>(); |
|||
|
|||
[JsonIgnore] |
|||
public Queue<string> CachedLogs { get; } = new Queue<string>(); |
|||
|
|||
[JsonIgnore] |
|||
public Subject<string> Logs { get; } = new Subject<string>(); |
|||
|
|||
[JsonIgnore] |
|||
public Subject<ReplicaEvent> ReplicaEvents { get; } = new Subject<ReplicaEvent>(); |
|||
} |
|||
|
|||
public readonly struct ReplicaEvent |
|||
{ |
|||
public ReplicaState State { get; } |
|||
public ReplicaStatus Replica { get; } |
|||
|
|||
public ReplicaEvent(ReplicaState state, ReplicaStatus replica) |
|||
{ |
|||
State = state; |
|||
Replica = replica; |
|||
} |
|||
} |
|||
|
|||
public enum ReplicaState |
|||
{ |
|||
Removed, |
|||
Added, |
|||
Started, |
|||
Stopped, |
|||
} |
|||
|
|||
public class ServiceStatus |
|||
{ |
|||
public string ProjectFilePath { get; set; } |
|||
public string ExecutablePath { get; set; } |
|||
public string Args { get; set; } |
|||
public string WorkingDirectory { get; set; } |
|||
} |
|||
|
|||
public class ProcessStatus : ReplicaStatus |
|||
{ |
|||
public ProcessStatus(Service service, string name) : base(service, name) |
|||
{ |
|||
} |
|||
public int? ExitCode { get; set; } |
|||
public int? Pid { get; set; } |
|||
public IDictionary<string, string> Environment { get; set; } |
|||
} |
|||
|
|||
public class DockerStatus : ReplicaStatus |
|||
{ |
|||
public DockerStatus(Service service, string name) : base(service, name) |
|||
{ |
|||
} |
|||
|
|||
public string DockerCommand { get; set; } |
|||
|
|||
public string ContainerId { get; set; } |
|||
|
|||
public int DockerLogsPid { get; set; } |
|||
} |
|||
|
|||
public class ReplicaStatus |
|||
{ |
|||
public ReplicaStatus(Service service, string name) |
|||
{ |
|||
Service = service; |
|||
Name = name; |
|||
} |
|||
|
|||
public string Name { get; } |
|||
|
|||
public static JsonConverter<ReplicaStatus> JsonConverter = new Converter(); |
|||
|
|||
public IEnumerable<int> Ports { get; set; } |
|||
|
|||
[JsonIgnore] |
|||
public Service Service { get; } |
|||
|
|||
[JsonIgnore] |
|||
public Dictionary<object, object> Items { get; } = new Dictionary<object, object>(); |
|||
|
|||
[JsonIgnore] |
|||
public Dictionary<string, string> Metrics { get; set; } = new Dictionary<string, string>(); |
|||
|
|||
private class Converter : JsonConverter<ReplicaStatus> |
|||
{ |
|||
public override ReplicaStatus Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
|||
{ |
|||
throw new NotImplementedException(); |
|||
} |
|||
|
|||
public override void Write(Utf8JsonWriter writer, ReplicaStatus value, JsonSerializerOptions options) |
|||
{ |
|||
// Use the runtime type since we really want to serialize either the DockerStatus or ProcessStatus
|
|||
JsonSerializer.Serialize(writer, value, value.GetType(), options); |
|||
} |
|||
} |
|||
} |
|||
|
|||
public enum ServiceType |
|||
{ |
|||
Project, |
|||
Executable, |
|||
Container |
|||
} |
|||
|
|||
public class PortMapping |
|||
{ |
|||
public int ExternalPort { get; set; } |
|||
|
|||
public List<int> InteralPorts { get; set; } = new List<int>(); |
|||
} |
|||
} |
|||
@ -0,0 +1,12 @@ |
|||
namespace Micronetes.Hosting.Model |
|||
{ |
|||
public class ServiceBinding |
|||
{ |
|||
public string Name { get; set; } |
|||
public string ConnectionString { get; set; } |
|||
public int? Port { get; set; } |
|||
public int? InternalPort { get; set; } |
|||
public string Host { get; set; } |
|||
public string Protocol { get; set; } |
|||
} |
|||
} |
|||
@ -0,0 +1,29 @@ |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using YamlDotNet.Serialization; |
|||
|
|||
namespace Micronetes.Hosting.Model |
|||
{ |
|||
public class ServiceDescription |
|||
{ |
|||
public string Name { get; set; } |
|||
public bool External { get; set; } |
|||
public string DockerImage { get; set; } |
|||
public string Project { get; set; } |
|||
public bool? Build { get; set; } = true; |
|||
public string Executable { get; set; } |
|||
public string WorkingDirectory { get; set; } |
|||
public string Args { get; set; } |
|||
public int? Replicas { get; set; } |
|||
public List<ServiceBinding> Bindings { get; set; } = new List<ServiceBinding>(); |
|||
[YamlMember(Alias = "env")] |
|||
public List<ConfigurationSource> Configuration { get; set; } = new List<ConfigurationSource>(); |
|||
} |
|||
|
|||
public class ConfigurationSource |
|||
{ |
|||
public string Name { get; set; } |
|||
public string Value { get; set; } |
|||
public string Source { get; set; } |
|||
} |
|||
} |
|||
@ -0,0 +1,14 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Text; |
|||
|
|||
namespace Micronetes.Hosting.Model |
|||
{ |
|||
public enum ServiceState |
|||
{ |
|||
Starting, |
|||
Running, |
|||
NotRunning, |
|||
Stopped |
|||
} |
|||
} |
|||
@ -0,0 +1,300 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.IO; |
|||
using System.Linq; |
|||
using System.Runtime.InteropServices; |
|||
using System.Threading; |
|||
using System.Threading.Tasks; |
|||
using Micronetes.Hosting.Model; |
|||
using Microsoft.Extensions.Logging; |
|||
|
|||
namespace Micronetes.Hosting |
|||
{ |
|||
public class ProcessRunner : IApplicationProcessor |
|||
{ |
|||
private readonly ILogger _logger; |
|||
private readonly bool _debugMode; |
|||
private readonly bool _buildProjects; |
|||
|
|||
public ProcessRunner(ILogger logger, ProcessRunnerOptions options) |
|||
{ |
|||
_logger = logger; |
|||
_debugMode = options.DebugMode; |
|||
_buildProjects = options.BuildProjects; |
|||
} |
|||
|
|||
public Task StartAsync(Application application) |
|||
{ |
|||
var tasks = new Task[application.Services.Count]; |
|||
var index = 0; |
|||
foreach (var s in application.Services) |
|||
{ |
|||
tasks[index++] = s.Value.Description.External ? Task.CompletedTask : LaunchService(application, s.Value); |
|||
} |
|||
|
|||
return Task.WhenAll(tasks); |
|||
} |
|||
|
|||
public Task StopAsync(Application application) |
|||
{ |
|||
return KillRunningProcesses(application.Services); |
|||
} |
|||
|
|||
private async Task LaunchService(Application application, Service service) |
|||
{ |
|||
var serviceDescription = service.Description; |
|||
|
|||
if (serviceDescription.DockerImage != null) |
|||
{ |
|||
return; |
|||
} |
|||
|
|||
var serviceName = serviceDescription.Name; |
|||
|
|||
var path = ""; |
|||
var workingDirectory = ""; |
|||
var args = service.Description.Args ?? ""; |
|||
|
|||
if (serviceDescription.Project != null) |
|||
{ |
|||
var expandedProject = Environment.ExpandEnvironmentVariables(serviceDescription.Project); |
|||
var fullProjectPath = Path.GetFullPath(Path.Combine(application.ContextDirectory, expandedProject)); |
|||
path = GetExePath(fullProjectPath); |
|||
workingDirectory = Path.GetDirectoryName(fullProjectPath); |
|||
service.Status.ProjectFilePath = fullProjectPath; |
|||
} |
|||
else |
|||
{ |
|||
var expandedExecutable = Environment.ExpandEnvironmentVariables(serviceDescription.Executable); |
|||
path = Path.GetFullPath(Path.Combine(application.ContextDirectory, expandedExecutable)); |
|||
workingDirectory = serviceDescription.WorkingDirectory != null ? |
|||
Path.GetFullPath(Path.Combine(application.ContextDirectory, Environment.ExpandEnvironmentVariables(serviceDescription.WorkingDirectory))) : |
|||
Path.GetDirectoryName(path); |
|||
} |
|||
|
|||
// If this is a dll then use dotnet to run it
|
|||
if (Path.GetExtension(path) == ".dll") |
|||
{ |
|||
args = $"\"{path}\" {args}".Trim(); |
|||
path = "dotnet"; |
|||
} |
|||
|
|||
service.Status.ExecutablePath = path; |
|||
service.Status.WorkingDirectory = workingDirectory; |
|||
service.Status.Args = args; |
|||
|
|||
var processInfo = new ProcessInfo |
|||
{ |
|||
Tasks = new Task[service.Description.Replicas.Value] |
|||
}; |
|||
|
|||
if (service.Status.ProjectFilePath != null && service.Description.Build.GetValueOrDefault() && _buildProjects) |
|||
{ |
|||
// Sometimes building can fail because of file locking (like files being open in VS)
|
|||
_logger.LogInformation("Building project {ProjectFile}", service.Status.ProjectFilePath); |
|||
|
|||
service.Logs.OnNext($"dotnet build \"{service.Status.ProjectFilePath}\" /nologo"); |
|||
|
|||
var buildResult = await ProcessUtil.RunAsync("dotnet", $"build \"{service.Status.ProjectFilePath}\" /nologo", |
|||
outputDataReceived: data => service.Logs.OnNext(data), |
|||
throwOnError: false); |
|||
|
|||
if (buildResult.ExitCode != 0) |
|||
{ |
|||
_logger.LogInformation("Building {ProjectFile} failed with exit code {ExitCode}: " + buildResult.StandardOutput + buildResult.StandardError, service.Status.ProjectFilePath, buildResult.ExitCode); |
|||
return; |
|||
} |
|||
} |
|||
|
|||
async Task RunApplicationAsync(IEnumerable<(int Port, int BindingPort, string Protocol)> ports) |
|||
{ |
|||
var hasPorts = ports.Any(); |
|||
|
|||
var environment = new Dictionary<string, string> |
|||
{ |
|||
// Default to development environment
|
|||
["DOTNET_ENVIRONMENT"] = "Development" |
|||
}; |
|||
|
|||
application.PopulateEnvironment(service, (k, v) => environment[k] = v); |
|||
|
|||
if (_debugMode) |
|||
{ |
|||
environment["DOTNET_STARTUP_HOOKS"] = typeof(Hosting.Runtime.HostingRuntimeHelpers).Assembly.Location; |
|||
} |
|||
|
|||
if (hasPorts) |
|||
{ |
|||
// These ports should also be passed in not assuming ASP.NET Core
|
|||
environment["ASPNETCORE_URLS"] = string.Join(";", ports.Select(p => $"{p.Protocol ?? "http"}://localhost:{p.Port}")); |
|||
|
|||
foreach (var p in ports) |
|||
{ |
|||
environment[$"{p.Protocol?.ToUpper() ?? "HTTP"}_PORT"] = p.BindingPort.ToString(); |
|||
} |
|||
} |
|||
|
|||
while (!processInfo.StoppedTokenSource.IsCancellationRequested) |
|||
{ |
|||
var replica = serviceName + "_" + Guid.NewGuid().ToString().Substring(0, 10).ToLower(); |
|||
var status = new ProcessStatus(service, replica); |
|||
service.Replicas[replica] = status; |
|||
|
|||
service.ReplicaEvents.OnNext(new ReplicaEvent(ReplicaState.Added, status)); |
|||
|
|||
// This isn't your host name
|
|||
environment["APP_INSTANCE"] = replica; |
|||
|
|||
status.ExitCode = null; |
|||
status.Pid = null; |
|||
status.Environment = environment; |
|||
|
|||
if (hasPorts) |
|||
{ |
|||
status.Ports = ports.Select(p => p.Port); |
|||
} |
|||
|
|||
_logger.LogInformation("Launching service {ServiceName}: {ExePath} {args}", replica, path, args); |
|||
|
|||
try |
|||
{ |
|||
service.Logs.OnNext($"[{replica}]:{path} {args}"); |
|||
|
|||
var result = await ProcessUtil.RunAsync(path, args, |
|||
environmentVariables: environment, |
|||
workingDirectory: workingDirectory, |
|||
outputDataReceived: data => service.Logs.OnNext($"[{replica}]: {data}"), |
|||
onStart: pid => |
|||
{ |
|||
if (hasPorts) |
|||
{ |
|||
_logger.LogInformation("{ServiceName} running on process id {PID} bound to {Address}", replica, pid, string.Join(", ", ports.Select(p => $"{p.Protocol ?? "http"}://localhost:{p.Port}"))); |
|||
} |
|||
else |
|||
{ |
|||
_logger.LogInformation("{ServiceName} running on process id {PID}", replica, pid); |
|||
} |
|||
|
|||
status.Pid = pid; |
|||
|
|||
service.ReplicaEvents.OnNext(new ReplicaEvent(ReplicaState.Started, status)); |
|||
}, |
|||
throwOnError: false, |
|||
cancellationToken: processInfo.StoppedTokenSource.Token); |
|||
|
|||
status.ExitCode = result.ExitCode; |
|||
|
|||
if (status.Pid != null) |
|||
{ |
|||
service.ReplicaEvents.OnNext(new ReplicaEvent(ReplicaState.Stopped, status)); |
|||
} |
|||
} |
|||
catch (Exception ex) |
|||
{ |
|||
_logger.LogError(0, ex, "Failed to launch process for service {ServiceName}", replica); |
|||
|
|||
Thread.Sleep(5000); |
|||
} |
|||
|
|||
service.Restarts++; |
|||
|
|||
if (status.ExitCode != null) |
|||
{ |
|||
_logger.LogInformation("{ServiceName} process exited with exit code {ExitCode}", replica, status.ExitCode); |
|||
} |
|||
|
|||
// Remove the replica from the set
|
|||
service.Replicas.TryRemove(replica, out _); |
|||
service.ReplicaEvents.OnNext(new ReplicaEvent(ReplicaState.Removed, status)); |
|||
} |
|||
} |
|||
|
|||
if (serviceDescription.Bindings.Count > 0) |
|||
{ |
|||
// Each replica is assigned a list of internal ports, one mapped to each external
|
|||
// port
|
|||
for (int i = 0; i < serviceDescription.Replicas; i++) |
|||
{ |
|||
var ports = new List<(int, int, string)>(); |
|||
foreach (var binding in serviceDescription.Bindings) |
|||
{ |
|||
if (binding.Port == null) |
|||
{ |
|||
continue; |
|||
} |
|||
|
|||
ports.Add((service.PortMap[binding.Port.Value][i], binding.Port.Value, binding.Protocol)); |
|||
} |
|||
|
|||
processInfo.Tasks[i] = RunApplicationAsync(ports); |
|||
} |
|||
} |
|||
else |
|||
{ |
|||
for (int i = 0; i < service.Description.Replicas; i++) |
|||
{ |
|||
processInfo.Tasks[i] = RunApplicationAsync(Enumerable.Empty<(int, int, string)>()); |
|||
} |
|||
} |
|||
|
|||
service.Items[typeof(ProcessInfo)] = processInfo; |
|||
} |
|||
|
|||
private Task KillRunningProcesses(IDictionary<string, Service> services) |
|||
{ |
|||
static async Task KillProcessAsync(Service service) |
|||
{ |
|||
if (service.Items.TryGetValue(typeof(ProcessInfo), out var stateObj) && stateObj is ProcessInfo state) |
|||
{ |
|||
// Cancel the token before stopping the process
|
|||
state.StoppedTokenSource.Cancel(); |
|||
|
|||
await Task.WhenAll(state.Tasks); |
|||
} |
|||
} |
|||
|
|||
var index = 0; |
|||
var tasks = new Task[services.Count]; |
|||
foreach (var s in services.Values) |
|||
{ |
|||
var state = s; |
|||
tasks[index++] = KillProcessAsync(state); |
|||
} |
|||
|
|||
return Task.WhenAll(tasks); |
|||
} |
|||
|
|||
private static string GetExePath(string projectFilePath) |
|||
{ |
|||
// TODO: Use msbuild to get the target path
|
|||
|
|||
var outputFileName = Path.GetFileNameWithoutExtension(projectFilePath) + (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? ".exe" : ""); |
|||
|
|||
var debugOutputPath = Path.Combine(Path.GetDirectoryName(projectFilePath), "bin", "Debug"); |
|||
|
|||
var tfms = Directory.Exists(debugOutputPath) ? Directory.GetDirectories(debugOutputPath) : Array.Empty<string>(); |
|||
|
|||
if (tfms.Length > 0) |
|||
{ |
|||
// Pick the first one
|
|||
var path = Path.Combine(debugOutputPath, tfms[0], outputFileName); |
|||
if (File.Exists(path)) |
|||
{ |
|||
return path; |
|||
} |
|||
|
|||
// Older versions of .NET Core didn't have TFMs
|
|||
return Path.Combine(debugOutputPath, tfms[0], Path.GetFileNameWithoutExtension(projectFilePath) + ".dll"); |
|||
} |
|||
|
|||
return Path.Combine(debugOutputPath, "netcoreapp3.1", outputFileName); |
|||
} |
|||
|
|||
private class ProcessInfo |
|||
{ |
|||
public Task[] Tasks { get; set; } |
|||
|
|||
public CancellationTokenSource StoppedTokenSource { get; set; } = new CancellationTokenSource(); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,21 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
|
|||
namespace Micronetes.Hosting |
|||
{ |
|||
public class ProcessRunnerOptions |
|||
{ |
|||
public bool DebugMode { get; set; } |
|||
public bool BuildProjects { get; set; } |
|||
|
|||
public static ProcessRunnerOptions FromArgs(string[] args) |
|||
{ |
|||
return new ProcessRunnerOptions |
|||
{ |
|||
BuildProjects = !args.Contains("--no-build"), |
|||
DebugMode = args.Contains("--debug") |
|||
}; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,178 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.IO.Pipelines; |
|||
using System.Linq; |
|||
using System.Net; |
|||
using System.Net.Sockets; |
|||
using System.Threading; |
|||
using System.Threading.Tasks; |
|||
using Bedrock.Framework; |
|||
using Micronetes.Hosting.Model; |
|||
using Microsoft.AspNetCore.Connections; |
|||
using Microsoft.AspNetCore.Connections.Features; |
|||
using Microsoft.AspNetCore.Hosting; |
|||
using Microsoft.Extensions.Hosting; |
|||
using Microsoft.Extensions.Logging; |
|||
|
|||
namespace Micronetes.Hosting |
|||
{ |
|||
public class ProxyService : IApplicationProcessor |
|||
{ |
|||
private IHost _host; |
|||
private readonly ILogger _logger; |
|||
|
|||
public ProxyService(ILogger logger) |
|||
{ |
|||
_logger = logger; |
|||
} |
|||
|
|||
public async Task StartAsync(Application application) |
|||
{ |
|||
_host = new HostBuilder() |
|||
.ConfigureServer(server => |
|||
{ |
|||
server.UseSockets(sockets => |
|||
{ |
|||
foreach (var service in application.Services.Values) |
|||
{ |
|||
if (service.Description.External) |
|||
{ |
|||
// We eventually want to proxy everything, this is temporary
|
|||
continue; |
|||
} |
|||
|
|||
static int GetNextPort() |
|||
{ |
|||
// Let the OS assign the next available port. Unless we cycle through all ports
|
|||
// on a test run, the OS will always increment the port number when making these calls.
|
|||
// This prevents races in parallel test runs where a test is already bound to
|
|||
// a given port, and a new test is able to bind to the same port due to port
|
|||
// reuse being enabled by default by the OS.
|
|||
using var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); |
|||
socket.Bind(new IPEndPoint(IPAddress.Loopback, 0)); |
|||
return ((IPEndPoint)socket.LocalEndPoint).Port; |
|||
} |
|||
|
|||
foreach (var binding in service.Description.Bindings) |
|||
{ |
|||
if (binding.Port == null) |
|||
{ |
|||
continue; |
|||
} |
|||
|
|||
if (service.Description.Replicas == 1) |
|||
{ |
|||
// No need to proxy
|
|||
service.PortMap[binding.Port.Value] = new List<int> { binding.Port.Value }; |
|||
continue; |
|||
} |
|||
|
|||
var ports = new List<int>(); |
|||
|
|||
for (int i = 0; i < service.Description.Replicas; i++) |
|||
{ |
|||
// Reserve a port for each replica
|
|||
var port = GetNextPort(); |
|||
ports.Add(port); |
|||
} |
|||
|
|||
_logger.LogInformation("Mapping external port {ExternalPort} to internal port(s) {InternalPorts} for {ServiceName}", binding.Port, string.Join(", ", ports.Select(p => p.ToString())), service.Description.Name); |
|||
|
|||
service.PortMap[binding.Port.Value] = ports; |
|||
|
|||
sockets.Listen(IPAddress.Loopback, binding.Port.Value, o => |
|||
{ |
|||
long count = 0; |
|||
|
|||
// o.UseConnectionLogging("Micronetes.Proxy");
|
|||
|
|||
o.Run(async connection => |
|||
{ |
|||
var notificationFeature = connection.Features.Get<IConnectionLifetimeNotificationFeature>(); |
|||
|
|||
var next = (int)(Interlocked.Increment(ref count) % ports.Count); |
|||
|
|||
NetworkStream targetStream = null; |
|||
|
|||
try |
|||
{ |
|||
var target = new Socket(SocketType.Stream, ProtocolType.Tcp) |
|||
{ |
|||
NoDelay = true |
|||
}; |
|||
var port = ports[next]; |
|||
|
|||
_logger.LogDebug("Attempting to connect to {ServiceName} listening on {ExternalPort}:{Port}", service.Description.Name, binding.Port, port); |
|||
|
|||
await target.ConnectAsync(IPAddress.Loopback, port); |
|||
|
|||
_logger.LogDebug("Successfully connected to {ServiceName} listening on {ExternalPort}:{Port}", service.Description.Name, binding.Port, port); |
|||
|
|||
targetStream = new NetworkStream(target, ownsSocket: true); |
|||
} |
|||
catch (Exception ex) |
|||
{ |
|||
_logger.LogDebug(ex, "Proxy error for service {ServiceName}", service.Description.Name); |
|||
|
|||
await targetStream.DisposeAsync(); |
|||
|
|||
connection.Abort(); |
|||
return; |
|||
} |
|||
|
|||
try |
|||
{ |
|||
_logger.LogDebug("Proxying traffic to {ServiceName} {ExternalPort}:{InternalPort}", service.Description.Name, binding.Port, ports[next]); |
|||
|
|||
// external -> internal
|
|||
var reading = Task.Run(() => connection.Transport.Input.CopyToAsync(targetStream, notificationFeature.ConnectionClosedRequested)); |
|||
|
|||
// internal -> external
|
|||
var writing = Task.Run(() => targetStream.CopyToAsync(connection.Transport.Output, notificationFeature.ConnectionClosedRequested)); |
|||
|
|||
await Task.WhenAll(reading, writing); |
|||
} |
|||
catch (ConnectionResetException) |
|||
{ |
|||
// Connection was reset
|
|||
} |
|||
catch (OperationCanceledException ex) |
|||
{ |
|||
if (!notificationFeature.ConnectionClosedRequested.IsCancellationRequested) |
|||
{ |
|||
_logger.LogDebug(0, ex, "Proxy error for service {ServiceName}", service.Description.Name); |
|||
} |
|||
} |
|||
catch (Exception ex) |
|||
{ |
|||
_logger.LogDebug(0, ex, "Proxy error for service {ServiceName}", service.Description.Name); |
|||
} |
|||
finally |
|||
{ |
|||
await targetStream.DisposeAsync(); |
|||
} |
|||
|
|||
// This needs to reconnect to the target port(s) until its bound
|
|||
// it has to stop if the service is no longer running
|
|||
}); |
|||
}); |
|||
} |
|||
} |
|||
}); |
|||
}) |
|||
.Build(); |
|||
|
|||
await _host.StartAsync(); |
|||
} |
|||
|
|||
public async Task StopAsync(Application application) |
|||
{ |
|||
if (_host != null) |
|||
{ |
|||
await _host.StopAsync(); |
|||
|
|||
await (_host as IAsyncDisposable).DisposeAsync(); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@ -0,0 +1,86 @@ |
|||
SIL OPEN FONT LICENSE Version 1.1 |
|||
|
|||
Copyright (c) 2014 Waybury |
|||
|
|||
PREAMBLE |
|||
The goals of the Open Font License (OFL) are to stimulate worldwide |
|||
development of collaborative font projects, to support the font creation |
|||
efforts of academic and linguistic communities, and to provide a free and |
|||
open framework in which fonts may be shared and improved in partnership |
|||
with others. |
|||
|
|||
The OFL allows the licensed fonts to be used, studied, modified and |
|||
redistributed freely as long as they are not sold by themselves. The |
|||
fonts, including any derivative works, can be bundled, embedded, |
|||
redistributed and/or sold with any software provided that any reserved |
|||
names are not used by derivative works. The fonts and derivatives, |
|||
however, cannot be released under any other type of license. The |
|||
requirement for fonts to remain under this license does not apply |
|||
to any document created using the fonts or their derivatives. |
|||
|
|||
DEFINITIONS |
|||
"Font Software" refers to the set of files released by the Copyright |
|||
Holder(s) under this license and clearly marked as such. This may |
|||
include source files, build scripts and documentation. |
|||
|
|||
"Reserved Font Name" refers to any names specified as such after the |
|||
copyright statement(s). |
|||
|
|||
"Original Version" refers to the collection of Font Software components as |
|||
distributed by the Copyright Holder(s). |
|||
|
|||
"Modified Version" refers to any derivative made by adding to, deleting, |
|||
or substituting -- in part or in whole -- any of the components of the |
|||
Original Version, by changing formats or by porting the Font Software to a |
|||
new environment. |
|||
|
|||
"Author" refers to any designer, engineer, programmer, technical |
|||
writer or other person who contributed to the Font Software. |
|||
|
|||
PERMISSION & CONDITIONS |
|||
Permission is hereby granted, free of charge, to any person obtaining |
|||
a copy of the Font Software, to use, study, copy, merge, embed, modify, |
|||
redistribute, and sell modified and unmodified copies of the Font |
|||
Software, subject to the following conditions: |
|||
|
|||
1) Neither the Font Software nor any of its individual components, |
|||
in Original or Modified Versions, may be sold by itself. |
|||
|
|||
2) Original or Modified Versions of the Font Software may be bundled, |
|||
redistributed and/or sold with any software, provided that each copy |
|||
contains the above copyright notice and this license. These can be |
|||
included either as stand-alone text files, human-readable headers or |
|||
in the appropriate machine-readable metadata fields within text or |
|||
binary files as long as those fields can be easily viewed by the user. |
|||
|
|||
3) No Modified Version of the Font Software may use the Reserved Font |
|||
Name(s) unless explicit written permission is granted by the corresponding |
|||
Copyright Holder. This restriction only applies to the primary font name as |
|||
presented to the users. |
|||
|
|||
4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font |
|||
Software shall not be used to promote, endorse or advertise any |
|||
Modified Version, except to acknowledge the contribution(s) of the |
|||
Copyright Holder(s) and the Author(s) or with their explicit written |
|||
permission. |
|||
|
|||
5) The Font Software, modified or unmodified, in part or in whole, |
|||
must be distributed entirely under this license, and must not be |
|||
distributed under any other license. The requirement for fonts to |
|||
remain under this license does not apply to any document created |
|||
using the Font Software. |
|||
|
|||
TERMINATION |
|||
This license becomes null and void if any of the above conditions are |
|||
not met. |
|||
|
|||
DISCLAIMER |
|||
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
|||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF |
|||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT |
|||
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE |
|||
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
|||
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL |
|||
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
|||
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM |
|||
OTHER DEALINGS IN THE FONT SOFTWARE. |
|||
@ -0,0 +1,21 @@ |
|||
The MIT License (MIT) |
|||
|
|||
Copyright (c) 2014 Waybury |
|||
|
|||
Permission is hereby granted, free of charge, to any person obtaining a copy |
|||
of this software and associated documentation files (the "Software"), to deal |
|||
in the Software without restriction, including without limitation the rights |
|||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|||
copies of the Software, and to permit persons to whom the Software is |
|||
furnished to do so, subject to the following conditions: |
|||
|
|||
The above copyright notice and this permission notice shall be included in |
|||
all copies or substantial portions of the Software. |
|||
|
|||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|||
THE SOFTWARE. |
|||
@ -0,0 +1,114 @@ |
|||
[Open Iconic v1.1.1](http://useiconic.com/open) |
|||
=========== |
|||
|
|||
### Open Iconic is the open source sibling of [Iconic](http://useiconic.com). It is a hyper-legible collection of 223 icons with a tiny footprint—ready to use with Bootstrap and Foundation. [View the collection](http://useiconic.com/open#icons) |
|||
|
|||
|
|||
|
|||
## What's in Open Iconic? |
|||
|
|||
* 223 icons designed to be legible down to 8 pixels |
|||
* Super-light SVG files - 61.8 for the entire set |
|||
* SVG sprite—the modern replacement for icon fonts |
|||
* Webfont (EOT, OTF, SVG, TTF, WOFF), PNG and WebP formats |
|||
* Webfont stylesheets (including versions for Bootstrap and Foundation) in CSS, LESS, SCSS and Stylus formats |
|||
* PNG and WebP raster images in 8px, 16px, 24px, 32px, 48px and 64px. |
|||
|
|||
|
|||
## Getting Started |
|||
|
|||
#### For code samples and everything else you need to get started with Open Iconic, check out our [Icons](http://useiconic.com/open#icons) and [Reference](http://useiconic.com/open#reference) sections. |
|||
|
|||
### General Usage |
|||
|
|||
#### Using Open Iconic's SVGs |
|||
|
|||
We like SVGs and we think they're the way to display icons on the web. Since Open Iconic are just basic SVGs, we suggest you display them like you would any other image (don't forget the `alt` attribute). |
|||
|
|||
``` |
|||
<img src="/open-iconic/svg/icon-name.svg" alt="icon name"> |
|||
``` |
|||
|
|||
#### Using Open Iconic's SVG Sprite |
|||
|
|||
Open Iconic also comes in a SVG sprite which allows you to display all the icons in the set with a single request. It's like an icon font, without being a hack. |
|||
|
|||
Adding an icon from an SVG sprite is a little different than what you're used to, but it's still a piece of cake. *Tip: To make your icons easily style able, we suggest adding a general class to the* `<svg>` *tag and a unique class name for each different icon in the* `<use>` *tag.* |
|||
|
|||
``` |
|||
<svg class="icon"> |
|||
<use xlink:href="open-iconic.svg#account-login" class="icon-account-login"></use> |
|||
</svg> |
|||
``` |
|||
|
|||
Sizing icons only needs basic CSS. All the icons are in a square format, so just set the `<svg>` tag with equal width and height dimensions. |
|||
|
|||
``` |
|||
.icon { |
|||
width: 16px; |
|||
height: 16px; |
|||
} |
|||
``` |
|||
|
|||
Coloring icons is even easier. All you need to do is set the `fill` rule on the `<use>` tag. |
|||
|
|||
``` |
|||
.icon-account-login { |
|||
fill: #f00; |
|||
} |
|||
``` |
|||
|
|||
To learn more about SVG Sprites, read [Chris Coyier's guide](http://css-tricks.com/svg-sprites-use-better-icon-fonts/). |
|||
|
|||
#### Using Open Iconic's Icon Font... |
|||
|
|||
|
|||
##### …with Bootstrap |
|||
|
|||
You can find our Bootstrap stylesheets in `font/css/open-iconic-bootstrap.{css, less, scss, styl}` |
|||
|
|||
|
|||
``` |
|||
<link href="/open-iconic/font/css/open-iconic-bootstrap.css" rel="stylesheet"> |
|||
``` |
|||
|
|||
|
|||
``` |
|||
<span class="oi oi-icon-name" title="icon name" aria-hidden="true"></span> |
|||
``` |
|||
|
|||
##### …with Foundation |
|||
|
|||
You can find our Foundation stylesheets in `font/css/open-iconic-foundation.{css, less, scss, styl}` |
|||
|
|||
``` |
|||
<link href="/open-iconic/font/css/open-iconic-foundation.css" rel="stylesheet"> |
|||
``` |
|||
|
|||
|
|||
``` |
|||
<span class="fi-icon-name" title="icon name" aria-hidden="true"></span> |
|||
``` |
|||
|
|||
##### …on its own |
|||
|
|||
You can find our default stylesheets in `font/css/open-iconic.{css, less, scss, styl}` |
|||
|
|||
``` |
|||
<link href="/open-iconic/font/css/open-iconic.css" rel="stylesheet"> |
|||
``` |
|||
|
|||
``` |
|||
<span class="oi" data-glyph="icon-name" title="icon name" aria-hidden="true"></span> |
|||
``` |
|||
|
|||
|
|||
## License |
|||
|
|||
### Icons |
|||
|
|||
All code (including SVG markup) is under the [MIT License](http://opensource.org/licenses/MIT). |
|||
|
|||
### Fonts |
|||
|
|||
All fonts are under the [SIL Licensed](http://scripts.sil.org/cms/scripts/page.php?item_id=OFL_web). |
|||
File diff suppressed because one or more lines are too long
Binary file not shown.
Binary file not shown.
|
After Width: | Height: | Size: 54 KiB |
Binary file not shown.
Binary file not shown.
@ -0,0 +1,205 @@ |
|||
@import url('open-iconic/font/css/open-iconic-bootstrap.min.css'); |
|||
|
|||
html, body { |
|||
font-family: "Roboto",sans-serif; |
|||
} |
|||
|
|||
a, .btn-link { |
|||
color: #0366d6; |
|||
} |
|||
|
|||
.btn-primary { |
|||
color: #fff; |
|||
background-color: #3371e3; |
|||
border-color: #303030; |
|||
} |
|||
|
|||
app { |
|||
position: relative; |
|||
display: flex; |
|||
flex-direction: column; |
|||
} |
|||
|
|||
.top-row { |
|||
height: 3.5rem; |
|||
display: flex; |
|||
align-items: center; |
|||
} |
|||
|
|||
.main { |
|||
flex: 1; |
|||
} |
|||
|
|||
.main .top-row { |
|||
background-color: #303030; |
|||
border-bottom: 1px solid #d6d5d5; |
|||
justify-content: flex-end; |
|||
} |
|||
|
|||
.main .top-row > a, .main .top-row .btn-link { |
|||
white-space: nowrap; |
|||
margin-left: 1.5rem; |
|||
} |
|||
|
|||
.main .top-row a:first-child { |
|||
overflow: hidden; |
|||
text-overflow: ellipsis; |
|||
} |
|||
|
|||
.sidebar { |
|||
background-image: linear-gradient(180deg, #000 0%, #333 50%); |
|||
} |
|||
|
|||
.sidebar .top-row { |
|||
background-color: #303030; |
|||
} |
|||
|
|||
.sidebar .navbar-brand { |
|||
font-size: 1.1rem; |
|||
} |
|||
|
|||
.sidebar .oi { |
|||
width: 2rem; |
|||
font-size: 1.1rem; |
|||
vertical-align: text-top; |
|||
top: -2px; |
|||
} |
|||
|
|||
.sidebar .nav-item, top-nav-item { |
|||
font-size: 0.9rem; |
|||
padding-bottom: 0.5rem; |
|||
} |
|||
|
|||
.sidebar .nav-item:first-of-type { |
|||
padding-top: 1rem; |
|||
} |
|||
|
|||
.sidebar .nav-item:last-of-type { |
|||
padding-bottom: 1rem; |
|||
} |
|||
|
|||
.sidebar .nav-item a, .top-nav-item a { |
|||
color: #d7d7d7; |
|||
border-radius: 4px; |
|||
height: 3rem; |
|||
display: flex; |
|||
align-items: center; |
|||
line-height: 3rem; |
|||
background-color: #3371e3; |
|||
} |
|||
|
|||
.sidebar .nav-item a.active, .top-nav-item a.active { |
|||
background-color: #0366d6; |
|||
color: white; |
|||
} |
|||
|
|||
.sidebar .nav-item a:hover, .top-nav-item a:hover { |
|||
background-color: rgba(255,255,255,0.1); |
|||
transition: 0.3s; |
|||
color: white; |
|||
} |
|||
|
|||
button.navbar-toggler { |
|||
color: white; |
|||
background-color: #303030; |
|||
} |
|||
|
|||
button.navbar-toggler:hover { |
|||
color: #3371e3; |
|||
background-color: #3371e3; |
|||
transition: 0.3s; |
|||
} |
|||
|
|||
.top-nav-item a { |
|||
padding-left: 2rem; |
|||
padding-right: 2rem; |
|||
text-decoration: none; |
|||
background-color: #303030; |
|||
border: 1px solid rgba(255,255,255,.1); |
|||
border-radius: .25rem; |
|||
} |
|||
|
|||
.content { |
|||
padding-top: 1.1rem; |
|||
} |
|||
|
|||
.navbar-toggler { |
|||
background-color: rgba(255, 255, 255, 0.1); |
|||
} |
|||
|
|||
.valid.modified:not([type=checkbox]) { |
|||
outline: 1px solid #26b050; |
|||
} |
|||
|
|||
.invalid { |
|||
outline: 1px solid red; |
|||
} |
|||
|
|||
.validation-message { |
|||
color: red; |
|||
} |
|||
|
|||
#blazor-error-ui { |
|||
background: lightyellow; |
|||
bottom: 0; |
|||
box-shadow: 0 -1px 2px rgba(0, 0, 0, 0.2); |
|||
display: none; |
|||
left: 0; |
|||
padding: 0.6rem 1.25rem 0.7rem 1.25rem; |
|||
position: fixed; |
|||
width: 100%; |
|||
z-index: 1000; |
|||
} |
|||
|
|||
#blazor-error-ui .dismiss { |
|||
cursor: pointer; |
|||
position: absolute; |
|||
right: 0.75rem; |
|||
top: 0.5rem; |
|||
} |
|||
|
|||
@media (max-width: 767.98px) { |
|||
.main .top-row:not(.auth) { |
|||
display: none; |
|||
} |
|||
|
|||
.main .top-row.auth { |
|||
justify-content: space-between; |
|||
} |
|||
|
|||
.main .top-row a, .main .top-row .btn-link { |
|||
margin-left: 0; |
|||
} |
|||
} |
|||
|
|||
@media (min-width: 768px) { |
|||
app { |
|||
flex-direction: row; |
|||
} |
|||
|
|||
.sidebar { |
|||
width: 250px; |
|||
height: 100vh; |
|||
position: sticky; |
|||
top: 0; |
|||
} |
|||
|
|||
.main .top-row { |
|||
position: sticky; |
|||
top: 0; |
|||
} |
|||
|
|||
.main > div { |
|||
padding-left: 2rem !important; |
|||
padding-right: 1.5rem !important; |
|||
} |
|||
|
|||
.navbar-toggler { |
|||
display: none; |
|||
} |
|||
|
|||
.sidebar .collapse { |
|||
/* Never collapse the sidebar for wide screens */ |
|||
display: block; |
|||
} |
|||
} |
|||
|
After Width: | Height: | Size: 15 KiB |
|
After Width: | Height: | Size: 2.2 KiB |
@ -0,0 +1,259 @@ |
|||
using System; |
|||
using System.CommandLine; |
|||
using System.CommandLine.Builder; |
|||
using System.CommandLine.Invocation; |
|||
using System.CommandLine.IO; |
|||
using System.CommandLine.Parsing; |
|||
using System.CommandLine.Rendering; |
|||
using System.IO; |
|||
using System.Linq; |
|||
using System.Reflection; |
|||
using System.Threading.Tasks; |
|||
using Micronetes.Hosting; |
|||
using Micronetes.Hosting.Model; |
|||
using YamlDotNet.Serialization; |
|||
using YamlDotNet.Serialization.NamingConventions; |
|||
|
|||
namespace Micronetes.Host |
|||
{ |
|||
class Program |
|||
{ |
|||
static async Task<int> Main(string[] args) |
|||
{ |
|||
var command = new RootCommand(); |
|||
|
|||
command.Add(RunCommand(args)); |
|||
command.Add(NewCommand()); |
|||
|
|||
command.Description = "Process manager and orchestrator for microservices."; |
|||
|
|||
var builder = new CommandLineBuilder(command); |
|||
builder.UseHelp(); |
|||
builder.UseVersionOption(); |
|||
builder.UseDebugDirective(); |
|||
builder.UseParseErrorReporting(); |
|||
builder.ParseResponseFileAs(ResponseFileHandling.ParseArgsAsSpaceSeparated); |
|||
builder.UsePrefixes(new[] { "-", "--", }); // disable garbage windows conventions
|
|||
|
|||
builder.CancelOnProcessTermination(); |
|||
builder.UseExceptionHandler(HandleException); |
|||
|
|||
// Allow fancy drawing.
|
|||
builder.UseAnsiTerminalWhenAvailable(); |
|||
|
|||
var parser = builder.Build(); |
|||
return await parser.InvokeAsync(args); |
|||
} |
|||
|
|||
private static Command NewCommand() |
|||
{ |
|||
var command = new Command("new", "create a yaml manifest") |
|||
{ |
|||
}; |
|||
|
|||
var argument = new Argument("path") |
|||
{ |
|||
Description = "A solution or project file to generate a yaml manifest from", |
|||
Arity = ArgumentArity.ZeroOrOne |
|||
}; |
|||
|
|||
command.AddArgument(argument); |
|||
|
|||
command.Handler = CommandHandler.Create<IConsole, string>((console, path) => |
|||
{ |
|||
if (File.Exists("m8s.yaml")) |
|||
{ |
|||
console.Out.WriteLine("\"m8s.yaml\" already exists."); |
|||
return; |
|||
} |
|||
|
|||
var template = @"- name: app
|
|||
# project: app.csproj # msbuild project path (relative to this file) |
|||
# executable: app.exe # path to an executable (relative to this file) |
|||
# args: --arg1=3 # arguments to pass to the process |
|||
# replicas: 5 # number of times to launch the application |
|||
# env: # array of environment variables |
|||
# - name: key |
|||
# value: value |
|||
# bindings: # optional array of bindings (ports, connection strings) |
|||
# - port: 8080 # number port of the binding |
|||
";
|
|||
|
|||
try |
|||
{ |
|||
if (!string.IsNullOrEmpty(path)) |
|||
{ |
|||
var application = ResolveApplication(path); |
|||
var serializer = new SerializerBuilder() |
|||
.WithNamingConvention(CamelCaseNamingConvention.Instance) |
|||
.ConfigureDefaultValuesHandling(DefaultValuesHandling.OmitDefaults) |
|||
.Build(); |
|||
|
|||
var extension = Path.GetExtension(application.Source).ToLowerInvariant(); |
|||
var directory = Path.GetDirectoryName(application.Source); |
|||
var descriptions = application.Services.Select(s => s.Value.Description).ToList(); |
|||
|
|||
// Clear all bindings if any for solutions and project files
|
|||
if (extension == ".sln" || extension == ".csproj" || extension == ".fsproj") |
|||
{ |
|||
foreach (var d in descriptions) |
|||
{ |
|||
d.Bindings = null; |
|||
d.Replicas = null; |
|||
d.Build = null; |
|||
d.Configuration = null; |
|||
d.Project = d.Project.Substring(directory.Length).TrimStart(Path.DirectorySeparatorChar); |
|||
} |
|||
} |
|||
|
|||
template = serializer.Serialize(descriptions); |
|||
} |
|||
} |
|||
catch (FileNotFoundException) |
|||
{ |
|||
// No file found, just generate a new one
|
|||
} |
|||
|
|||
File.WriteAllText("m8s.yaml", template); |
|||
console.Out.WriteLine("Created \"m8s.yaml\""); |
|||
}); |
|||
|
|||
return command; |
|||
} |
|||
|
|||
private static Command RunCommand(string[] args) |
|||
{ |
|||
var command = new Command("run", "run the application") |
|||
{ |
|||
}; |
|||
|
|||
var argument = new Argument("path") |
|||
{ |
|||
Description = "A file or directory to execute. Supports a project files, solution files or a yaml manifest.", |
|||
Arity = ArgumentArity.ZeroOrOne |
|||
}; |
|||
|
|||
// TODO: We'll need to support a --build-args
|
|||
command.AddOption(new Option("--no-build") |
|||
{ |
|||
Description = "Do not build project files before running.", |
|||
Required = false |
|||
}); |
|||
|
|||
command.AddOption(new Option("--port") |
|||
{ |
|||
Description = "The port to run control plane on.", |
|||
Argument = new Argument<int>("port"), |
|||
Required = false |
|||
}); |
|||
|
|||
command.AddOption(new Option("--logs") |
|||
{ |
|||
Description = "Write structured application logs to the specified log providers. Supported providers are console, elastic (Elasticsearch), ai (ApplicationInsights), seq.", |
|||
Argument = new Argument<string>("logs"), |
|||
Required = false |
|||
}); |
|||
|
|||
command.AddOption(new Option("--dtrace") |
|||
{ |
|||
Description = "Write distributed traces to the specified providers. Supported providers are zipkin.", |
|||
Argument = new Argument<string>("logs"), |
|||
Required = false |
|||
}); |
|||
|
|||
command.AddOption(new Option("--debug") |
|||
{ |
|||
Description = "Wait for debugger attach in all services.", |
|||
Required = false |
|||
}); |
|||
|
|||
command.AddArgument(argument); |
|||
|
|||
command.Handler = CommandHandler.Create<IConsole, string>((console, path) => |
|||
{ |
|||
Application app = ResolveApplication(path); |
|||
return MicronetesHost.RunAsync(app, args); |
|||
}); |
|||
|
|||
return command; |
|||
} |
|||
|
|||
private static Application ResolveApplication(string path) |
|||
{ |
|||
if (string.IsNullOrEmpty(path)) |
|||
{ |
|||
path = ResolveFileFromDirectory(Directory.GetCurrentDirectory()); |
|||
} |
|||
else if (Directory.Exists(path)) |
|||
{ |
|||
path = ResolveFileFromDirectory(Path.GetFullPath(path)); |
|||
} |
|||
|
|||
if (!File.Exists(path)) |
|||
{ |
|||
throw new FileNotFoundException($"{path} does not exist"); |
|||
} |
|||
|
|||
switch (Path.GetExtension(path).ToLower()) |
|||
{ |
|||
case ".yaml": |
|||
case ".yml": |
|||
return Application.FromYaml(path); |
|||
case ".csproj": |
|||
case ".fsproj": |
|||
return Application.FromProject(path); |
|||
case ".sln": |
|||
return Application.FromSolution(path); |
|||
default: |
|||
throw new NotSupportedException($"{path} not supported"); |
|||
} |
|||
} |
|||
|
|||
private static string ResolveFileFromDirectory(string basePath) |
|||
{ |
|||
var formats = new[] { "m8s.yaml", "m8s.yml", "*.csproj", "*.fsproj", "*.sln" }; |
|||
|
|||
foreach (var format in formats) |
|||
{ |
|||
var files = Directory.GetFiles(basePath, format); |
|||
if (files.Length == 0) |
|||
{ |
|||
continue; |
|||
} |
|||
|
|||
if (files.Length > 1) |
|||
{ |
|||
throw new InvalidOperationException($"Ambiguous match found {string.Join(", ", files.Select(Path.GetFileName))}"); |
|||
} |
|||
|
|||
return files[0]; |
|||
} |
|||
|
|||
throw new InvalidOperationException($"None of the supported files were found (m8s.yaml, .csproj, .fsproj, .sln)"); |
|||
} |
|||
|
|||
private static void HandleException(Exception exception, InvocationContext context) |
|||
{ |
|||
// context.Console.ResetTerminalForegroundColor();
|
|||
// context.Console.SetTerminalForegroundColor(ConsoleColor.Red);
|
|||
|
|||
if (exception is OperationCanceledException) |
|||
{ |
|||
context.Console.Error.WriteLine("operation canceled."); |
|||
} |
|||
else if (exception is TargetInvocationException tae) |
|||
{ |
|||
context.Console.Error.WriteLine(tae.InnerException.Message); |
|||
} |
|||
else |
|||
{ |
|||
context.Console.Error.WriteLine("unhandled exception: "); |
|||
context.Console.Error.WriteLine(exception.ToString()); |
|||
} |
|||
|
|||
// context.Console.ResetTerminalForegroundColor();
|
|||
|
|||
context.ResultCode = 1; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,7 @@ |
|||
{ |
|||
"profiles": { |
|||
"dotnet-micronetes": { |
|||
"commandName": "Project" |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,20 @@ |
|||
<Project Sdk="Microsoft.NET.Sdk"> |
|||
|
|||
<PropertyGroup> |
|||
<OutputType>Exe</OutputType> |
|||
<TargetFramework>netcoreapp3.1</TargetFramework> |
|||
<PackAsTool>true</PackAsTool> |
|||
<RootNamespace>Micronetes.Host</RootNamespace> |
|||
<AssemblyName>m8s</AssemblyName> |
|||
<ToolCommandName>m8s</ToolCommandName> |
|||
<Description>Local orchestrator for microservices.</Description> |
|||
<Nullable>disable</Nullable> |
|||
</PropertyGroup> |
|||
|
|||
<ItemGroup> |
|||
<ProjectReference Include="..\Micronetes.Hosting\Micronetes.Hosting.csproj" /> |
|||
<PackageReference Include="System.CommandLine" Version="2.0.0-beta1.20071.2" /> |
|||
<PackageReference Include="System.Commandline.Rendering" Version="0.3.0-alpha.20070.2" /> |
|||
</ItemGroup> |
|||
|
|||
</Project> |
|||
Loading…
Reference in new issue