예제 #1
0
        private static string TruncateMessageIfRequired(
            string name,
            StringBuilder builder,
            bool truncateIfTooLong,
            string processedMessage)
        {
            if (builder.Length > ServiceCheckMaxSize)
            {
                if (!truncateIfTooLong)
                {
                    throw new Exception(string.Format("ServiceCheck {0} payload is too big (more than 8kB)", name));
                }

                var overage = builder.Length - ServiceCheckMaxSize;

                if (processedMessage == null || overage > processedMessage.Length)
                {
                    throw new ArgumentException(string.Format("ServiceCheck name is too long to truncate, payload is too big (more than 8Kb) for {0}", name), "name");
                }

                return(SerializerHelper.TruncateOverage(processedMessage, overage));
            }

            return(null);
        }
예제 #2
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));
                }
            }
        }
예제 #3
0
        public SerializedMetric Serialize(
            string title,
            string text,
            string alertType,
            string aggregationKey,
            string sourceType,
            int?dateHappened,
            string priority,
            string hostname,
            string[] tags,
            bool truncateIfTooLong = false)
        {
            string processedTitle = SerializerHelper.EscapeContent(title);
            string processedText  = SerializerHelper.EscapeContent(text);

            var serializedMetric = _serializerHelper.GetOptionalSerializedMetric();

            if (serializedMetric == null)
            {
                return(null);
            }

            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 (dateHappened != null)
            {
                builder.AppendFormat(CultureInfo.InvariantCulture, "|d:{0}", dateHappened.Value);
            }

            SerializerHelper.AppendIfNotNull(builder, "|h:", hostname);
            SerializerHelper.AppendIfNotNull(builder, "|k:", aggregationKey);
            SerializerHelper.AppendIfNotNull(builder, "|p:", priority);
            SerializerHelper.AppendIfNotNull(builder, "|s:", sourceType);
            SerializerHelper.AppendIfNotNull(builder, "|t:", alertType);

            _serializerHelper.AppendTags(builder, tags);

            if (builder.Length > MaxSize)
            {
                if (truncateIfTooLong)
                {
                    var overage = builder.Length - MaxSize;
                    if (title.Length > text.Length)
                    {
                        title = SerializerHelper.TruncateOverage(title, overage);
                    }
                    else
                    {
                        text = SerializerHelper.TruncateOverage(text, overage);
                    }

                    return(Serialize(title, text, alertType, aggregationKey, sourceType, dateHappened, priority, hostname, tags, true));
                }
                else
                {
                    throw new Exception(string.Format("Event {0} payload is too big (more than 8kB)", title));
                }
            }

            return(serializedMetric);
        }