/// <summary> /// An IMetric extension method that converts a given metric value into a string representation based on the given MetricFormat. /// </summary> /// <exception cref="ArgumentNullException"> Thrown when the metric is null. </exception> /// <exception cref="ArgumentException"> Thrown when the key is invalid. </exception> /// <param name="metric"> The metric to act on. </param> /// <param name="key"> The optional key to prefix metrics with. </param> /// <param name="format"> Describes the format to use. </param> /// <returns> A string representation of this object. </returns> public static string ToString(this IMetric metric, string key, MetricFormat format) { if (null == metric) { throw new ArgumentNullException("metric"); } if (!key.IsValidKey()) { throw new ArgumentException("contains invalid characters", "key"); } string converted = null; switch (format) { case MetricFormat.StatSite: converted = metric.ToStatSiteString(); break; case MetricFormat.StatsD: default: converted = metric.ToStatsDString(); break; } return(string.IsNullOrEmpty(key) ? converted : string.Format(CultureInfo.InvariantCulture, "{0}.{1}", key, converted)); }
/// <summary> Times a given Action and reports it as a Timing metrics to the server. </summary> /// <remarks> Exceptions generated by the Action are not handled. </remarks> /// <exception cref="ArgumentException"> Thrown when the hostNameOrAddress is null or empty OR the key contains invalid characters. </exception> /// <exception cref="ArgumentNullException"> Thrown when the action is null. </exception> /// <param name="hostNameOrAddress"> The DNS hostName or IPv4 or IPv6 address of the server. </param> /// <param name="port"> The port. </param> /// <param name="format"> Describes the metric format to use. </param> /// <param name="key"> The optional key to prefix metrics with. </param> /// <param name="action"> The action. </param> public static void Time(string hostNameOrAddress, int port, MetricFormat format, string key, Action action) { if (string.IsNullOrEmpty(hostNameOrAddress)) { throw new ArgumentException("cannot be null or empty", "hostNameOrAddress"); } if (!key.IsValidKey()) { throw new ArgumentException("contains invalid characters", "key"); } if (null == action) { throw new ArgumentNullException("action"); } Stopwatch timer = null; try { timer = new Stopwatch(); timer.Start(); action(); } finally { if (null != timer) { timer.Stop(); SendToServer(hostNameOrAddress, port, new[] { Metric.Timing(key, timer.Elapsed.TotalSeconds).ToString(null, format) }, false); } } }
/// <summary> Initializes a new instance of the MetricClient class. </summary> /// <exception cref="ArgumentException"> Thrown when the hostNameOrAddress is null or empty OR the key contains invalid characters. </exception> /// <param name="hostNameOrAddress"> The DNS hostName or IPv4 or IPv6 address of the server. </param> /// <param name="port"> The port. </param> /// <param name="format"> Describes the metric format to use. </param> /// <param name="key"> The optional key to prefix metrics with. </param> public MetricClient(string hostNameOrAddress, int port, MetricFormat format, string key) { if (string.IsNullOrEmpty(hostNameOrAddress)) { throw new ArgumentException("cannot be null or empty", "hostNameOrAddress"); } if (!key.IsValidKey()) { throw new ArgumentException("contains invalid characters", "key"); } _messenger = new UdpMessenger(hostNameOrAddress, port); _key = key; _format = format; }
/// <summary> Lazy converts a list of metrics to strings based on the given MetricFormat. </summary> /// <exception cref="ArgumentNullException"> Thrown when the metrics are null. </exception> /// <exception cref="ArgumentException"> Thrown when the key is invalid. </exception> /// <param name="metrics"> The metrics to act on. </param> /// <param name="key"> The optional key to prefix metrics with. </param> /// <param name="format"> Describes the format to use. </param> /// <returns> An enumerator that allows foreach to be used to process the strings in this collection. </returns> public static IEnumerable<string> ToStrings(this IEnumerable<IMetric> metrics, string key, MetricFormat format) { if (null == metrics) { throw new ArgumentNullException("metrics"); } if (!key.IsValidKey()) { throw new ArgumentException("contains invalid characters", "key"); } foreach (var metric in metrics) { yield return metric.ToString(key, format); } }
/// <summary> Initializes a new instance of the MetricClient class. </summary> /// <exception cref="ArgumentNullException"> Thrown when the configuration object is null. </exception> /// <param name="configuration"> The configuration. </param> public MetricClient(IMetricPublishingConfiguration configuration) { if (null == configuration) { throw new ArgumentNullException("configuration"); } if (string.IsNullOrEmpty(configuration.HostNameOrAddress)) { throw new ArgumentException("HostNameOrAddress cannot be null or empty", "configuration"); } if (!configuration.PrefixKey.IsValidKey()) { throw new ArgumentException("PrefixKey contains invalid characters", "configuration"); } _messenger = new UdpMessenger(configuration.HostNameOrAddress, configuration.Port); _key = configuration.PrefixKey; _format = configuration.Format; }
/// <summary> /// Will stream the given metrics in the specified format, breaking up the metrics into 10 packets at a time, where multiple metrics may /// comprise a single packet. This call is appropriate for infinite IEnumerables. /// </summary> /// <exception cref="ArgumentException"> Thrown when the hostNameOrAddress is null or empty OR the key contains invalid characters. </exception> /// <exception cref="ArgumentNullException"> Thrown when the metrics are null. </exception> /// <param name="hostNameOrAddress"> The DNS hostName or IPv4 or IPv6 address of the server. </param> /// <param name="port"> The port. </param> /// <param name="format"> Describes the metric format to use. </param> /// <param name="key"> The optional key to prefix metrics with. </param> /// <param name="metrics"> The metrics. </param> public static void Stream(string hostNameOrAddress, int port, MetricFormat format, string key, IEnumerable <IMetric> metrics) { if (string.IsNullOrEmpty(hostNameOrAddress)) { throw new ArgumentException("cannot be null or empty", "hostNameOrAddress"); } if (!key.IsValidKey()) { throw new ArgumentException("contains invalid characters", "key"); } if (null == metrics) { throw new ArgumentNullException("metrics"); } SendToServer(hostNameOrAddress, port, metrics.ToStrings(key, format), true); }
/// <summary> /// An IMetric extension method that converts a given metric value into a string representation based on the given MetricFormat. /// </summary> /// <exception cref="ArgumentNullException"> Thrown when the metric is null. </exception> /// <exception cref="ArgumentException"> Thrown when the key is invalid. </exception> /// <param name="metric"> The metric to act on. </param> /// <param name="key"> The optional key to prefix metrics with. </param> /// <param name="format"> Describes the format to use. </param> /// <returns> A string representation of this object. </returns> public static string ToString(this IMetric metric, string key, MetricFormat format) { if (null == metric) { throw new ArgumentNullException("metric"); } if (!key.IsValidKey()) { throw new ArgumentException("contains invalid characters", "key"); } string converted = null; switch (format) { case MetricFormat.StatSite: converted = metric.ToStatSiteString(); break; case MetricFormat.StatsD: default: converted = metric.ToStatsDString(); break; } return string.IsNullOrEmpty(key) ? converted : string.Format(CultureInfo.InvariantCulture, "{0}.{1}", key, converted); }
/// <summary> Lazy converts a list of metrics to strings based on the given MetricFormat. </summary> /// <exception cref="ArgumentNullException"> Thrown when the metrics are null. </exception> /// <exception cref="ArgumentException"> Thrown when the key is invalid. </exception> /// <param name="metrics"> The metrics to act on. </param> /// <param name="key"> The optional key to prefix metrics with. </param> /// <param name="format"> Describes the format to use. </param> /// <returns> An enumerator that allows foreach to be used to process the strings in this collection. </returns> public static IEnumerable <string> ToStrings(this IEnumerable <IMetric> metrics, string key, MetricFormat format) { if (null == metrics) { throw new ArgumentNullException("metrics"); } if (!key.IsValidKey()) { throw new ArgumentException("contains invalid characters", "key"); } foreach (var metric in metrics) { yield return(metric.ToString(key, format)); } }
/// <summary> /// Will stream the given metrics in the specified format, breaking up the metrics into 10 packets at a time, where multiple metrics may /// comprise a single packet. This call is appropriate for infinite IEnumerables. /// </summary> /// <exception cref="ArgumentException"> Thrown when the hostNameOrAddress is null or empty OR the key contains invalid characters. </exception> /// <exception cref="ArgumentNullException"> Thrown when the metrics are null. </exception> /// <param name="hostNameOrAddress"> The DNS hostName or IPv4 or IPv6 address of the server. </param> /// <param name="port"> The port. </param> /// <param name="format"> Describes the metric format to use. </param> /// <param name="key"> The optional key to prefix metrics with. </param> /// <param name="metrics"> The metrics. </param> public static void Stream(string hostNameOrAddress, int port, MetricFormat format, string key, IEnumerable<IMetric> metrics) { if (string.IsNullOrEmpty(hostNameOrAddress)) { throw new ArgumentException("cannot be null or empty", "hostNameOrAddress"); } if (!key.IsValidKey()) { throw new ArgumentException("contains invalid characters", "key"); } if (null == metrics) { throw new ArgumentNullException("metrics"); } SendToServer(hostNameOrAddress, port, metrics.ToStrings(key, format), true); }