Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SDK doesn't implement MetricsData retrieval #31

Open
the-avant-it opened this issue Nov 7, 2024 · 2 comments
Open

SDK doesn't implement MetricsData retrieval #31

the-avant-it opened this issue Nov 7, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@the-avant-it
Copy link

It seems that SDK only implements gRPC endpoints, but YandexCloud has only REST endpoint for metric data retrieval.

Doc reference to endpoints I'am talking about: https://yandex.cloud/en-ru/docs/monitoring/api-ref/MetricsData/

@nikolaymatrosov
Copy link
Contributor

You are right. The SDK does not implement clients for REST-based APIs.

@the-avant-it
Copy link
Author

the-avant-it commented Nov 9, 2024

Sorry, I don't have time to implement this support myself, but here is the code I ended up using:

public class GetMetricRequestDownsampling
{
    public string? gridAggregation { get; set; } = "AVG";
    public string? gapFilling { get; set; } = "PREVIOUS";
    public string? maxPoints { get; set; }
    public string? gridInterval { get; set; }
    public bool? disabled { get; set; } = false;
}

/// <summary>
/// https://json2csharp.com/
/// https://yandex.cloud/en-ru/docs/monitoring/api-ref/MetricsData/read
/// </summary>
public class GetMetricRequest
{
    /// <summary>
    /// <example>cpu_utilization{service="compute", resource_type="vm", resource_id="fhme0qcfsgi4pv56v9pn"}</example>
    /// </summary>
    public string query { get; set; }
    /// <summary>
    /// RFC3339
    /// </summary>
    /// <example>2024-11-07T07:22:07+00:00</example>
    public string fromTime { get; set; }
    /// <summary>
    /// RFC3339
    /// </summary>
    /// <example>2024-11-07T07:22:07+00:00</example>
    public string toTime { get; set; }
    public GetMetricRequestDownsampling downsampling { get; set; }
}

public class GetMetricResponseMetric
{
    /// <summary>
    /// <example>cpu_utilization</example>
    /// </summary>
    public string name { get; set; }
    /// <summary>
    /// <example>{"cpu_name":"cpu_1","service":"compute","resource_type":"vm","resource_id":"ipsec"}</example>
    /// </summary>
    public IDictionary<string, string> labels { get; set; }
    /// <summary>
    /// <example>DGAUGE</example>
    /// </summary>
    public string type { get; set; }
    public GetMetricResponseMetricTimeseries timeseries { get; set; }
}

/// <summary>
/// https://yandex.cloud/en-ru/docs/monitoring/api-ref/MetricsData/read
/// </summary>
public class GetMetricResponse
{
    public IList<GetMetricResponseMetric> metrics { get; set; }
}

public class GetMetricResponseMetricTimeseries
{
    public IList<long> timestamps { get; set; }
    public IList<double> doubleValues { get; set; }
    public IList<long> int64Values { get; set; }
}

public class YandexCloudRestClient
{
    private readonly HttpClient _client;
    private readonly ILogger<YandexCloudRestClient> _logger;

    public YandexCloudRestClient(HttpClient client, ILogger<YandexCloudRestClient> logger)
    {
        _client = client;
        _logger = logger;
    }

    public async Task InitializeAsync(ICredentialsProvider credentials)
    {
        _client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", credentials.GetToken());
    }

    /// <summary>
    /// See: https://yandex.cloud/en-ru/docs/monitoring/api-ref/MetricsData/read
    /// </summary>
    /// <param name="folderId"></param>
    /// <param name="request"></param>
    /// <returns></returns>
    public async Task<GetMetricResponse> GetMetricsAsync(string folderId, GetMetricRequest request)
    {
        var response = await _client.PostAsync(
            $"https://monitoring.api.cloud.yandex.net/monitoring/v2/data/read?folderId={folderId}",
            new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json")
        );
        response.EnsureSuccessStatusCode();
        
        return JsonConvert.DeserializeObject<GetMetricResponse>(await response.Content.ReadAsStringAsync());
    }
}

Usage:

        // See: https://github.com/yandex-cloud/dotnet-sdk/issues/29
        var configuration = System.Text.Json.JsonSerializer.Deserialize<IamJwtCredentialsConfiguration>(key);
        await _yandexRest.InitializeAsync(new IamJwtCredentialsProvider(configuration));
        var responce = await _yandexRest.GetMetricsAsync(_folderId, new GetMetricRequest()
        {
            fromTime = request.From.ToUniversalTime().ToString("yyyy-MM-dd'T'HH:mm:ss.fffK"),
            toTime = request.To.ToUniversalTime().ToString("yyyy-MM-dd'T'HH:mm:ss.fffK"),
            downsampling = new GetMetricRequestDownsampling(),
            query = "YOUR Query"
        });

@opportunity356 opportunity356 added the enhancement New feature or request label Dec 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants