예제 #1
0
        private static DogStatsdEndPoint BuildEndPoint(StatsdConfig config)
        {
            var statsdServerName = !string.IsNullOrEmpty(config.StatsdServerName)
                            ? config.StatsdServerName
                            : Environment.GetEnvironmentVariable(StatsdConfig.AgentHostEnvVar);

            var pipeName = !string.IsNullOrEmpty(config.PipeName)
                            ? config.PipeName
                            : Environment.GetEnvironmentVariable(StatsdConfig.AgentPipeNameEnvVar);

            if (string.IsNullOrEmpty(statsdServerName) && string.IsNullOrEmpty(pipeName))
            {
                // Ignore pipe name in the error message as its usage is internal only.
                throw new ArgumentNullException(
                          $"{nameof(config)}.{nameof(config.StatsdServerName)} and"
                          + $" {StatsdConfig.AgentHostEnvVar} environment variable not set");
            }

            return(new DogStatsdEndPoint
            {
                ServerName = statsdServerName,
                PipeName = pipeName,
                Port = GetPort(config.StatsdPort),
            });
        }
예제 #2
0
        private ITransport CreateTransport(DogStatsdEndPoint endPoint, StatsdConfig config)
        {
            var serverName = endPoint.ServerName;

            if (!string.IsNullOrEmpty(serverName))
            {
                if (serverName.StartsWith(UnixDomainSocketPrefix))
                {
                    serverName = serverName.Substring(UnixDomainSocketPrefix.Length);
                    var unixEndPoint = new UnixEndPoint(serverName);
                    return(_factory.CreateUnixDomainSocketTransport(
                               unixEndPoint,
                               config.Advanced.UDSBufferFullBlockDuration));
                }

                return(CreateUDPTransport(endPoint));
            }

            var pipeName = endPoint.PipeName;

            if (string.IsNullOrEmpty(pipeName))
            {
                throw new ArgumentException($"Error: empty {nameof(DogStatsdEndPoint)}");
            }

            return(_factory.CreateNamedPipeTransport(pipeName));
        }
예제 #3
0
        public StatsdData BuildStatsData(StatsdConfig config)
        {
            var endPoint       = BuildEndPoint(config);
            var transportData  = CreateTransportData(endPoint, config);
            var transport      = transportData.Transport;
            var globalTags     = GetGlobalTags(config);
            var serializers    = CreateSerializers(config.Prefix, globalTags, config.Advanced.MaxMetricsInAsyncQueue);
            var telemetry      = CreateTelemetry(serializers.MetricSerializer, config, globalTags, endPoint, transportData.Transport);
            var statsBufferize = CreateStatsBufferize(
                telemetry,
                transportData.Transport,
                transportData.BufferCapacity,
                config.Advanced,
                serializers,
                config.ClientSideAggregation);

            var metricsSender = new MetricsSender(
                statsBufferize,
                new RandomGenerator(),
                new StopWatchFactory(),
                telemetry,
                config.StatsdTruncateIfTooLong,
                config.Advanced.MaxMetricsInAsyncQueue * 2);

            return(new StatsdData(metricsSender, statsBufferize, transport, telemetry));
        }
예제 #4
0
        private Telemetry CreateTelemetry(
            MetricSerializer metricSerializer,
            StatsdConfig config,
            string[] globalTags,
            DogStatsdEndPoint dogStatsdEndPoint,
            ITransport transport)
        {
            var telemetryFlush = config.Advanced.TelemetryFlushInterval;

            if (telemetryFlush.HasValue)
            {
                var        assembly = typeof(StatsdBuilder).GetTypeInfo().Assembly;
                var        version  = assembly.GetName().Version.ToString();
                var        optionalTelemetryEndPoint = config.Advanced.OptionalTelemetryEndPoint;
                ITransport telemetryTransport        = transport;
                if (optionalTelemetryEndPoint != null && !dogStatsdEndPoint.AreEquals(optionalTelemetryEndPoint))
                {
                    telemetryTransport = CreateTransport(optionalTelemetryEndPoint, config);
                }

                return(_factory.CreateTelemetry(metricSerializer, version, telemetryFlush.Value, telemetryTransport, globalTags));
            }

            // Telemetry is not enabled
            return(new Telemetry());
        }
