Ejemplo n.º 1
0
        private IMeter GetMetric(string scope)
        {
            string key = scope;

            if (string.IsNullOrWhiteSpace(scope) == true)
            {
                key = GlobalKey;
            }

            var settings = _configCache.GetExistingOrCreate(key, () => GetSettings(scope));

            if (settings == null || settings.Count == 0)
            {
                return(NullMeter.Instance);
            }
            return(CreateMeter(scope, settings));
        }
Ejemplo n.º 2
0
        private IMeter CreateMeter(string scope, NameValueCollection settings)
        {
            int port = 0, mtu = 1432, batchingInterval = 1;
            var applyPrefix  = settings["applyPrefix"] ?? string.Empty;
            var tenantPrefix = settings["prefix"] ?? string.Empty;

            var @namespace = string.Empty;
            // Should apply prefix if
            var shouldApplyPrefix =
                string.IsNullOrWhiteSpace(applyPrefix) == true ||
                "|no|n|false|0|off|".IndexOf(applyPrefix, StringComparison.OrdinalIgnoreCase) == -1;
            var isGlobal = string.IsNullOrWhiteSpace(scope);

            if (isGlobal == false && shouldApplyPrefix == true)
            {
                if (string.IsNullOrWhiteSpace(tenantPrefix) == false)
                {
                    @namespace = tenantPrefix + "." + _application;
                }
                else
                {
                    @namespace = scope + "." + _application;
                }
            }
            else
            {
                @namespace = _application;
            }

            var host = settings["host"];

            if (string.IsNullOrWhiteSpace(host) == true)
            {
                return(NullMeter.Instance);
            }
            if (int.TryParse(settings["port"], out port) == false)
            {
                return(NullMeter.Instance);
            }
            EndPoint  endpoint = null;
            IPAddress ip;

            if (IPAddress.TryParse(host, out ip) == true)
            {
                endpoint = new IPEndPoint(ip, port);
            }
            else
            {
                endpoint = new DnsEndPoint(host, port);
            }


            if (int.TryParse(settings["mtu"], out mtu) == false)
            {
                mtu = 1432;
            }
            if (int.TryParse(settings["batchingInterval"], out batchingInterval) == false)
            {
                batchingInterval = 1;
            }

            var key = $"{host}:{port}";

            return(_meterCache.GetExistingOrCreate(key, () => new AsyncStatsDMeter(new StatsdMeter(endpoint, @namespace, mtu, batchingInterval))));
        }