private async Task SetDeployedApplicationReplicaOrInstanceListAsync( Uri applicationNameFilter = null, string applicationType = null) { DeployedApplicationList deployedApps; if (applicationNameFilter != null) { deployedApps = await FabricClientInstance.QueryManager.GetDeployedApplicationListAsync(NodeName, applicationNameFilter).ConfigureAwait(true); } else { deployedApps = await FabricClientInstance.QueryManager.GetDeployedApplicationListAsync(NodeName).ConfigureAwait(true); if (deployedApps.Count > 0 && !string.IsNullOrEmpty(applicationType)) { for (int i = 0; i < deployedApps.Count; i++) { if (deployedApps[i].ApplicationTypeName == applicationType) { continue; } deployedApps.Remove(deployedApps[i]); --i; } } } var currentReplicaInfoList = new List <ReplicaOrInstanceMonitoringInfo>(); foreach (var deployedApp in deployedApps) { List <string> filteredServiceList = null; var appFilter = this.userTargetList.Where(x => (x.TargetApp != null || x.TargetAppType != null) && (x.TargetApp?.ToLower() == deployedApp.ApplicationName?.OriginalString.ToLower() || x.TargetAppType?.ToLower() == deployedApp.ApplicationTypeName?.ToLower()) && (!string.IsNullOrEmpty(x.ServiceExcludeList) || !string.IsNullOrEmpty(x.ServiceIncludeList)))?.FirstOrDefault(); // Filter service list if include/exclude service(s) config setting is supplied. var filterType = ServiceFilterType.None; if (appFilter != null) { if (!string.IsNullOrEmpty(appFilter.ServiceExcludeList)) { filteredServiceList = appFilter.ServiceExcludeList.Split(',').ToList(); filterType = ServiceFilterType.Exclude; } else if (!string.IsNullOrEmpty(appFilter.ServiceIncludeList)) { filteredServiceList = appFilter.ServiceIncludeList.Split(',').ToList(); filterType = ServiceFilterType.Include; } } var replicasOrInstances = await GetDeployedPrimaryReplicaAsync( deployedApp.ApplicationName, filteredServiceList, filterType, applicationType).ConfigureAwait(true); ReplicaOrInstanceList.AddRange(replicasOrInstances); this.deployedTargetList.AddRange(this.userTargetList.Where( x => (x.TargetApp != null || x.TargetAppType != null) && (x.TargetApp?.ToLower() == deployedApp.ApplicationName?.OriginalString.ToLower() || x.TargetAppType?.ToLower() == deployedApp.ApplicationTypeName?.ToLower()))); } }
// This runs each time ObserveAsync is run to ensure that any new app targets and config changes will // be up to date across observer loop iterations. private async Task <bool> InitializeAsync() { if (ReplicaOrInstanceList == null) { ReplicaOrInstanceList = new List <ReplicaOrInstanceMonitoringInfo>(); } if (!IsTestRun) { configSettings.Initialize( FabricServiceContext.CodePackageActivationContext.GetConfigurationPackageObject( ObserverConstants.ObserverConfigurationPackageName)?.Settings, ConfigurationSectionName, "AppObserverDataFileName"); } // For unit tests, this path will be an empty string and not generate an exception. var appObserverConfigFileName = Path.Combine( ConfigPackagePath ?? string.Empty, configSettings.AppObserverConfigFileName ?? string.Empty); if (!File.Exists(appObserverConfigFileName)) { WriteToLogWithLevel( ObserverName, $"Will not observe resource consumption as no configuration parameters have been supplied. | {NodeName}", LogLevel.Information); return(false); } // This code runs each time ObserveAsync is called, // so clear app list and deployed replica/instance list in case a new app has been added to watch list. if (this.userTargetList.Count > 0) { this.userTargetList.Clear(); ReplicaOrInstanceList.Clear(); } if (this.deployedTargetList.Count > 0) { this.deployedTargetList.Clear(); } using Stream stream = new FileStream( appObserverConfigFileName, FileMode.Open, FileAccess.Read, FileShare.Read); if (stream.Length > 0 && JsonHelper.IsJson <List <ApplicationInfo> >(File.ReadAllText(appObserverConfigFileName))) { this.userTargetList.AddRange(JsonHelper.ReadFromJsonStream <ApplicationInfo[]>(stream)); } // Are any of the config-supplied apps deployed?. if (this.userTargetList.Count == 0) { WriteToLogWithLevel( ObserverName, $"Will not observe resource consumption as no configuration parameters have been supplied. | {NodeName}", LogLevel.Information); return(false); } int settingSFail = 0; foreach (var application in this.userTargetList) { if (string.IsNullOrWhiteSpace(application.TargetApp) && string.IsNullOrWhiteSpace(application.TargetAppType)) { HealthReporter.ReportFabricObserverServiceHealth( FabricServiceContext.ServiceName.ToString(), ObserverName, HealthState.Warning, $"Initialize() | {application.TargetApp}: Required setting, target, is not set."); settingSFail++; continue; } // No required settings supplied for deployed application(s). if (settingSFail == this.userTargetList.Count) { return(false); } if (!string.IsNullOrEmpty(application.TargetAppType)) { await SetDeployedApplicationReplicaOrInstanceListAsync( null, application.TargetAppType).ConfigureAwait(false); } else { await SetDeployedApplicationReplicaOrInstanceListAsync(new Uri(application.TargetApp)) .ConfigureAwait(false); } } foreach (var repOrInst in ReplicaOrInstanceList) { ObserverLogger.LogInfo( $"Will observe resource consumption by {repOrInst.ApplicationName?.OriginalString} " + $"on Node {NodeName}."); } return(true); }