Skip to content

Commit

Permalink
Merge pull request #14 from ahdde/cancellationtoken
Browse files Browse the repository at this point in the history
add missing cancellationtoken
  • Loading branch information
MirkoFl committed Jan 31, 2018
2 parents 7eb87bc + f7caec3 commit ee4f421
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 17 deletions.
4 changes: 2 additions & 2 deletions Graphite/Base/SeriesListBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -964,7 +964,7 @@ public SeriesListFunction WeightedAverage(SeriesListBase seriesListWeight, param

/// <summary>
/// allows the metric paths to contain named variables.
/// Variable values can be specified or overriden in <see cref="GraphiteClient.GetMetricsDataAsync(ahd.Graphite.Base.SeriesListBase,string,string,System.Collections.Generic.IDictionary{string,string},System.Nullable{ulong})"/>
/// Variable values can be specified or overriden in <see cref="GraphiteClient.GetMetricsDataAsync(ahd.Graphite.Base.SeriesListBase,string,string,System.Collections.Generic.IDictionary{string,string},System.Nullable{ulong},System.Threading.CancellationToken)"/>
/// </summary>
/// <param name="defaultValues">default values for the template variables</param>
/// <returns></returns>
Expand All @@ -975,7 +975,7 @@ public SeriesListFunction Template(params Tuple<string, string>[] defaultValues)

/// <summary>
/// allows the metric paths to contain positional variables.
/// Variable values can be specified or overriden in <see cref="GraphiteClient.GetMetricsDataAsync(ahd.Graphite.Base.SeriesListBase,string,string,System.Collections.Generic.IDictionary{string,string},System.Nullable{ulong})"/>
/// Variable values can be specified or overriden in <see cref="GraphiteClient.GetMetricsDataAsync(ahd.Graphite.Base.SeriesListBase,string,string,System.Collections.Generic.IDictionary{string,string},System.Nullable{ulong},System.Threading.CancellationToken)"/>
/// </summary>
/// <param name="defaultValues">default values for the template variables</param>
/// <returns></returns>
Expand Down
46 changes: 31 additions & 15 deletions Graphite/GraphiteClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -251,14 +251,15 @@ private void SendInternal(ICollection<Datapoint> datapoints)
/// <summary>
/// Walks the metrics tree and returns every metric found as a sorted JSON array
/// </summary>
/// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is <see cref="CancellationToken.None"/>.</param>
/// <returns>list of all metrics</returns>
public async Task<string[]> GetAllMetricsAsync()
public async Task<string[]> GetAllMetricsAsync(CancellationToken cancellationToken = default(CancellationToken))
{
using (var client = new HttpClient {BaseAddress = GetHttpApiUri()})
{
var response = await client.GetAsync("/metrics/index.json").ConfigureAwait(false);
var response = await client.GetAsync("/metrics/index.json", cancellationToken).ConfigureAwait(false);
await response.EnsureSuccessStatusCodeAsync().ConfigureAwait(false);
return await response.Content.ReadAsAsync<string[]>().ConfigureAwait(false);
return await response.Content.ReadAsAsync<string[]>(cancellationToken).ConfigureAwait(false);
}
}

Expand All @@ -269,8 +270,9 @@ public async Task<string[]> GetAllMetricsAsync()
/// <param name="wildcards">Whether to add a wildcard result at the end or not</param>
/// <param name="from">timestamp from which to consider metrics</param>
/// <param name="until">timestamp until which to consider metrics</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is <see cref="CancellationToken.None"/>.</param>
/// <returns></returns>
public async Task<GraphiteMetric[]> FindMetricsAsync(string query, bool wildcards = false, DateTime? from = null, DateTime? until = null)
public async Task<GraphiteMetric[]> FindMetricsAsync(string query, bool wildcards = false, DateTime? from = null, DateTime? until = null, CancellationToken cancellationToken = default(CancellationToken))
{
if (String.IsNullOrEmpty(query)) throw new ArgumentNullException(nameof(query));

Expand All @@ -287,9 +289,9 @@ public async Task<GraphiteMetric[]> FindMetricsAsync(string query, bool wildcard
var uri = String.Format("/metrics/find?query={0}&format=completer&wildcards={1}&from={2}&until={3}",
query, wildcards ? 1 : 0, fromUnix, untilUnix);

var response = await client.GetAsync(uri).ConfigureAwait(false);
var response = await client.GetAsync(uri, cancellationToken).ConfigureAwait(false);
await response.EnsureSuccessStatusCodeAsync().ConfigureAwait(false);
return (await response.Content.ReadAsAsync<GraphiteFindResult>().ConfigureAwait(false)).Metrics;
return (await response.Content.ReadAsAsync<GraphiteFindResult>(cancellationToken).ConfigureAwait(false)).Metrics;
}
}

Expand All @@ -300,16 +302,28 @@ public async Task<GraphiteMetric[]> FindMetricsAsync(string query, bool wildcard
/// <returns>list of matching metric names</returns>
public Task<string[]> ExpandMetricsAsync(params GraphitePath[] query)
{
return ExpandMetricsAsync(false, query);
return ExpandMetricsAsync(false, CancellationToken.None, query);
}

/// <summary>
/// Expands the given query with matching paths
/// </summary>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <param name="query">The metrics query</param>
/// <returns>list of matching metric names</returns>
public Task<string[]> ExpandMetricsAsync(CancellationToken cancellationToken, params GraphitePath[] query)
{
return ExpandMetricsAsync(false, cancellationToken, query);
}

/// <summary>
/// Expands the given query with matching paths
/// </summary>
/// <param name="leavesOnly">Whether to only return leaves or both branches and leaves</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
/// <param name="query">The metrics query</param>
/// <returns>list of matching metric names</returns>
public async Task<string[]> ExpandMetricsAsync(bool leavesOnly, params GraphitePath[] query)
public async Task<string[]> ExpandMetricsAsync(bool leavesOnly, CancellationToken cancellationToken, params GraphitePath[] query)
{
if (query == null || query.Length == 0) throw new ArgumentNullException(nameof(query));

Expand All @@ -325,9 +339,9 @@ public async Task<string[]> ExpandMetricsAsync(bool leavesOnly, params GraphiteP
}

var body = new FormUrlEncodedContent(values);
var response = await client.PostAsync("/metrics/expand", body).ConfigureAwait(false);
var response = await client.PostAsync("/metrics/expand", body, cancellationToken).ConfigureAwait(false);
await response.EnsureSuccessStatusCodeAsync().ConfigureAwait(false);
return (await response.Content.ReadAsAsync<GraphiteExpandResult>().ConfigureAwait(false)).Results;
return (await response.Content.ReadAsAsync<GraphiteExpandResult>(cancellationToken).ConfigureAwait(false)).Results;
}
}

Expand All @@ -347,10 +361,11 @@ private Uri GetHttpApiUri()
/// <param name="until">specify the relative or absolute end to graph</param>
/// <param name="template">The target metrics can use a special <see cref="SeriesListBase.Template(string[])"/> function which allows the metric paths to contain variables</param>
/// <param name="maxDataPoints"></param>
/// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is <see cref="CancellationToken.None"/>.</param>
/// <returns></returns>
public Task<GraphiteMetricData[]> GetMetricsDataAsync(SeriesListBase target, string from = null, string until = null, IDictionary<string, string> template = null, ulong? maxDataPoints = null)
public Task<GraphiteMetricData[]> GetMetricsDataAsync(SeriesListBase target, string from = null, string until = null, IDictionary<string, string> template = null, ulong? maxDataPoints = null, CancellationToken cancellationToken = default(CancellationToken))
{
return GetMetricsDataAsync(new[] {target}, from, until, template, maxDataPoints);
return GetMetricsDataAsync(new[] {target}, from, until, template, maxDataPoints, cancellationToken);
}

/// <summary>
Expand All @@ -361,8 +376,9 @@ public Task<GraphiteMetricData[]> GetMetricsDataAsync(SeriesListBase target, str
/// <param name="until">specify the relative or absolute end to graph</param>
/// <param name="template">The target metrics can use a special <see cref="SeriesListBase.Template(string[])"/> function which allows the metric paths to contain variables</param>
/// <param name="maxDataPoints"></param>
/// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is <see cref="CancellationToken.None"/>.</param>
/// <returns></returns>
public async Task<GraphiteMetricData[]> GetMetricsDataAsync(SeriesListBase[] targets, string from = null, string until = null, IDictionary<string,string> template = null, ulong? maxDataPoints = null)
public async Task<GraphiteMetricData[]> GetMetricsDataAsync(SeriesListBase[] targets, string from = null, string until = null, IDictionary<string,string> template = null, ulong? maxDataPoints = null, CancellationToken cancellationToken = default(CancellationToken))
{
if (targets == null || targets.Length == 0) throw new ArgumentNullException(nameof(targets));

Expand Down Expand Up @@ -408,10 +424,10 @@ public async Task<GraphiteMetricData[]> GetMetricsDataAsync(SeriesListBase[] tar
}
var body = new StringContent(sb.ToString(), Encoding.UTF8, "application/x-www-form-urlencoded");

var response = await client.PostAsync("/render",body).ConfigureAwait(false);
var response = await client.PostAsync("/render",body, cancellationToken).ConfigureAwait(false);
await response.EnsureSuccessStatusCodeAsync().ConfigureAwait(false);

return await response.Content.ReadAsAsync<GraphiteMetricData[]>().ConfigureAwait(false);
return await response.Content.ReadAsAsync<GraphiteMetricData[]>(cancellationToken).ConfigureAwait(false);
}
}
}
Expand Down

0 comments on commit ee4f421

Please sign in to comment.