Ejemplo n.º 1
0
        IList <MetricsData> GetMetricsDataList(string prometheusMessage)
        {
            var metricsDataList = new List <MetricsData>();

            using (StringReader sr = new StringReader(prometheusMessage))
            {
                string line;
                while ((line = sr.ReadLine()) != null)
                {
                    if (line.Trim().StartsWith('#'))
                    {
                        continue;
                    }

                    Match match = PrometheusSchemaRegex.Match(line.Trim());
                    if (match.Success)
                    {
                        var metricName  = string.Empty;
                        var metricValue = string.Empty;
                        var tagNames    = new List <string>();
                        var tagValues   = new List <string>();

                        var name = match.Groups["metricname"];
                        if (name?.Length > 0)
                        {
                            metricName = name.Value;
                        }

                        var value = match.Groups["metricvalue"];
                        if (value?.Length > 0)
                        {
                            metricValue = value.Value;
                        }

                        var tagnames = match.Groups["tagname"];
                        if (tagnames.Length > 0)
                        {
                            for (int i = 0; i < tagnames.Captures.Count; i++)
                            {
                                tagNames.Add(tagnames.Captures[i].Value);
                            }
                        }

                        var tagvalues = match.Groups["tagvalue"];
                        if (tagvalues.Length > 0)
                        {
                            for (int i = 0; i < tagvalues.Captures.Count; i++)
                            {
                                tagValues.Add(tagvalues.Captures[i].Value);
                            }
                        }

                        var tags = tagNames.Zip(tagValues, (k, v) => new { k, v })
                                   .ToDictionary(x => x.k, x => x.v);
                        var metricsData = new MetricsData(
                            "prometheus",
                            metricName,
                            metricValue,
                            JsonConvert.SerializeObject(tags));

                        metricsDataList.Add(metricsData);
                    }
                }
            }
            return(metricsDataList);
        }
Ejemplo n.º 2
0
        IList <Message> BuildMessagesInJsonFormat2(string prometheusMessage)
        {
            var messages = new List <Message>();

            using (StringReader sr = new StringReader(prometheusMessage))
            {
                string line;
                while ((line = sr.ReadLine()) != null)
                {
                    if (line.Trim().StartsWith('#'))
                    {
                        continue;
                    }

                    Match match = PrometheusSchemaRegex.Match(line.Trim());
                    if (match.Success)
                    {
                        var metricName  = string.Empty;
                        var metricValue = string.Empty;
                        var tagNames    = new List <string>();
                        var tagValues   = new List <string>();

                        var name = match.Groups["metricname"];
                        if (name?.Length > 0)
                        {
                            metricName = name.Value;
                        }

                        var value = match.Groups["metricvalue"];
                        if (value?.Length > 0)
                        {
                            metricValue = value.Value;
                        }

                        var tagnames = match.Groups["tagname"];
                        if (tagnames.Length > 0)
                        {
                            for (int i = 0; i < tagnames.Captures.Count; i++)
                            {
                                tagNames.Add(tagnames.Captures[i].Value);
                            }
                        }

                        var tagvalues = match.Groups["tagvalue"];
                        if (tagvalues.Length > 0)
                        {
                            for (int i = 0; i < tagvalues.Captures.Count; i++)
                            {
                                tagValues.Add(tagvalues.Captures[i].Value);
                            }
                        }

                        var tags = tagNames.Zip(tagValues, (k, v) => new { k, v })
                                   .ToDictionary(x => x.k, x => x.v);
                        var metricsData = new MetricsData(
                            "prometheus",
                            metricName,
                            metricValue,
                            JsonConvert.SerializeObject(tags));

                        var messageBytes = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(metricsData));
                        var message      = new Message(messageBytes);
                        message.Properties[IdentifierPropertyName] = this.identifier;
                        message.MessageSchema   = this.format.ToString();
                        message.ContentEncoding = "UTF-8";
                        message.ContentType     = "application/json";
                        messages.Add(message);
                    }
                }
            }

            return(messages);
        }