BloombergFLP.CollectdWin.MetricValue.getJSON C# (CSharp) Method

getJSON() public method

public getJSON ( ) : string
return string
        public override string getJSON()
        {
            IList<DataSource> dsList = DataSetCollection.Instance.GetDataSource(TypeName);
            var dsNames = new List<string>();
            var dsTypes = new List<string>();
            if (dsList == null)
            {
                Logger.Debug("Invalid type : {0}, not found in types.db", TypeName);
            }
            else
            {
                foreach (DataSource ds in dsList)
                {
                    dsNames.Add(ds.Name);
                    dsTypes.Add(ds.Type.ToString().ToLower());
                }
            }
            string dsTypesStr = string.Join(",", dsTypes.ConvertAll(m => string.Format("\"{0}\"", m)).ToArray());
            string dsNamesStr = string.Join(",", dsNames.ConvertAll(m => string.Format("\"{0}\"", m)).ToArray());
            string valStr = string.Join(",", Array.ConvertAll(Values, val => val.ToString(CultureInfo.InvariantCulture)));

            string res = string.Format(MetricJsonFormat, HostName, PluginName,
                PluginInstanceName, TypeName, TypeInstanceName, Epoch,
                Interval, dsTypesStr, dsNamesStr, valStr);
            return (res);
        }

Usage Example

        public void Write(CollectableValue value)
        {
            // This Write Plugin only knows about metrics
            if (!(value is MetricValue))
            {
                return;
            }

            if (value == null)
            {
                Logger.Debug("write() - Invalid null metric");
                return;
            }
            MetricValue metric = (MetricValue)value;

            if (!_connected)
            {
                StartConnection();
            }
            if (_connected && _channel != null)
            {
                string routingKey = GetAmqpRoutingKey(metric);
                string message    = metric.getJSON();
                try
                {
                    _channel.BasicPublish(_exchange, routingKey, null, Encoding.UTF8.GetBytes(message));
                }
                catch
                {
                    CloseConnection();
                }
            }
        }