Ejemplo n.º 1
0
        public SerializedMetric Serialize <T>(
            MetricType metricType,
            string name,
            T value,
            double sampleRate = 1.0,
            string[] tags     = null)
        {
            var serializedMetric = _serializerHelper.GetOptionalSerializedMetric();

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

            var builder = serializedMetric.Builder;
            var unit    = _commandToUnit[metricType];

            builder.Append(_prefix);
            builder.Append(name);
            builder.Append(':');
            builder.AppendFormat(CultureInfo.InvariantCulture, "{0}", value);
            builder.Append('|');
            builder.Append(unit);

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

            _serializerHelper.AppendTags(builder, tags);
            return(serializedMetric);
        }
Ejemplo n.º 2
0
        public SerializedMetric Serialize(
            string name,
            int status,
            int?timestamp,
            string hostname,
            string[] tags,
            string serviceCheckMessage,
            bool truncateIfTooLong = false)
        {
            var serializedMetric = _serializerHelper.GetOptionalSerializedMetric();

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

            var builder = serializedMetric.Builder;

            string processedName    = EscapeName(name);
            string processedMessage = EscapeMessage(serviceCheckMessage);

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

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

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

            _serializerHelper.AppendTags(builder, tags);

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

            var truncatedMessage = TruncateMessageIfRequired(name, builder, truncateIfTooLong, processedMessage);

            if (truncatedMessage != null)
            {
                return(Serialize(name, status, timestamp, hostname, tags, truncatedMessage, true));
            }

            return(serializedMetric);
        }
Ejemplo n.º 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);
        }