Example #1
0
        /// <inheritdoc/>
        public Task <IDiagnosticEventSink> CreateDiagnosticEventSink(IConfiguration configuration)
        {
            var uri      = configuration?.GetValue <Uri>("uri");
            var database = configuration?["database"];

            if (uri == null)
            {
                throw new ConfigurationErrorsException("'uri' attribute is required (e.g. http://localhost:8086)");
            }
            if (string.IsNullOrWhiteSpace(database))
            {
                throw new ConfigurationErrorsException("'database' attribute is required");
            }

            var options = new InfluxDBOptions(uri, database)
            {
                Measurement = configuration["measurement"],
                Username    = configuration["username"],
                Password    = configuration["password"]
            };

            var precision = configuration["precision"];

            if (!string.IsNullOrWhiteSpace(precision))
            {
                options.Precision = InfluxDBPrecision.Parse(precision);
            }

            var tags = configuration["tags"];

            if (!string.IsNullOrWhiteSpace(tags))
            {
                options.Tags = tags.Split(',')
                               .Select(tag =>
                {
                    var keyValue = tag.Split('=');
                    var key      = keyValue.FirstOrDefault();
                    var value    = keyValue.Skip(1).FirstOrDefault();
                    return(new KeyValuePair <string, string>(key, value));
                })
                               .ToDictionary(tag => tag.Key, tag => tag.Value);
            }

            var sampleRate    = default(TimeSpan);
            var sampleRateStr = configuration["sampleRate"];

            if (!string.IsNullOrWhiteSpace(sampleRateStr))
            {
                if (!TimeSpan.TryParse(sampleRateStr, out sampleRate))
                {
                    throw new ConfigurationErrorsException("Invalid timespan specified for 'sampleRate'");
                }
            }

            return(Task.FromResult <IDiagnosticEventSink>(new InfluxDBSink(options, sampleRate)));
        }
Example #2
0
        /// <summary>
        /// Initializes a new <see cref="InfluxDBSink"/>
        /// </summary>
        /// <param name="options">Options specifying connection parameters and details governing
        /// how points are written to the InfluxDB database</param>
        /// <param name="sampleRate">(Optional) The rate at which samples should be taken</param>
        public InfluxDBSink(InfluxDBOptions options, TimeSpan sampleRate = default(TimeSpan))
        {
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            _measurement = string.IsNullOrWhiteSpace(options.Measurement)
                ? InfluxDBOptions.DefaultMeasurement
                : options.Measurement.Trim();

            _precision = options.Precision ?? InfluxDBPrecision.Nanosecond;

            _sampleRate = sampleRate <= TimeSpan.Zero
                ? TimeSpan.FromSeconds(5)
                : sampleRate;

            var parameters = new Dictionary <string, string>();

            parameters["db"]        = options.Database;
            parameters["u"]         = options.Username;
            parameters["p"]         = options.Password;
            parameters["precision"] = _precision.Name;

            var queryString = string.Join("&", parameters
                                          .Where(param => !string.IsNullOrWhiteSpace(param.Value))
                                          .Select(param => param.Key + "=" + param.Value));

            _client = new HttpClient
            {
                BaseAddress = new UriBuilder(options.Uri)
                {
                    Path  = "write",
                    Query = queryString
                }.Uri
            };

            var myTags    = options.Tags.Any() ? options.Tags : DefaultTags;
            var validTags = myTags.Where(tag => !string.IsNullOrWhiteSpace(tag.Key));

            _tagSet = string.Join(",", validTags.Select(tag => TagEscape(tag.Key) + "=" + TagEscape(tag.Value)));
        }