-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from ahdde/carbon_http
add support for go-carbon http receiver
- Loading branch information
Showing
5 changed files
with
167 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace ahd.Graphite | ||
{ | ||
/// <summary> | ||
/// Base class for clients for submitting data to carbon | ||
/// </summary> | ||
public abstract class AbstractCarbonClient | ||
{ | ||
/// <summary> | ||
/// Send a single datapoint | ||
/// </summary> | ||
/// <param name="series">metric path</param> | ||
/// <param name="value">metric value</param> | ||
/// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is <see cref="CancellationToken.None"/>.</param> | ||
/// <returns></returns> | ||
public Task SendAsync(string series, double value, CancellationToken cancellationToken = default(CancellationToken)) | ||
{ | ||
return SendAsync(series, value, DateTime.Now, cancellationToken); | ||
} | ||
|
||
/// <summary> | ||
/// Send a single datapoint | ||
/// </summary> | ||
/// <param name="series">metric path</param> | ||
/// <param name="value">metric value</param> | ||
/// <param name="timestamp">metric timestamp</param> | ||
/// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is <see cref="CancellationToken.None"/>.</param> | ||
/// <returns></returns> | ||
public Task SendAsync(string series, double value, DateTime timestamp, CancellationToken cancellationToken = default(CancellationToken)) | ||
{ | ||
return SendAsync(new []{new Datapoint(series, value, timestamp)}, cancellationToken); | ||
} | ||
|
||
/// <summary> | ||
/// Send a list of datapoints | ||
/// </summary> | ||
/// <param name="datapoints"></param> | ||
/// <returns></returns> | ||
public Task SendAsync(params Datapoint[] datapoints) | ||
{ | ||
return SendAsync(datapoints, CancellationToken.None); | ||
} | ||
|
||
/// <summary> | ||
/// Send a list of datapoints | ||
/// </summary> | ||
/// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is <see cref="CancellationToken.None"/>.</param> | ||
/// <param name="datapoints"></param> | ||
/// <returns></returns> | ||
public Task SendAsync(CancellationToken cancellationToken, params Datapoint[] datapoints) | ||
{ | ||
return SendAsync(datapoints, cancellationToken); | ||
} | ||
|
||
/// <summary> | ||
/// Send a list of datapoints | ||
/// </summary> | ||
/// <param name="datapoints"></param> | ||
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param> | ||
/// <returns></returns> | ||
public Task SendAsync(Datapoint[] datapoints, CancellationToken cancellationToken) | ||
{ | ||
ICollection<Datapoint> points = datapoints; | ||
return SendAsync(points, cancellationToken); | ||
} | ||
|
||
/// <summary> | ||
/// Send a list of datapoints | ||
/// </summary> | ||
/// <param name="datapoints"></param> | ||
/// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is <see cref="CancellationToken.None"/>.</param> | ||
/// <returns></returns> | ||
public abstract Task SendAsync(ICollection<Datapoint> datapoints, CancellationToken cancellationToken = default(CancellationToken)); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Net.Http; | ||
using System.Net.Http.Headers; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using ahd.Graphite.Exceptions; | ||
using Razorvine.Pickle; | ||
|
||
namespace ahd.Graphite | ||
{ | ||
/// <summary> | ||
/// Client for submitting data to carbon via HTTP using the pickle protocol | ||
/// </summary> | ||
public class CarbonHttpClient:AbstractCarbonClient | ||
{ | ||
private readonly HttpClient _client; | ||
|
||
/// <summary> | ||
/// Creates a client for localhost:2007 | ||
/// </summary> | ||
public CarbonHttpClient():this("http://localhost:2007") | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Creates a client with the specified endpoint | ||
/// </summary> | ||
/// <param name="baseAddress">carbon http endpoint</param> | ||
public CarbonHttpClient(string baseAddress):this(new Uri(baseAddress)) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Creates a client with the specified endpoint | ||
/// </summary> | ||
/// <param name="baseAddress">carbon http endpoint</param> | ||
public CarbonHttpClient(Uri baseAddress):this(new HttpClient{BaseAddress = baseAddress}) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Creates a client using the supplied http client | ||
/// </summary> | ||
/// <param name="client">preconfigured http client</param> | ||
public CarbonHttpClient(HttpClient client) | ||
{ | ||
_client = client; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override async Task SendAsync(ICollection<Datapoint> datapoints, CancellationToken cancellationToken = default(CancellationToken)) | ||
{ | ||
var response = await _client.PostAsync("/", Serialize(datapoints), cancellationToken).ConfigureAwait(false); | ||
await response.EnsureSuccessStatusCodeAsync().ConfigureAwait(false); | ||
} | ||
|
||
private HttpContent Serialize(ICollection<Datapoint> datapoints) | ||
{ | ||
using (var pickler = new Pickler()) | ||
{ | ||
var data = datapoints.Select(x => new object[] { x.Series, new object[] { x.UnixTimestamp, x.Value } }); | ||
var pickled = pickler.dumps(data); | ||
return new ByteArrayContent(pickled) | ||
{ | ||
Headers = | ||
{ | ||
ContentType = new MediaTypeHeaderValue("application/python-pickle") | ||
} | ||
}; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters