Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 60 additions & 13 deletions Conductor/Client/ApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
using Conductor.Client.Telemetry;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using RestSharp;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
Expand All @@ -29,6 +31,12 @@
/// </summary>
public partial class ApiClient
{
/// <summary>
/// Optional metrics collector for recording http_api_client_request_seconds.
/// Set once via DI or startup; safe to leave null.
/// </summary>
public static MetricsCollector Metrics { get; set; }

public JsonSerializerSettings serializerSettings = new JsonSerializerSettings
{
ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor
Expand All @@ -54,7 +62,7 @@
public ApiClient(int timeOut)
{
Configuration = Conductor.Client.Configuration.Default;
RestClient = new RestClient(options: new RestClientOptions() { BaseUrl = new Uri("https://play.orkes.io/api"), MaxTimeout = timeOut });

Check warning on line 65 in Conductor/Client/ApiClient.cs

View workflow job for this annotation

GitHub Actions / unit_tests

'RestClientOptions.MaxTimeout' is obsolete: 'Use Timeout instead.'

Check warning on line 65 in Conductor/Client/ApiClient.cs

View workflow job for this annotation

GitHub Actions / legacy_integration_tests

'RestClientOptions.MaxTimeout' is obsolete: 'Use Timeout instead.'

Check warning on line 65 in Conductor/Client/ApiClient.cs

View workflow job for this annotation

GitHub Actions / integration_tests

'RestClientOptions.MaxTimeout' is obsolete: 'Use Timeout instead.'
}

/// <summary>
Expand Down Expand Up @@ -179,11 +187,30 @@
Dictionary<String, FileParameter> fileParams, Dictionary<String, String> pathParams,
String contentType, Configuration configuration)
{
int retryCount = 0;
RestResponse response = RetryRestClientCallApi(path, method, queryParams, postBody, headerParams,
formParams, fileParams, pathParams, contentType, configuration, ref retryCount);

return (Object)response;
var sw = Stopwatch.StartNew();
string statusCode = "0";
try
{
int retryCount = 0;
RestResponse response = RetryRestClientCallApi(path, method, queryParams, postBody, headerParams,
formParams, fileParams, pathParams, contentType, configuration, ref retryCount);
statusCode = ((int)response.StatusCode).ToString();
return (Object)response;
}
catch
{
statusCode = "0";
throw;
}
finally
{
sw.Stop();
var resolvedUri = path;
foreach (var param in pathParams)
resolvedUri = resolvedUri.Replace("{" + param.Key + "}", param.Value);
var basePath = RestClient.Options.BaseUrl?.AbsolutePath?.TrimEnd('/') ?? "";
Metrics?.RecordHttpApiClientRequest(method.ToString().ToUpperInvariant(), basePath + resolvedUri, statusCode, sw.Elapsed.TotalSeconds);
}
}

private RestResponse RetryRestClientCallApi(String path, Method method, List<KeyValuePair<String, String>> queryParams, Object postBody,
Expand Down Expand Up @@ -226,15 +253,35 @@
Dictionary<String, FileParameter> fileParams, Dictionary<String, String> pathParams,
String contentType)
{
var request = PrepareRequest(
path, method, queryParams, postBody, headerParams, formParams, fileParams,
pathParams, contentType);
var sw = Stopwatch.StartNew();
string statusCode = "0";
try
{
var request = PrepareRequest(
path, method, queryParams, postBody, headerParams, formParams, fileParams,
pathParams, contentType);

InterceptRequest(request);
var response = await RestClient.ExecuteAsync(request, method);
InterceptResponse(request, response);
FormatHeaders(response);
return (object)response;
InterceptRequest(request);
var response = await RestClient.ExecuteAsync(request, method);
InterceptResponse(request, response);
FormatHeaders(response);
statusCode = ((int)response.StatusCode).ToString();
return (object)response;
}
catch
{
statusCode = "0";
throw;
}
finally
{
sw.Stop();
var resolvedUri = path;
foreach (var param in pathParams)
resolvedUri = resolvedUri.Replace("{" + param.Key + "}", param.Value);
var basePath = RestClient.Options.BaseUrl?.AbsolutePath?.TrimEnd('/') ?? "";
Metrics?.RecordHttpApiClientRequest(method.ToString().ToUpperInvariant(), basePath + resolvedUri, statusCode, sw.Elapsed.TotalSeconds);
}
}

/// <summary>
Expand Down
7 changes: 6 additions & 1 deletion Conductor/Client/Extensions/DependencyInjectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ public static IServiceCollection AddConductorWorker(this IServiceCollection serv
}
services.AddSingleton<Configuration>(configuration);
services.AddSingleton<IWorkflowTaskClient, WorkflowTaskHttpClient>();
services.AddSingleton<MetricsCollector>();
services.AddSingleton<MetricsCollector>(sp =>
{
var collector = new MetricsCollector();
ApiClient.Metrics = collector;
return collector;
});
services.AddTransient<IWorkflowTaskCoordinator, WorkflowTaskCoordinator>();
return services;
}
Expand Down
Loading
Loading