public static void Main(string[] args) { CommandLine.Parser.Default.ParseArguments <Config>(args) .WithParsed(options => { var collectorConfig = new CollectorConfiguration() .WriteTo.InfluxDB(options.InfluxUri, options.InfluxDatabase, options.InfluxUsername, options.InfluxPassword); CollectorLog.RegisterErrorHandler((message, exception) => { Console.Error.WriteLine($"Error when recording influx stats: \"{message}\""); Console.Error.WriteLine(exception); }); using (var influxCollector = collectorConfig.CreateCollector()) { if (options.Mode.Equals(PriceMode, StringComparison.InvariantCultureIgnoreCase)) { RecordCurrentPrice(options, influxCollector).GetAwaiter().GetResult(); } else if (options.Mode.Equals(UsageSimpleMode, StringComparison.InvariantCultureIgnoreCase)) { RecordSimpleHistoricUsage(options, influxCollector).GetAwaiter().GetResult(); } else if (options.Mode.Equals(UsageDetailedMode, StringComparison.InvariantCultureIgnoreCase)) { RecordDetailedHistoricUsage(options, influxCollector).GetAwaiter().GetResult(); } else { Console.Error.WriteLine($"Unrecognized mode \"{options.Mode}\" A valid --mode of either \"{PriceMode}\", \"{UsageSimpleMode}\" or \"{UsageDetailedMode}\" must be specified"); Environment.Exit(1); } } }); }
private static void LogWeatherData(Object source, ElapsedEventArgs e) { var weather = FetchWeatherData(); CollectorLog.RegisterErrorHandler((message, exception) => { Console.WriteLine($"{message}: {exception}"); }); Metrics.Collector = new CollectorConfiguration() .Tag.With("location", Zipcode) .WriteTo.InfluxDB(InfluxDBUrl, "weather", InfluxDBUsername, InfluxDBPassword) .CreateCollector(); Console.WriteLine("Writing data to InfluxDb..."); Metrics.Write("weather", new Dictionary <string, object> { { "temperature", weather.Temperature }, { "pressure", weather.Pressure }, { "humidity", weather.Humidity }, { "wind-speed", weather.WindSpeed }, { "wind-direction", weather.WindDirection }, { "clouds", weather.Clouds } }); Metrics.Collector.Dispose(); }
public HubitatClient(HubitatOptions options, ILogger logger) { _options = options; _logger = logger; var socketOptions = new PureWebSocketOptions() { DebugMode = true, SendDelay = 100, IgnoreCertErrors = true, MyReconnectStrategy = new ReconnectStrategy(options.MinReconnectInterval, options.MaxReconnectInterval, options.MaxReconnectAttempts) }; _webSocket = new PureWebSocket(_options.WebSocketURL, socketOptions); _webSocket.OnOpened += (sender) => SocketOpen(); _webSocket.OnStateChanged += (sender, newState, previousState) => SocketStateChanged(newState, previousState); _webSocket.OnMessage += (sender, message) => MessageReceived(message); _webSocket.OnClosed += (sender, reason) => SocketClosed(reason); _webSocket.OnSendFailed += (sender, data, ex) => SocketSendFailed(data, ex); _webSocket.OnError += (sender, e) => SocketError(e); _collector = Metrics.Collector = new CollectorConfiguration() .Batch.AtInterval(TimeSpan.FromSeconds(options.BatchInterval)) .WriteTo.InfluxDB(options.InfluxDbURL, options.InfluxDbDatabase, options.InfluxDbUsername, options.InfluxDbPassword) .CreateCollector(); CollectorLog.RegisterErrorHandler((string message, Exception ex) => { _logger.Error(ex, "Failed to write metrics to InfluxDB: {Message}", message); }); }
private static async Task Main(string[] args) { Console.WriteLine("Hello Old World!"); CollectorLog.RegisterErrorHandler((message, exception) => { Console.WriteLine($"{message}: {exception}"); }); var client = new LineProtocolClient(new Uri(DBUrl), DBName); var createEvents = new LineProtocolPayload(); for (var i = 0; i < PlanAmount; i++) { createEvents.Add(Create_PlanCreateDTO().ToLineProtocolPoint()); } var influxResult = await client.WriteAsync(createEvents); if (!influxResult.Success) { await Console.Error.WriteLineAsync(influxResult.ErrorMessage); } var changeEvents = new LineProtocolPayload(); PlanIDs.ForEach(planId => { for (var i = 0; i < ChangeEventPerPlanAmount; i++) { changeEvents.Add(Create_PlanChangeValueDTO(planId, i).ToLineProtocolPoint()); } }); influxResult = await client.WriteAsync(changeEvents); if (!influxResult.Success) { await Console.Error.WriteLineAsync(influxResult.ErrorMessage); } var deleteEvents = new LineProtocolPayload(); PlanIDs.ForEach(planId => { deleteEvents.Add(new PlanDeleteDTO { PlanId = planId, UserId = "1234567", PlanVersion = new Version(1, 2).ToString() }.ToLineProtocolPoint()); }); influxResult = await client.WriteAsync(deleteEvents); if (!influxResult.Success) { await Console.Error.WriteLineAsync(influxResult.ErrorMessage); } }
public InfluxDBRepository(string uri, string database, string measurement) { this.uri = uri; this.database = database; this.measurement = measurement; CollectorLog.RegisterErrorHandler((message, exception) => { throw new Exception(message, exception); }); }
public void ListenerCanUnregister() { var invocationCount = 0; using (CollectorLog.RegisterErrorHandler((message, exception) => invocationCount++)) { CollectorLog.ReportError("", null); } CollectorLog.ReportError("", null); Assert.Equal(1, invocationCount); }
public InfluxCollector(IConfiguration configuration, ILogger <InfluxCollector> logger) { var url = configuration.GetValue <string>("influxdb:url"); var database = configuration.GetValue <string>("influxdb:database"); Metrics.Collector = new CollectorConfiguration() .Batch.AtInterval(TimeSpan.FromSeconds(2)) .WriteTo.InfluxDB(url, database) .CreateCollector(); CollectorLog.RegisterErrorHandler((message, exception) => logger.LogError(exception, message)); }
public MetricsCollectorWrapper(string serverBaseAddress, string database, TimeSpan interval) { Collector = new CollectorConfiguration() .Batch.AtInterval(interval) .WriteTo.InfluxDB(serverBaseAddress, database) .CreateCollector(); CollectorLog.RegisterErrorHandler((message, exception) => { Console.Error.WriteLine($"[{DateTime.Now}] - {message}: {exception}"); }); }
public void SingleListenerGetsInvoked() { var invocationCount = 0; Exception dummyException = new Exception("Bang!"); const string errorMessage = "Bad things"; CollectorLog.RegisterErrorHandler((message, exception) => { Assert.Equal(errorMessage, message); Assert.Equal(dummyException, exception); invocationCount++; }); CollectorLog.ReportError(errorMessage, dummyException); Assert.Equal(1, invocationCount); }
private static async Task Main(string[] args) { CollectorLog.RegisterErrorHandler((message, exception) => { Console.WriteLine($"{message}: {exception}"); }); Console.WriteLine("Hello New World!"); var influxDBClient = InfluxDBClientFactory.Create(DbUrl, Token); using (var writeApi = influxDBClient.GetWriteApi()) { // create events for (var i = 0; i < PlanAmount; i++) { writeApi.WriteMeasurement(BucketName, OrgId, WritePrecision.Ns, Create_PlanCreateDTO()); } // change events PlanIDs.ForEach(planId => { for (var i = 0; i < ChangeEventPerPlanAmount; i++) { writeApi.WriteMeasurement(BucketName, OrgId, WritePrecision.Ns, Create_PlanChangeValueDTO(planId, i)); } }); // delete events PlanIDs.ForEach(planId => writeApi.WriteMeasurement(BucketName, OrgId, WritePrecision.Ns, new PlanDeleteDTO { PlanId = planId, UserId = "12345", PlanVersion = new Version(1, 2).ToString() }) ); writeApi.Dispose(); } await Read(influxDBClient); }
public void EnsureListenerExceptionPropagates() { CollectorLog.RegisterErrorHandler((message, exception) => { throw new Exception(""); }); var threw = false; try { CollectorLog.ReportError("", null); } catch { threw = true; } Assert.True(threw, "Excpected an exception thrown by the error handler to propagate to the caller"); }
public static MetricsCollector connection; // to InfluxDB public static void Initialize() { DotEnv.Config(); var envReader = new EnvReader(); var address = envReader.GetStringValue("INFLUX_ADDRESS"); var dbname = envReader.GetStringValue("INFLUX_DBNAME"); var username = envReader.GetStringValue("INFLUX_USERNAME"); var password = envReader.GetStringValue("INFLUX_PASSWORD"); connection = new CollectorConfiguration() .Tag.With("host", "client1") .Batch.AtInterval(TimeSpan.FromSeconds(5)) .WriteTo.InfluxDB(address, dbname, username, password) .CreateCollector(); CollectorLog.RegisterErrorHandler((m, e) => { Console.WriteLine($"{m}: {e}"); }); }
public InfluxDBRepository( string uri, string database, string measurement, string username = null, string password = null, Action <string, Exception> errorHandler = null) { this.uri = uri; this.database = database; this.measurement = measurement; this.username = username; this.password = password; metricsCollector = Metrics.Collector = new CollectorConfiguration() .Batch.AtInterval(TimeSpan.FromSeconds(2)) .WriteTo.InfluxDB(uri, database, username, password) .CreateCollector(); if (errorHandler != null) { CollectorLog.RegisterErrorHandler(errorHandler); } }
public void DisallowNullErrorHandlers() { Assert.Throws <ArgumentNullException>(() => CollectorLog.RegisterErrorHandler(null)); }
void Run(string[] args) { AppDomain.CurrentDomain.ProcessExit += (sender, eventArgs) => _active = false; Console.CancelKeyPress += (sender, eventArgs) => { eventArgs.Cancel = true; _active = false; }; var result = new ArgsParser(args) .Help("?", "help") .Comment("antimetrics is an application for a process health metrics collection (like cpu and memory consumptions) via API and ETW\n" + "counters and publishing them into an database for further analyses.") .Keys("v", "verbose").Tip("increase output verbosity").Flag(out _verbose) .Keys("no-proxy").Tip("disable proxy usage").Flag(out var noProxy) .Keys("with-influx").Tip("enable influx usage and set its address (ex. http://127.0.0.1:8086)").Value <string>(out var influxAddress, null) .Keys("with-dumps").Tip("enable auto dumping and set its directory (ex. C:\\dumps)").Value <string>(out _dumpsDirectory, null) .Name("process").Amount(1, int.MaxValue).Tip("list of process names for monitoring").Values <string>(out var processes) .Result(); if (result != null) { Console.WriteLine(result); return; } if (noProxy > 0) { HttpClient.DefaultProxy = new DisableSystemProxy(); } CollectorLog.RegisterErrorHandler((message, exception) => { if (_verbose >= 1) { Console.WriteLine($"{message}: {exception}"); } }); for (var i = 0; i < processes.Count; ++i) { if (FindProcessByPid(processes[i], out var process)) { _monitoredProcesses[processes[i]] = new ProcessState { Pid = process.Id, Name = process.ProcessName, Process = process, }; } else { _monitoredProcesses[processes[i]] = new ProcessState { Pid = FORBIDDEN_ID, Name = processes[i], }; } } foreach (var process in _monitoredProcesses.Values) { process.Concurrency = new long[Environment.ProcessorCount + 1]; process.ConcurrencyLast = 0; process.ConcurrencyCounter = 0; process.Threads = new ConcurrentDictionary <int, ThreadState>(); if (influxAddress != null) { process.Collector = new CollectorConfiguration() .Tag.With("host", Environment.GetEnvironmentVariable("COMPUTERNAME")) .Tag.With("app", process.Name) .Batch.AtInterval(TimeSpan.FromSeconds(5)) .WriteTo.InfluxDB(influxAddress, DB_NAME) .CreateCollector(); } process.Process ??= Process.GetProcesses().FirstOrDefault(x => x.ProcessName == process.Name); if (process.Process != null) { Console.WriteLine("Process {0} is already active, pid {1}", process.Name, process.Process.Id); process.Pid = process.Process.Id; _activeProcesses[process.Pid] = process; } else { Console.WriteLine("Process {0} is not found, waiting...", process.Name); } } _nextReport = Environment.TickCount64 + REP_INTERVAL; _reportId = 0; _active = true; // ETW works quite unstable for PerformanceCounters and MemInfo events, so get them through good old API var apiCollector = new Thread(ApiCollector) { Name = "API Collector" }; apiCollector.Start(); var etwCollector = new Thread(EtwCollector) { Name = "ETW Collector" }; etwCollector.Start(); MainWorker(); _session.Stop(); apiCollector.Join(); etwCollector.Join(); }