Ejemplo n.º 1
0
        public void SerializeTo(ref StatsMetric metricStats, SerializedMetric serializedMetric)
        {
            serializedMetric.Reset();

            var builder = serializedMetric.Builder;
            var unit    = _commandToUnit[metricStats.MetricType];

            builder.Append(_prefix);
            builder.Append(metricStats.StatName);
            builder.Append(':');
            switch (metricStats.MetricType)
            {
            case MetricType.Set: builder.Append(metricStats.StringValue); break;

            default: builder.AppendFormat(CultureInfo.InvariantCulture, "{0}", metricStats.NumericValue); break;
            }

            builder.Append('|');
            builder.Append(unit);

            if (metricStats.SampleRate != 1.0)
            {
                builder.AppendFormat(CultureInfo.InvariantCulture, "|@{0}", metricStats.SampleRate);
            }

            _serializerHelper.AppendTags(builder, metricStats.Tags);
        }
Ejemplo n.º 2
0
        public void SerializeTo(ref StatsServiceCheck sc, SerializedMetric serializedMetric)
        {
            serializedMetric.Reset();

            var builder = serializedMetric.Builder;

            string processedName    = EscapeName(sc.Name);
            string processedMessage = EscapeMessage(sc.ServiceCheckMessage);

            builder.Append("_sc|");
            builder.Append(processedName);
            builder.AppendFormat(CultureInfo.InvariantCulture, "|{0}", sc.Status);

            if (sc.Timestamp != null)
            {
                builder.AppendFormat(CultureInfo.InvariantCulture, "|d:{0}", sc.Timestamp.Value);
            }

            SerializerHelper.AppendIfNotNull(builder, "|h:", sc.Hostname);

            _serializerHelper.AppendTags(builder, sc.Tags);

            // Note: this must always be appended to the result last.
            SerializerHelper.AppendIfNotNull(builder, "|m:", processedMessage);

            sc.ServiceCheckMessage = TruncateMessageIfRequired(sc.Name, builder, sc.TruncateIfTooLong, processedMessage);
            if (sc.ServiceCheckMessage != null)
            {
                sc.TruncateIfTooLong = true;
                SerializeTo(ref sc, serializedMetric);
            }
        }
Ejemplo n.º 3
0
 private void Send(SerializedMetric optionalSerializedMetric, Action onSuccess)
 {
     if (optionalSerializedMetric != null && _statsBufferize.Send(optionalSerializedMetric))
     {
         onSuccess();
     }
     else
     {
         _optionalTelemetry?.OnPacketsDroppedQueue();
     }
 }
Ejemplo n.º 4
0
        public void SerializeTo(ref StatsEvent statsEvent, SerializedMetric serializedMetric)
        {
            serializedMetric.Reset();
            string processedTitle = SerializerHelper.EscapeContent(statsEvent.Title);
            string processedText  = SerializerHelper.EscapeContent(statsEvent.Text);
            var    builder        = serializedMetric.Builder;

            builder.Append("_e{");
            builder.AppendFormat(CultureInfo.InvariantCulture, "{0}", processedTitle.Length);
            builder.Append(',');
            builder.AppendFormat(CultureInfo.InvariantCulture, "{0}", processedText.Length);
            builder.Append("}:");
            builder.Append(processedTitle);
            builder.Append('|');
            builder.Append(processedText);

            if (statsEvent.DateHappened != null)
            {
                builder.AppendFormat(CultureInfo.InvariantCulture, "|d:{0}", statsEvent.DateHappened.Value);
            }

            SerializerHelper.AppendIfNotNull(builder, "|h:", statsEvent.Hostname);
            SerializerHelper.AppendIfNotNull(builder, "|k:", statsEvent.AggregationKey);
            SerializerHelper.AppendIfNotNull(builder, "|p:", statsEvent.Priority);
            SerializerHelper.AppendIfNotNull(builder, "|s:", statsEvent.SourceType);
            SerializerHelper.AppendIfNotNull(builder, "|t:", statsEvent.AlertType);

            _serializerHelper.AppendTags(builder, statsEvent.Tags);

            if (builder.Length > MaxSize)
            {
                if (statsEvent.TruncateIfTooLong)
                {
                    var overage = builder.Length - MaxSize;
                    if (statsEvent.Title.Length > statsEvent.Text.Length)
                    {
                        statsEvent.Title = SerializerHelper.TruncateOverage(statsEvent.Title, overage);
                    }
                    else
                    {
                        statsEvent.Text = SerializerHelper.TruncateOverage(statsEvent.Text, overage);
                    }

                    statsEvent.TruncateIfTooLong = true;
                    SerializeTo(ref statsEvent, serializedMetric);
                }
                else
                {
                    throw new Exception(string.Format("Event {0} payload is too big (more than 8kB)", statsEvent.Title));
                }
            }
        }
Ejemplo n.º 5
0
 private void SendMetric(string metricName, int value)
 {
     if (_optionalTransport != null && _optionalMetricSerializer != null)
     {
         var serializedMetric = new SerializedMetric();
         var metricStats      = new StatsMetric
         {
             MetricType   = MetricType.Count,
             StatName     = metricName,
             NumericValue = value,
             SampleRate   = 1.0,
             Tags         = _optionalTags,
         };
         _optionalMetricSerializer.SerializeTo(ref metricStats, serializedMetric);
         var bytes = BufferBuilder.GetBytes(serializedMetric.ToString());
         _optionalTransport.Send(bytes, bytes.Length);
     }
 }