forked from ragnard/Graphite.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphiteTcpClient.cs
74 lines (60 loc) · 1.59 KB
/
GraphiteTcpClient.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using System;
using System.Net.Sockets;
using Graphite.Policy;
namespace Graphite
{
/// <summary>
/// Typed TCP client for logging into graphite. Have a look at
/// http://graphite.wikidot.com/getting-your-data-into-graphite
/// for details
/// </summary>
public class GraphiteTcpClient : IGraphiteClient, IDisposable
{
readonly ExceptionPolicy _policy;
readonly TcpClient _tcpClient;
public GraphiteTcpClient(string hostname, int port = 2003, string keyPrefix = null, ExceptionPolicy policy = null)
{
_policy = policy ?? TracingPolicy.Default;
Hostname = hostname;
Port = port;
KeyPrefix = keyPrefix;
_tcpClient = new TcpClient(Hostname, Port);
}
public string Hostname { get; private set; }
public int Port { get; private set; }
public string KeyPrefix { get; private set; }
#region IGraphiteClient Members
public void Send(string path, int value, DateTime timeStamp)
{
_policy.Do(() =>
{
#if NET35
if (!string.IsNullOrEmpty(KeyPrefix))
#else
if (!string.IsNullOrWhiteSpace(KeyPrefix))
#endif
{
path = KeyPrefix + "." + path;
}
byte[] message = new PlaintextMessage(path, value, timeStamp).ToByteArray();
_tcpClient.GetStream().Write(message, 0, message.Length);
});
}
#endregion
#region IDisposable
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!disposing) return;
if (_tcpClient != null)
{
_tcpClient.Close();
}
}
#endregion
}
}