Esempio n. 1
0
        /// <summary>
        /// Adds a sink that sends log events to Datadog.
        /// </summary>
        /// <param name="loggerConfiguration">The logger configuration.</param>
        /// <param name="apiKey">Your Datadog API key.</param>
        /// <param name="source">The integration name.</param>
        /// <param name="service">The service name.</param>
        /// <param name="host">The host name.</param>
        /// <param name="tags">Custom tags.</param>
        /// <param name="configuration">The Datadog logs client configuration.</param>
        /// <param name="configurationSection">A config section defining the datadog configuration.</param>
        /// <param name="logLevel">The minimum log level for the sink.</param>
        /// <param name="batchSizeLimit">The maximum number of events to emit in a single batch.</param>
        /// <param name="batchPeriod">The time to wait before emitting a new event batch.</param>
        /// <param name="queueLimit">
        /// Maximum number of events to hold in the sink's internal queue, or <c>null</c>
        /// for an unbounded queue. The default is <c>10000</c>
        /// </param>
        /// <param name="exceptionHandler">This function is called when an exception occurs when using
        /// DatadogConfiguration.UseTCP=false (the default configuration)</param>
        /// <param name="detectTCPDisconnection">Detect when the TCP connection is lost and recreate a new connection.</param>
        /// <returns>Logger configuration</returns>
        /// <exception cref="ArgumentNullException">A required parameter is null.</exception>
        public static LoggerConfiguration DatadogLogs(
            this LoggerSinkConfiguration loggerConfiguration,
            string apiKey,
            string source  = null,
            string service = null,
            string host    = null,
            string[] tags  = null,
            DatadogConfiguration configuration         = null,
            IConfigurationSection configurationSection = null,
            LogEventLevel logLevel = LevelAlias.Minimum,
            int?batchSizeLimit     = null,
            TimeSpan?batchPeriod   = null,
            int?queueLimit         = null,
            Action <Exception> exceptionHandler = null,
            bool detectTCPDisconnection         = false, IDatadogClient client = null)
        {
            if (loggerConfiguration == null)
            {
                throw new ArgumentNullException(nameof(loggerConfiguration));
            }
            if (string.IsNullOrWhiteSpace(apiKey))
            {
                throw new ArgumentNullException(nameof(apiKey));
            }

            var config = ApplyMicrosoftExtensionsConfiguration.ConfigureDatadogConfiguration(configuration, configurationSection);
            var sink   = DatadogSink.Create(apiKey, source, service, host, tags, config, batchSizeLimit, batchPeriod, queueLimit, exceptionHandler, detectTCPDisconnection, client);

            return(loggerConfiguration.Sink(sink, logLevel));
        }
        protected override void InitializeTarget()
        {
            if (Port == 0)
            {
                Port = UseSSL ? DDPort : DDPortNoSSL;
            }

            base.InitializeTarget();

            var useTCP = UseTCP;

            if (Uri.TryCreate(Url, UriKind.Absolute, out var uri) && uri?.Scheme?.StartsWith("http") == true)
            {
                useTCP = false;
            }

            if (useTCP)
            {
                _client = new DatadogTcpClient(Url, Port, UseSSL, ApiKey);
            }
            else
            {
                _client = new DatadogHttpClient(Url, ApiKey);
            }
        }
Esempio n. 3
0
        public DatadogReporter(
            MetricsReportingDatadogOptions options,
            IDatadogClient datadogClient)
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            if (options.FlushInterval < TimeSpan.Zero)
            {
                throw new InvalidOperationException($"{nameof(MetricsReportingDatadogOptions.FlushInterval)} must not be less than zero");
            }

            _datadogClient = datadogClient ?? throw new ArgumentNullException(nameof(datadogClient));

            Formatter = options.MetricsOutputFormatter ?? new MetricsDatadogJsonOutputFormatter(options.FlushInterval);

            FlushInterval = options.FlushInterval > TimeSpan.Zero
                ? options.FlushInterval
                : AppMetricsConstants.Reporting.DefaultFlushInterval;

            Filter = options.Filter;
            Logger.Info($"Using Metrics Reporter {this}. Url: {options.Datadog.BaseUri} FlushInterval: {FlushInterval}");
        }
Esempio n. 4
0
 public DatadogSink(string apiKey, string source, string service, string host, string[] tags,
                    DatadogConfiguration config, int queueLimit, int?batchSizeLimit = null, TimeSpan?batchPeriod = null,
                    Action <Exception> exceptionHandler = null)
     : base(batchSizeLimit ?? DefaultBatchSizeLimit, batchPeriod ?? DefaultBatchPeriod, queueLimit)
 {
     _client           = CreateDatadogClient(apiKey, source, service, host, tags, config);
     _exceptionHandler = exceptionHandler;
 }
        /// <summary>
        /// Free resources held by the sink.
        /// </summary>
        /// <param name="disposing">If true, called because the object is being disposed; if false,
        /// the object is being disposed from the finalizer.</param>
        protected override void Dispose(bool disposing)
        {
            _batchTimer.Dispose();
            _client.Close();

            _client     = null;
            _batchTimer = null;

            base.Dispose(disposing);
        }
Esempio n. 6
0
 public DatadogSink(string apiKey, string source, string service, string host, string[] tags, DatadogConfiguration config) : base(BatchSizeLimit, Period)
 {
     if (config.UseTCP)
     {
         _client = new DatadogTcpClient(config, new LogFormatter(source, service, host, tags), apiKey);
     }
     else
     {
         _client = new DatadogHttpClient(config, new LogFormatter(source, service, host, tags), apiKey);
     }
 }
 private void EnsureClient()
 {
     if (_client == null)
     {
         var configuration = new DatadogConfiguration(Url, Port, UseSSL, UseTCP);
         if (UseTCP)
         {
             _client = new DatadogTcpClient(configuration, new LogFormatter(Source, Service, Host, Tags.Split(',')), ApiKey);
         }
         else
         {
             _client = new DatadogHttpClient(configuration, new LogFormatter(Source, Service, Host, Tags != null ? Tags.Split(',') : null), ApiKey);
         }
     }
 }
        public static DatadogSink Create(
            string apiKey,
            string source,
            string service,
            string host,
            string[] tags,
            DatadogConfiguration config,
            int?batchSizeLimit   = null,
            TimeSpan?batchPeriod = null,
            int?queueLimit       = null,
            Action <Exception> exceptionHandler = null,
            bool detectTCPDisconnection         = false, IDatadogClient client = null)
        {
            if (queueLimit.HasValue)
            {
                return(new DatadogSink(apiKey, source, service, host, tags, config, queueLimit.Value, batchSizeLimit, batchPeriod, exceptionHandler, detectTCPDisconnection, client));
            }

            return(new DatadogSink(apiKey, source, service, host, tags, config, batchSizeLimit, batchPeriod, exceptionHandler, detectTCPDisconnection, client));
        }
 public DatadogSink(string apiKey, string source, string service, string host, string[] tags, DatadogConfiguration config, int?batchSizeLimit = null, TimeSpan?batchPeriod = null, Action <Exception> exceptionHandler = null, bool detectTCPDisconnection = false, IDatadogClient client = null)
     : base(batchSizeLimit ?? DefaultBatchSizeLimit, batchPeriod ?? DefaultBatchPeriod)
 {
     _client           = client ?? CreateDatadogClient(apiKey, source, service, host, tags, config, detectTCPDisconnection);
     _exceptionHandler = exceptionHandler;
 }