protected internal virtual void WriteMetricEvents(SystemMetricEvent[] metricEvents) { if (metricEvents == null) { throw new ArgumentNullException(nameof(metricEvents)); } AppServiceOptions currentAppServiceOptions = _appServiceOptions.CurrentValue; foreach (SystemMetricEvent metricEvent in metricEvents) { _eventGenerator.LogFunctionMetricEvent( currentAppServiceOptions.SubscriptionId, currentAppServiceOptions.AppName, metricEvent.FunctionName ?? string.Empty, metricEvent.EventName.ToLowerInvariant(), metricEvent.Average, metricEvent.Minimum, metricEvent.Maximum, metricEvent.Count, metricEvent.Timestamp, metricEvent.Data ?? string.Empty, currentAppServiceOptions.RuntimeSiteName ?? string.Empty, currentAppServiceOptions.SlotName ?? string.Empty); } }
public DiagnosticLoggerTests() { _environment.SetEnvironmentVariable(EnvironmentSettingNames.AzureWebsiteOwnerName, $"{_subscriptionId}+westuswebspace"); _environment.SetEnvironmentVariable(EnvironmentSettingNames.AzureWebsiteHostName, _websiteHostName); _environment.SetEnvironmentVariable(EnvironmentSettingNames.RegionName, _regionName); _environment.SetEnvironmentVariable(EnvironmentSettingNames.AzureWebsiteInstanceId, _roleInstance); _mockEventGenerator = new Mock <IEventGenerator>(MockBehavior.Strict); var appServiceOptions = new AppServiceOptions { AppName = "TestApp", SlotName = "Production", SubscriptionId = "abc123", RuntimeSiteName = "TestApp_Runtime" }; _appServiceOptionsWrapper = new TestOptionsMonitor <AppServiceOptions>(appServiceOptions); _category = LogCategories.CreateFunctionCategory(_functionName); var loggerProvider = new TestLoggerProvider(); var loggerFactory = new LoggerFactory(); loggerFactory.AddProvider(loggerProvider); _hostNameProvider = new HostNameProvider(_environment); _logger = new AzureMonitorDiagnosticLogger(_category, _hostInstanceId, _mockEventGenerator.Object, _environment, new LoggerExternalScopeProvider(), _hostNameProvider, _appServiceOptionsWrapper); }
private static int RunServiceHost() { Console.WriteLine(Environment.CommandLine); AppServiceOptions serviceOptions = null; var topshelfHost = HostFactory.New(config => { CreateHost(config, out serviceOptions); }); // Инициализация окружения для приложения if (!string.IsNullOrWhiteSpace(serviceOptions.StartOptions) && serviceOptions.StartOptions.IndexOf("init", StringComparison.OrdinalIgnoreCase) >= 0) { var initAppServiceHost = new AppServiceHost(); initAppServiceHost.Init(TimeSpan.FromSeconds(10)); } // Запуск службы приложения if (string.IsNullOrWhiteSpace(serviceOptions.StartOptions) || serviceOptions.StartOptions.IndexOf("start", StringComparison.OrdinalIgnoreCase) >= 0) { var topshelfExitCode = topshelfHost.Run(); return(topshelfExitCode == TopshelfExitCode.Ok ? (int)TopshelfExitCode.Ok : (int)topshelfExitCode); } return((int)TopshelfExitCode.Ok); }
public AppService(IServiceProvider serviceProvider, Harvesting.HarvestingOrchestratorService harvestingOrchestratorService, PrintoutService printoutService, IOptions <AppServiceOptions> appServiceOptions) { this._serviceProvider = serviceProvider; this._harvestingOrchestratorService = harvestingOrchestratorService; this._printoutService = printoutService; this._appServiceOptions = appServiceOptions.Value; }
public SystemLogger(string hostInstanceId, string categoryName, IEventGenerator eventGenerator, IEnvironment environment, IDebugStateProvider debugStateProvider, IScriptEventManager eventManager, IExternalScopeProvider scopeProvider, IOptionsMonitor <AppServiceOptions> appServiceOptionsMonitor) { _environment = environment; _eventGenerator = eventGenerator; _categoryName = categoryName ?? string.Empty; _logLevel = LogLevel.Debug; _functionName = LogCategories.IsFunctionCategory(_categoryName) ? _categoryName.Split('.')[1] : null; _isUserFunction = LogCategories.IsFunctionUserCategory(_categoryName); _hostInstanceId = hostInstanceId; _debugStateProvider = debugStateProvider; _eventManager = eventManager; _scopeProvider = scopeProvider; appServiceOptionsMonitor.OnChange(newOptions => _appServiceOptions = newOptions); _appServiceOptions = appServiceOptionsMonitor.CurrentValue; }
public MetricsEventManager(IOptionsMonitor <AppServiceOptions> appServiceOptionsMonitor, IEventGenerator generator, int functionActivityFlushIntervalSeconds, IMetricsPublisher metricsPublisher, ILinuxContainerActivityPublisher linuxContainerActivityPublisher, ILogger <MetricsEventManager> logger, int metricsFlushIntervalMS = DefaultFlushIntervalMS) { _logger = logger ?? throw new ArgumentNullException(nameof(logger)); // we read these in the ctor (not static ctor) since it can change on the fly appServiceOptionsMonitor.OnChange(newOptions => _appServiceOptions = newOptions); _appServiceOptions = appServiceOptionsMonitor.CurrentValue; _eventGenerator = generator; _functionActivityFlushIntervalSeconds = functionActivityFlushIntervalSeconds; QueuedEvents = new ConcurrentDictionary <string, SystemMetricEvent>(StringComparer.OrdinalIgnoreCase); // Initialize the periodic log flush timer _metricsFlushTimer = new Timer(TimerFlush, null, metricsFlushIntervalMS, metricsFlushIntervalMS); _functionActivityTracker = new FunctionActivityTracker(appServiceOptionsMonitor, _eventGenerator, metricsPublisher, linuxContainerActivityPublisher, _functionActivityFlushIntervalSeconds, _logger); }
public AzureMonitorDiagnosticLogger(string category, string hostInstanceId, IEventGenerator eventGenerator, IEnvironment environment, IExternalScopeProvider scopeProvider, HostNameProvider hostNameProvider, IOptionsMonitor <AppServiceOptions> appServiceOptionsMonitor) { _category = category ?? throw new ArgumentNullException(nameof(category)); _hostInstanceId = hostInstanceId ?? throw new ArgumentNullException(nameof(hostInstanceId)); _eventGenerator = eventGenerator ?? throw new ArgumentNullException(nameof(eventGenerator)); _environment = environment ?? throw new ArgumentNullException(nameof(environment)); _scopeProvider = scopeProvider ?? throw new ArgumentNullException(nameof(scopeProvider)); _hostNameProvider = hostNameProvider ?? throw new ArgumentNullException(nameof(hostNameProvider)); _ = appServiceOptionsMonitor ?? throw new ArgumentNullException(nameof(appServiceOptionsMonitor)); appServiceOptionsMonitor.OnChange(newOptions => _appServiceOptions = newOptions); _appServiceOptions = appServiceOptionsMonitor.CurrentValue; _roleInstance = _environment.GetInstanceId(); _regionName = _environment.GetEnvironmentVariable(EnvironmentSettingNames.RegionName) ?? string.Empty; }
internal FunctionActivityTracker(IOptionsMonitor <AppServiceOptions> appServiceOptionsMonitor, IEventGenerator generator, IMetricsPublisher metricsPublisher, ILinuxContainerActivityPublisher linuxContainerActivityPublisher, int functionActivityFlushInterval, ILogger <MetricsEventManager> logger) { MetricsEventGenerator = generator; appServiceOptionsMonitor.OnChange(newOptions => _appServiceOptions = newOptions); _appServiceOptions = appServiceOptionsMonitor.CurrentValue; _functionActivityFlushInterval = functionActivityFlushInterval; if (linuxContainerActivityPublisher != null && linuxContainerActivityPublisher != NullLinuxContainerActivityPublisher.Instance) { _linuxContainerActivityPublisher = linuxContainerActivityPublisher; } if (metricsPublisher != null && metricsPublisher != NullMetricsPublisher.Instance) { _metricsPublisher = metricsPublisher; } _activityFlushCounter = _functionActivityFlushInterval; _activityTimer = new Timer(TimerFlush, null, _activityTimerIntervalMS, _activityTimerIntervalMS); _logger = logger; }
private static void CreateHost(HostConfigurator config, out AppServiceOptions serviceOptions) { // Чтение параметров командной строки var parameters = config.SelectPlatform(i => i.AddStringParameter("packageId") .AddStringParameter("packageVersion") .AddStringParameter("packageInstance") .AddStringParameter("packageDirectory") .AddStringParameter("packageTimeout") .AddStringParameter("startOptions")); serviceOptions = ParseServiceOptions(parameters); // Установка текущего каталога приложения Directory.SetCurrentDirectory(serviceOptions.PackageDirectory); // Установка таймаута для запуска и остановки var serviceTimeout = TimeSpan.MaxValue; if (serviceOptions.PackageTimeout != null) { serviceTimeout = TimeSpan.FromSeconds(serviceOptions.PackageTimeout.Value); config.SetStartTimeout(serviceTimeout); config.SetStopTimeout(serviceTimeout); } config.Service <AppServiceHost>(s => { // Создание экземпляра приложения s.ConstructUsing(hostSettings => { try { var instance = new AppServiceHost(); return(instance); } catch (Exception exception) { Console.WriteLine(exception); throw; } }); // Запуск экземпляра приложения s.WhenStarted((instance, hostControl) => { try { instance.Start(serviceTimeout); return(true); } catch (Exception exception) { Console.WriteLine(exception); throw; } }); // Остановка экземпляра приложения s.WhenStopped((instance, hostControl) => { try { instance.Stop(serviceTimeout); return(true); } catch (Exception exception) { Console.WriteLine(exception); throw; } }); }); // Установка имени службы var serviceName = GetServiceName(serviceOptions); config.SetServiceName(serviceName); config.SetDisplayName(serviceName); config.SetDescription(serviceName); // Установка экземпляра службы var serviceInstance = serviceOptions.PackageInstance; if (!string.IsNullOrWhiteSpace(serviceInstance)) { config.SetInstanceName(serviceInstance); } }
private static string GetServiceName(AppServiceOptions serviceOptions) { return($"{serviceOptions.PackageId}.{serviceOptions.PackageVersion}"); }