protected override async Task <EventPosition> GetLastEventPosition(CancellationToken cancellationToken)
        {
            // Subscription metrics are sampled each 60 sec, so we need to use an extended period
            var interval = new TimeInterval {
                StartTime = Timestamp.FromDateTime(DateTime.UtcNow - TimeSpan.FromMinutes(2)),
                EndTime   = Timestamp.FromDateTime(DateTime.UtcNow)
            };

            var undelivered = await GetPoint(_undeliveredCountRequest);

            var oldestAge = await GetPoint(_oldestAgeRequest);

            var age = oldestAge == null ? DateTime.UtcNow : DateTime.UtcNow.AddSeconds(-oldestAge.Value.Int64Value);

            return(new EventPosition((ulong?)undelivered?.Value?.Int64Value, age));

            async Task <Point?> GetPoint(ListTimeSeriesRequest request)
            {
                request.Interval = interval;

                var result = _metricClient.ListTimeSeriesAsync(request);
                var page   = await result.ReadPageAsync(1, cancellationToken);

                return(page.FirstOrDefault()?.Points?.FirstOrDefault());
            }
        }
Exemplo n.º 2
0
    public async ValueTask <SubscriptionGap> GetSubscriptionGap(CancellationToken cancellationToken)
    {
        if (!_monitoringEnabled)
        {
            return(new SubscriptionGap(_subscriptionId, 0, TimeSpan.Zero));
        }

        var now = DateTime.UtcNow;

        // Subscription metrics are sampled each 60 sec, so we need to use an extended period
        var interval = new TimeInterval {
            StartTime = Timestamp.FromDateTime(now - TimeSpan.FromMinutes(2)),
            EndTime   = Timestamp.FromDateTime(now)
        };

        var undelivered = await GetPoint(_undeliveredCountRequest).NoContext();

        var oldestAge = await GetPoint(_oldestAgeRequest).NoContext();

        var age = oldestAge == null
            ? TimeSpan.Zero
            : TimeSpan.FromSeconds(oldestAge.Value.Int64Value);

        return(new SubscriptionGap(_subscriptionId, (ulong)(undelivered?.Value?.Int64Value ?? 0), age));

        async Task <Point?> GetPoint(ListTimeSeriesRequest request)
        {
            request.Interval = interval;

            var result = _metricClient.ListTimeSeriesAsync(request);
            var page   = await result.ReadPageAsync(1, cancellationToken).NoContext();

            return(page.FirstOrDefault()?.Points?.FirstOrDefault());
        }
    }
Exemplo n.º 3
0
        /// <summary>Snippet for ListTimeSeriesAsync</summary>
        public async Task ListTimeSeriesAsync_RequestObject()
        {
            // Snippet: ListTimeSeriesAsync(ListTimeSeriesRequest,CallSettings)
            // Create client
            MetricServiceClient metricServiceClient = await MetricServiceClient.CreateAsync();

            // Initialize request argument(s)
            ListTimeSeriesRequest request = new ListTimeSeriesRequest
            {
                ProjectName = new ProjectName("[PROJECT]"),
                Filter      = "",
                Interval    = new TimeInterval(),
                View        = ListTimeSeriesRequest.Types.TimeSeriesView.Full,
            };
            // Make the request
            PagedAsyncEnumerable <ListTimeSeriesResponse, TimeSeries> response =
                metricServiceClient.ListTimeSeriesAsync(request);

            // Iterate over all response items, lazily performing RPCs as required
            await response.ForEachAsync((TimeSeries item) =>
            {
                // Do something with each item
                Console.WriteLine(item);
            });

            // Or iterate over pages (of server-defined size), performing one RPC per page
            await response.AsRawResponses().ForEachAsync((ListTimeSeriesResponse page) =>
            {
                // Do something with each page of items
                Console.WriteLine("A page of results:");
                foreach (TimeSeries item in page)
                {
                    Console.WriteLine(item);
                }
            });

            // Or retrieve a single page of known size (unless it's the final page), performing as many RPCs as required
            int pageSize = 10;
            Page <TimeSeries> singlePage = await response.ReadPageAsync(pageSize);

            // Do something with the page of items
            Console.WriteLine($"A page of {pageSize} results (unless it's the final page):");
            foreach (TimeSeries item in singlePage)
            {
                Console.WriteLine(item);
            }
            // Store the pageToken, for when the next page is required.
            string nextPageToken = singlePage.NextPageToken;
            // End snippet
        }
Exemplo n.º 4
0
        public async Task ListTimeSeriesAsync()
        {
            // Snippet: ListTimeSeriesAsync(string,string,TimeInterval,ListTimeSeriesRequest.Types.TimeSeriesView,string,int?,CallSettings)
            // Create client
            MetricServiceClient metricServiceClient = MetricServiceClient.Create();
            // Initialize request argument(s)
            string       formattedName = MetricServiceClient.FormatProjectName("[PROJECT]");
            string       filter        = "";
            TimeInterval interval      = new TimeInterval();

            ListTimeSeriesRequest.Types.TimeSeriesView view = ListTimeSeriesRequest.Types.TimeSeriesView.Full;
            // Make the request
            IPagedAsyncEnumerable <ListTimeSeriesResponse, TimeSeries> response =
                metricServiceClient.ListTimeSeriesAsync(formattedName, filter, interval, view);

            // Iterate over all response items, lazily performing RPCs as required
            await response.ForEachAsync((TimeSeries item) =>
            {
                // Do something with each item
                Console.WriteLine(item);
            });

            // Or iterate over fixed-sized pages, lazily performing RPCs as required
            int pageSize = 10;
            IAsyncEnumerable <FixedSizePage <TimeSeries> > fixedSizePages = response.AsPages().WithFixedSize(pageSize);
            await fixedSizePages.ForEachAsync((FixedSizePage <TimeSeries> page) =>
            {
                // Do something with each page of items
                Console.WriteLine("A page of results:");
                foreach (TimeSeries item in page)
                {
                    Console.WriteLine(item);
                }
            });

            // End snippet
        }