This repository has been archived by the owner on Apr 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 68
/
HttpSamplingManager.cs
53 lines (44 loc) 路 1.82 KB
/
HttpSamplingManager.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
锘縰sing System;
using System.Threading.Tasks;
using Jaeger.Samplers.HTTP;
using Jaeger.Util;
using Newtonsoft.Json;
namespace Jaeger.Samplers
{
public class HttpSamplingManager : ISamplingManager
{
public const string DefaultHostPort = "127.0.0.1:5778";
public const string DefaultEndpoint = "http://" + DefaultHostPort + "/sampling";
private readonly IHttpClient _httpClient;
private readonly string _endpoint;
public HttpSamplingManager(string endpoint = DefaultEndpoint)
: this(new DefaultHttpClient(), endpoint)
{
}
public HttpSamplingManager(IHttpClient httpClient, string endpoint = DefaultEndpoint)
{
_httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient));
_endpoint = endpoint ?? DefaultEndpoint;
// Workaround for obsolete HostPort notation.
// UriBuilder needs the schema if host is not an IP and port is given:
if (!_endpoint.StartsWith("http://") && !_endpoint.StartsWith("https://"))
{
_endpoint = "http://" + _endpoint;
}
}
internal SamplingStrategyResponse ParseJson(string json)
{
return JsonConvert.DeserializeObject<SamplingStrategyResponse>(json);
}
public async Task<SamplingStrategyResponse> GetSamplingStrategyAsync(string serviceName)
{
Uri uri = new UriBuilder(_endpoint) {Query = "service=" + Uri.EscapeDataString(serviceName)}.Uri;
string jsonString = await _httpClient.MakeGetRequestAsync(uri.AbsoluteUri).ConfigureAwait(false);
return ParseJson(jsonString);
}
public override string ToString()
{
return $"{nameof(HttpSamplingManager)}(Endpoint={_endpoint})";
}
}
}