/// <summary> /// Saves the current counters measurements. /// </summary> /// <param name="counters">current counters measurements to be saves.</param> protected override void Save(IEnumerable <Counter> counters) { if (_client == null || !_pushEnabled) { return; } try { var body = PrometheusCounterConverter.ToString(counters, null, null); using (HttpContent requestContent = new StringContent(body, Encoding.UTF8, "text/plain")) { HttpResponseMessage response = _client.PutAsync(_requestUri, requestContent).Result; if ((int)response.StatusCode >= 400) { _logger.Error("prometheus-counters", "Failed to push metrics to prometheus"); } } } catch (Exception ex) { _logger.Error("prometheus-counters", ex, "Failed to push metrics to prometheus"); } }
public string GetResourceUri(string correlationId, string resourceGroupName, string accountName, string accessKey, string databaseName, string collectionName) { try { var(databaseResourceId, collectionResourceId) = ServiceClient.GetResourceIDs(accountName, accessKey, databaseName, collectionName); return(string.Format(DocumentDBCollectionUriFormat, SubscriptionId, resourceGroupName, accountName, databaseResourceId, collectionResourceId)); } catch (Exception exception) { _logger.Error(correlationId, exception, $"GetResourceUri: Failed to get uri for parameters: resourceGroupName = '{resourceGroupName}', " + $"accountName = '{accountName}', accessKey = '{accessKey}, databaseName = '{databaseName}', collectionName = '{collectionName}'."); return(string.Empty); } }
private void LogTrace(string correlationId, string component, string operation, Exception error, long duration) { StringBuilder builder = new StringBuilder(); if (error != null) { builder.Append("Failed to execute "); } else { builder.Append("Executed "); } builder.Append(component); builder.Append('.'); builder.Append(operation); if (duration > 0) { builder.Append(" in " + duration + " msec"); } if (error != null) { _logger.Error(correlationId, error, builder.ToString()); } else { _logger.Log(_logLevel, correlationId, null, builder.ToString()); } }
protected async Task CreateSchemaAsync(string correlationId) { if (_schemaStatements == null || _schemaStatements.Count == 0) { return; } // If table already exists then exit if (await TableExistAsync(correlationId, _tableName)) { return; } _logger.Debug(correlationId, "Table {0} does not exist. Creating database objects...", _tableName); // Run all DML commands try { foreach (var dml in _schemaStatements) { await ExecuteNonQuery(correlationId, dml); } } catch (Exception ex) { _logger.Error(correlationId, ex, "Failed to autocreate database object"); throw; } }
public void SetReferences(IReferences references) { try { _logger.SetReferences(references); ClientSecret = ExtractClientSecret(ClientSecret); ClientCredentials = new CosmosDbClientCredentials(ClientId, ClientSecret, TenantId); ServiceClient = new CosmosDbServiceClient(ClientCredentials); } catch (Exception exception) { ServiceClient = null; _logger.Error("CosmosDbMetricsService", exception, $"SetReferences: Failed to initialize cosmos db metrics service."); } }
/// <summary> /// Adds instrumentation to error handling. /// </summary> /// <param name="correlationId">(optional) transaction id to trace execution through call chain.</param> /// <param name="methodName">a method name.</param> /// <param name="ex">Error that occured during the method call</param> /// <param name="rethrow">True to throw the exception</param> protected void InstrumentError(string correlationId, string methodName, Exception ex, bool rethrow = false) { _logger.Error(correlationId, ex, "Failed to execute {0} method", methodName); _counters.IncrementOne(methodName + ".exec_errors"); if (rethrow) { throw ex; } }
/// <summary> /// Adds instrumentation to error handling. /// </summary> /// <param name="correlationId">(optional) transaction id to trace execution through call chain.</param> /// <param name="methodName">a method name.</param> /// <param name="ex">Error that occured during the method call</param> /// <param name="rethrow">True to throw the exception</param> protected void InstrumentError(string correlationId, [CallerMemberName] string methodName = null, Exception ex = null, bool rethrow = false) { var typeName = GetType().Name; _logger.Error(correlationId, ex, "Failed to call {0} method of {1}", methodName, typeName); _counters.IncrementOne(typeName + "." + methodName + ".call_errors"); if (rethrow) { throw ex; } }
/// <summary> /// Saves the current counters measurements. /// </summary> /// <param name="counters">current counters measurements to be saves.</param> protected override void Save(IEnumerable <Counter> counters) { if (_client == null) { return; } try { var dimensions = new List <Dimension>(); dimensions.Add(new Dimension { Name = "InstanceID", Value = _instance }); var data = new List <MetricDatum>(); var now = DateTime.UtcNow; foreach (var counter in counters) { data.Add(GetCounterData(counter, now, dimensions)); if (data.Count >= 20) { _client.PutMetricDataAsync( new PutMetricDataRequest { Namespace = _source, MetricData = data } ).Wait(); data.Clear(); } } if (data.Count > 0) { _client.PutMetricDataAsync( new PutMetricDataRequest { Namespace = _source, MetricData = data } ).Wait(); } } catch (Exception ex) { _logger.Error("cloudwatch-counters", ex, "Failed to push metrics to prometheus"); } }
private async Task RunAsync(string correlationId, CancellationToken token) { _correlationId = correlationId; CaptureErrors(); //await StartAsync(correlationId, token); if (_config == null) { throw new InvalidStateException(correlationId, "NO_CONFIG", "Container was not configured"); } try { _logger.Trace(correlationId, "Starting container."); // Create references with configured components InitReferences(_references); _references.PutFromConfig(_config); // Reference and open components var components = _references.GetAll(); Referencer.SetReferences(_references, components); await Opener.OpenAsync(correlationId, _references.GetAll()); // Get reference to logger _logger = new CompositeLogger(_references); // Get reference to container info var infoDescriptor = new Descriptor("*", "container-info", "*", "*", "*"); _info = (ContextInfo)_references.GetOneRequired(infoDescriptor); _logger.Info(correlationId, "Container {0} started.", _info.Name); } catch (Exception ex) { _references = null; _logger.Error(correlationId, ex, "Failed to start container"); throw; } }
/// <summary> /// Opens the component. /// </summary> /// <param name="correlationId">(optional) transaction id to trace execution through call chain.</param> public async Task OpenAsync(string correlationId) { var options = await _connectionResolver.ResolveAsync(correlationId); var opts = ConnectionFactory.GetDefaultOptions(); opts.AllowReconnect = _retryConnect; opts.MaxReconnect = _maxReconnect; opts.ReconnectWait = _reconnectTimeout; var uri = options.GetAsString("uri"); opts.Servers = uri.Split(','); var username = options.GetAsString("username"); if (!string.IsNullOrEmpty(username)) { opts.User = username; var password = options.GetAsString("password"); opts.Password = password; } var token = options.GetAsString("token"); if (!string.IsNullOrEmpty(token)) { opts.Token = token; } try { var connection = new ConnectionFactory().CreateConnection(opts); _connection = connection; } catch (Exception ex) { _logger.Error(correlationId, ex, "Failed to connect to NATS broker at " + uri); throw ex; } _logger.Debug(correlationId, "Connected to NATS broker at " + uri); }
private async Task MsSqlWithSshOpenAsync(string correlationId) { var sshHost = _sshConfigs.GetAsNullableString("host"); var sshUsername = _sshConfigs.GetAsNullableString("username"); var sshPassword = _sshConfigs.GetAsNullableString("password"); var sshKeyFile = _sshConfigs.GetAsNullableString("key_file_path"); var sshKeepAliveInterval = _sshConfigs.GetAsNullableTimeSpan("keep_alive_interval"); var(sshClient, localPort) = ConnectSsh(sshHost, sshUsername, sshPassword, sshKeyFile, databaseServer: _databaseServer, databasePort: _databasePort, sshKeepAliveInterval: sshKeepAliveInterval); _sshClient = sshClient; _sshPort = localPort.ToString(); _config.Set("connection.host", "127.0.0.1"); _config.Set("connection.port", _sshPort); _connectionResolver.Configure(_config); var connectionString = await _connectionResolver.ResolveAsync(correlationId); _logger.Trace(correlationId, "Connecting to sqlserver..."); try { var settings = ComposeSettings(); var connString = connectionString.TrimEnd(';') + ";" + JoinParams(settings); _connection = new SqlConnection(connString); _databaseName = _connection.Database; // Try to connect await _connection.OpenAsync(); _logger.Debug(correlationId, "Connected to sqlserver database {0}", _databaseName); } catch (Exception ex) { _logger.Error(correlationId, ex.ToString()); throw new ConnectionException(correlationId, "CONNECT_FAILED", "Connection to sqlserver failed", ex); } }
public virtual void SetReferences(IReferences references) { try { _logger.SetReferences(references); var mongoDbConnectionUrl = new MongoUrlBuilder(ConnectionUri); AccountName = mongoDbConnectionUrl.Username; AccessKey = mongoDbConnectionUrl.Password; DatabaseName = mongoDbConnectionUrl.DatabaseName; _metricsService = references.GetOneRequired <ICosmosDbMetricsService>(new Descriptor("pip-services", "metrics-service", "*", "*", "*")); _lock = references.GetOneRequired <ILock>(new Descriptor("pip-services", "lock", "*", "*", "*")); _timer = new FixedRateTimer(PerformMonitorAsync, TimerInterval, DelayInterval); } catch (Exception exception) { _logger.Error("AbstractCosmosDbPersistenceThroughputMonitor", exception, $"Failed to configure the CosmosDb persistence throughput monitor with parameters '{_configParams}'."); } }
/// <summary> /// Opens the component. /// </summary> /// <param name="correlationId">(optional) transaction id to trace execution through call chain.</param> public async Task OpenAsync(string correlationId) { var options = await _connectionResolver.ResolveAsync(correlationId); var opts = new MqttClientOptionsBuilder() .WithClientId(_clientId); if (_keepAliveTimeout > 0) { opts.WithKeepAlivePeriod(TimeSpan.FromMilliseconds(_keepAliveTimeout)); } else { opts.WithNoKeepAlive(); } var uri = options.GetAsString("servers") ?? ""; var servers = uri.Split(','); foreach (var server in servers) { var host = server; var port = 1883; var pos = server.IndexOf(":"); if (pos > 0) { host = server.Substring(0, pos); Int32.TryParse(server.Substring(pos + 1), out port); } opts.WithTcpServer(host, port); } var username = options.GetAsString("username"); if (!string.IsNullOrEmpty(username)) { var password = options.GetAsString("password"); opts.WithCredentials(username, password); } //opts.SetAutoReconnect(c.retryConnect) //opts.SetConnectTimeout(time.Millisecond * time.Duration(c.connectTimeout)) //opts.SetConnectRetryInterval(time.Millisecond * time.Duration(c.reconnectTimeout)) var client = new MqttFactory().CreateMqttClient(); client.UseDisconnectedHandler(DisconnectedHandlerAsync); client.UseApplicationMessageReceivedHandler(MessageReceiveHandlerAsync); try { await client.ConnectAsync(opts.Build()); } catch (Exception ex) { _logger.Error(correlationId, ex, "Failed to connect to MQTT broker at " + uri); throw ex; } _connection = client; _clientOptions = opts.Build(); _logger.Debug(correlationId, "Connected to MQTT broker at " + uri); }
protected virtual void HandleError(string correlationId, string methodName, Exception ex) { _logger.Error(correlationId, ex, "Failed to execute {0}.{1}", Component, methodName); }
protected virtual void HandleError(string correlationId, string methodName, Exception ex) { _logger.Error(correlationId, ex, $"Failed to execute {methodName}"); }