Skip to content

Commit ee4f421

Browse files
authored
Merge pull request #14 from ahdde/cancellationtoken
add missing cancellationtoken
2 parents 7eb87bc + f7caec3 commit ee4f421

File tree

2 files changed

+33
-17
lines changed

2 files changed

+33
-17
lines changed

Graphite/Base/SeriesListBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -964,7 +964,7 @@ public SeriesListFunction WeightedAverage(SeriesListBase seriesListWeight, param
964964

965965
/// <summary>
966966
/// allows the metric paths to contain named variables.
967-
/// 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})"/>
967+
/// 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)"/>
968968
/// </summary>
969969
/// <param name="defaultValues">default values for the template variables</param>
970970
/// <returns></returns>
@@ -975,7 +975,7 @@ public SeriesListFunction Template(params Tuple<string, string>[] defaultValues)
975975

976976
/// <summary>
977977
/// allows the metric paths to contain positional variables.
978-
/// 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})"/>
978+
/// 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)"/>
979979
/// </summary>
980980
/// <param name="defaultValues">default values for the template variables</param>
981981
/// <returns></returns>

Graphite/GraphiteClient.cs

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -251,14 +251,15 @@ private void SendInternal(ICollection<Datapoint> datapoints)
251251
/// <summary>
252252
/// Walks the metrics tree and returns every metric found as a sorted JSON array
253253
/// </summary>
254+
/// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is <see cref="CancellationToken.None"/>.</param>
254255
/// <returns>list of all metrics</returns>
255-
public async Task<string[]> GetAllMetricsAsync()
256+
public async Task<string[]> GetAllMetricsAsync(CancellationToken cancellationToken = default(CancellationToken))
256257
{
257258
using (var client = new HttpClient {BaseAddress = GetHttpApiUri()})
258259
{
259-
var response = await client.GetAsync("/metrics/index.json").ConfigureAwait(false);
260+
var response = await client.GetAsync("/metrics/index.json", cancellationToken).ConfigureAwait(false);
260261
await response.EnsureSuccessStatusCodeAsync().ConfigureAwait(false);
261-
return await response.Content.ReadAsAsync<string[]>().ConfigureAwait(false);
262+
return await response.Content.ReadAsAsync<string[]>(cancellationToken).ConfigureAwait(false);
262263
}
263264
}
264265

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

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

290-
var response = await client.GetAsync(uri).ConfigureAwait(false);
292+
var response = await client.GetAsync(uri, cancellationToken).ConfigureAwait(false);
291293
await response.EnsureSuccessStatusCodeAsync().ConfigureAwait(false);
292-
return (await response.Content.ReadAsAsync<GraphiteFindResult>().ConfigureAwait(false)).Metrics;
294+
return (await response.Content.ReadAsAsync<GraphiteFindResult>(cancellationToken).ConfigureAwait(false)).Metrics;
293295
}
294296
}
295297

@@ -300,16 +302,28 @@ public async Task<GraphiteMetric[]> FindMetricsAsync(string query, bool wildcard
300302
/// <returns>list of matching metric names</returns>
301303
public Task<string[]> ExpandMetricsAsync(params GraphitePath[] query)
302304
{
303-
return ExpandMetricsAsync(false, query);
305+
return ExpandMetricsAsync(false, CancellationToken.None, query);
306+
}
307+
308+
/// <summary>
309+
/// Expands the given query with matching paths
310+
/// </summary>
311+
/// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
312+
/// <param name="query">The metrics query</param>
313+
/// <returns>list of matching metric names</returns>
314+
public Task<string[]> ExpandMetricsAsync(CancellationToken cancellationToken, params GraphitePath[] query)
315+
{
316+
return ExpandMetricsAsync(false, cancellationToken, query);
304317
}
305318

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

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

327341
var body = new FormUrlEncodedContent(values);
328-
var response = await client.PostAsync("/metrics/expand", body).ConfigureAwait(false);
342+
var response = await client.PostAsync("/metrics/expand", body, cancellationToken).ConfigureAwait(false);
329343
await response.EnsureSuccessStatusCodeAsync().ConfigureAwait(false);
330-
return (await response.Content.ReadAsAsync<GraphiteExpandResult>().ConfigureAwait(false)).Results;
344+
return (await response.Content.ReadAsAsync<GraphiteExpandResult>(cancellationToken).ConfigureAwait(false)).Results;
331345
}
332346
}
333347

@@ -347,10 +361,11 @@ private Uri GetHttpApiUri()
347361
/// <param name="until">specify the relative or absolute end to graph</param>
348362
/// <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>
349363
/// <param name="maxDataPoints"></param>
364+
/// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is <see cref="CancellationToken.None"/>.</param>
350365
/// <returns></returns>
351-
public Task<GraphiteMetricData[]> GetMetricsDataAsync(SeriesListBase target, string from = null, string until = null, IDictionary<string, string> template = null, ulong? maxDataPoints = null)
366+
public Task<GraphiteMetricData[]> GetMetricsDataAsync(SeriesListBase target, string from = null, string until = null, IDictionary<string, string> template = null, ulong? maxDataPoints = null, CancellationToken cancellationToken = default(CancellationToken))
352367
{
353-
return GetMetricsDataAsync(new[] {target}, from, until, template, maxDataPoints);
368+
return GetMetricsDataAsync(new[] {target}, from, until, template, maxDataPoints, cancellationToken);
354369
}
355370

356371
/// <summary>
@@ -361,8 +376,9 @@ public Task<GraphiteMetricData[]> GetMetricsDataAsync(SeriesListBase target, str
361376
/// <param name="until">specify the relative or absolute end to graph</param>
362377
/// <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>
363378
/// <param name="maxDataPoints"></param>
379+
/// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is <see cref="CancellationToken.None"/>.</param>
364380
/// <returns></returns>
365-
public async Task<GraphiteMetricData[]> GetMetricsDataAsync(SeriesListBase[] targets, string from = null, string until = null, IDictionary<string,string> template = null, ulong? maxDataPoints = null)
381+
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))
366382
{
367383
if (targets == null || targets.Length == 0) throw new ArgumentNullException(nameof(targets));
368384

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

411-
var response = await client.PostAsync("/render",body).ConfigureAwait(false);
427+
var response = await client.PostAsync("/render",body, cancellationToken).ConfigureAwait(false);
412428
await response.EnsureSuccessStatusCodeAsync().ConfigureAwait(false);
413429

414-
return await response.Content.ReadAsAsync<GraphiteMetricData[]>().ConfigureAwait(false);
430+
return await response.Content.ReadAsAsync<GraphiteMetricData[]>(cancellationToken).ConfigureAwait(false);
415431
}
416432
}
417433
}

0 commit comments

Comments
 (0)