コード例 #1
0
ファイル: Program.cs プロジェクト: veyalla/MetricsCollector
        /// <summary>
        ///     Initializes the ModuleClient and sets up the callback to receive
        ///     messages containing temperature information
        /// </summary>
        private static async Task Init()
        {
            var mqttSetting = new MqttTransportSettings(TransportType.Mqtt_Tcp_Only);

            ITransportSettings[] settings = { mqttSetting };

            // Open a connection to the Edge runtime
            var ioTHubModuleClient = await ModuleClient.CreateFromEnvironmentAsync(settings);

            await ioTHubModuleClient.OpenAsync();

            Console.WriteLine("IoT Hub module client initialized.");

            var configuration = await GetConfiguration(ioTHubModuleClient);

            Console.WriteLine($"Obtained configuration: {configuration}");

            var identifier = Environment.GetEnvironmentVariable("MessageIdentifier") ?? "IoTEdgeMetrics";

            Console.WriteLine($"Using message identifier {identifier}");

            var messageFormatter = new MessageFormatter(configuration.MetricsFormat, identifier);
            var scraper          = new Scraper(configuration.Endpoints.Values.ToList());

            IMetricsSync metricsSync;

            if (configuration.SyncTarget == SyncTarget.AzureLogAnalytics)
            {
                string workspaceId = Environment.GetEnvironmentVariable("AzMonWorkspaceId") ??
                                     Environment.GetEnvironmentVariable("azMonWorkspaceId") ?? // Workaround for IoT Edge k8s bug
                                     throw new Exception("AzMonWorkspaceId env var not set!");

                string wKey = Environment.GetEnvironmentVariable("AzMonWorkspaceKey") ??
                              Environment.GetEnvironmentVariable("azMonWorkspaceKey") ??
                              throw new Exception("AzMonWorkspaceKey env var not set!");

                string clName = Environment.GetEnvironmentVariable("AzMonCustomLogName") ??
                                Environment.GetEnvironmentVariable("azMonCustomLogName") ??
                                "promMetrics";

                metricsSync = new LogAnalyticsMetricsSync(messageFormatter, scraper, new AzureLogAnalytics(workspaceId, wKey, clName));
            }
            else
            {
                metricsSync = new IoTHubMetricsSync(messageFormatter, scraper, ioTHubModuleClient);
            }

            var scrapingInterval = TimeSpan.FromSeconds(configuration.ScrapeFrequencySecs);

            ScrapingTimer = new Timer(ScrapeAndSync, metricsSync, scrapingInterval, scrapingInterval);
        }
コード例 #2
0
 public IoTHubMetricsSync(MessageFormatter messageFormatter, Scraper scraper, ModuleClient moduleClient)
 {
     this.messageFormatter = messageFormatter ?? throw new ArgumentNullException(nameof(messageFormatter));
     this.scraper          = scraper ?? throw new ArgumentNullException(nameof(scraper));
     this.moduleClient     = moduleClient;
 }
コード例 #3
0
 public LogAnalyticsMetricsSync(MessageFormatter messageFormatter, Scraper scraper, AzureLogAnalytics logAnalytics)
 {
     this.messageFormatter = messageFormatter ?? throw new ArgumentNullException(nameof(messageFormatter));
     this.scraper          = scraper ?? throw new ArgumentNullException(nameof(scraper));
     this.logAnalytics     = logAnalytics;
 }