예제 #5
0
        public StatsdData BuildStatsData(StatsdConfig config)
        {
            var endPoint       = BuildEndPoint(config);
            var transportData  = CreateTransportData(endPoint, config);
            var transport      = transportData.Transport;
            var globalTags     = GetGlobalTags(config);
            var telemetry      = CreateTelemetry(config, globalTags, endPoint, transportData.Transport);
            var statsBufferize = CreateStatsBufferize(
                telemetry,
                transportData.Transport,
                transportData.BufferCapacity,
                config.Advanced);

            var serializers = CreateSerializers(config.Prefix, globalTags, config.Advanced.MaxMetricsInAsyncQueue);

#pragma warning disable 618
            // MANUAL CHANGE: Obsolete ignore because this is internal code
            var metricsSender = new MetricsSender(
                statsBufferize,
                new RandomGenerator(),
                new StopWatchFactory(),
                serializers,
                telemetry,
                config.StatsdTruncateIfTooLong);
#pragma warning restore 618

            return(new StatsdData(metricsSender, statsBufferize, transport, telemetry));
        }
예제 #6
0
        private string[] GetGlobalTags(StatsdConfig config)
        {
            var globalTags = new List <string>();

            if (config.ConstantTags != null)
            {
                globalTags.AddRange(config.ConstantTags);
            }

            AddTag(globalTags, _entityIdInternalTagKey, StatsdConfig.EntityIdEnvVar);
            AddTag(globalTags, StatsdConfig.ServiceTagKey, StatsdConfig.ServiceEnvVar, config.ServiceName);
            AddTag(globalTags, StatsdConfig.EnvironmentTagKey, StatsdConfig.EnvironmentEnvVar, config.Environment);
            AddTag(globalTags, StatsdConfig.VersionTagKey, StatsdConfig.VersionEnvVar, config.ServiceVersion);

            return(globalTags.ToArray());
        }
예제 #7
0
        private string[] GetGlobalTags(StatsdConfig config)
        {
            var globalTags = new List <string>();

            if (config.ConstantTags != null)
            {
                globalTags.AddRange(config.ConstantTags);
            }

            string entityId = Environment.GetEnvironmentVariable(StatsdConfig.EntityIdEnvVar);

            if (!string.IsNullOrEmpty(entityId))
            {
                globalTags.Add($"{_entityIdInternalTagKey}:{entityId}");
            }

            return(globalTags.ToArray());
        }
예제 #8
0
        private TransportData CreateTransportData(DogStatsdEndPoint endPoint, StatsdConfig config)
        {
            var transportData = new TransportData();

            transportData.Transport = CreateTransport(endPoint, config);
            switch (transportData.Transport.TransportType)
            {
            case TransportType.UDP: transportData.BufferCapacity = config.StatsdMaxUDPPacketSize; break;

            case TransportType.UDS: transportData.BufferCapacity = config.StatsdMaxUnixDomainSocketPacketSize; break;

            // use StatsdMaxUDPPacketSize for named pipe
            case TransportType.NamedPipe: transportData.BufferCapacity = config.StatsdMaxUDPPacketSize; break;

            default: throw new NotSupportedException();
            }

            return(transportData);
        }
예제 #9
0
        /// <summary>
        /// Configures the instance.
        /// Must be called before any other methods.
        /// </summary>
        /// <param name="config">The value of the config.</param>
        public void Configure(StatsdConfig config)
        {
            if (_statsdBuilder == null)
            {
                throw new ObjectDisposedException(nameof(DogStatsdService));
            }

            if (config == null)
            {
                throw new ArgumentNullException("config");
            }

            if (_config != null)
            {
                throw new InvalidOperationException("Configuration for DogStatsdService already performed");
            }

            _config        = config;
            _statsdData    = _statsdBuilder.BuildStatsData(config);
            _metricsSender = _statsdData.MetricsSender;
        }
예제 #10
0
 /// <summary>
 /// Configures the instance.
 /// Must be called before any other methods.
 /// </summary>
 /// <param name="config">The value of the config.</param>
 public static void Configure(StatsdConfig config) => _dogStatsdService.Configure(config);