コード例 #1
0
ファイル: InfluxDbObserver.cs プロジェクト: hyperdom/Okanshi
 /// <summary>
 /// Method used to write metrics to InfluxDB. This method are not meant to be used externally.
 /// </summary>
 /// <param name="metrics"></param>
 public async Task Update(IEnumerable <Metric> metrics)
 {
     try
     {
         var groupedMetrics = metrics.GroupBy(options.DatabaseSelector).ToList();
         Logger.Debug($"Metrics will be sent to the following databases: {string.Join(", ", groupedMetrics.Count)}");
         foreach (var metricGroup in groupedMetrics)
         {
             var groupedByRetention = metricGroup.GroupBy(x => options.RetentionPolicySelector(x, metricGroup.Key)).ToList();
             Logger.Debug($"Metrics will for '{metricGroup.Key}' will be sent the following retentions polices: {string.Join(", ", groupedByRetention.Count)}");
             foreach (var retentionGroup in groupedByRetention)
             {
                 var points = ConvertToPoints(retentionGroup);
                 await client.WriteAsync(retentionGroup.Key, metricGroup.Key, points).ContinueWith(t => {
                     if (t.IsFaulted)
                     {
                         Logger.Error("Exception while sending metrics to InfluxDB", t.Exception);
                     }
                 });
             }
         }
     }
     catch (Exception e)
     {
         Logger.Error("Exception while sending metrics to InfluxDB", e);
     }
 }
コード例 #2
0
        public QueuedInfluxDbProcessor(IInfluxDbClient client, InfluxDbMeasurementMappings dbMappings)
        {
            Ensure.That(client, nameof(client)).IsNotNull();
            Ensure.That(dbMappings, nameof(dbMappings)).IsNotNull();

            _worker = Task.Run(async() =>
            {
                while (!_cancellationTokenSource.IsCancellationRequested)
                {
                    SpinWait.SpinUntil(() => !_queue.IsEmpty);

                    foreach (var measurement in CheckoutMeasurements())
                    {
                        var databaseInfo = dbMappings.GetDbTargetInfoOrDefault(measurement);
                        await client.WriteAsync(
                            databaseInfo.DatabaseName,
                            measurement.Points.ToInfluxPoints(),
                            databaseInfo.WriteOptions).ForAwait();
                    }
                }
            }, _cancellationTokenSource.Token);
        }