Пример #1
0
        internal static QueryBody CreateQueryBody(string query, TimeSpan?timeSpan, LogsQueryOptions options, out string prefer)
        {
            var queryBody = new QueryBody(query);

            if (timeSpan != null)
            {
                queryBody.Timespan = TypeFormatters.ToString(timeSpan.Value, "P");
            }

            prefer = null;

            if (options?.Timeout is TimeSpan timeout)
            {
                prefer = "wait=" + (int)timeout.TotalSeconds;
            }

            if (options?.IncludeStatistics == true)
            {
                prefer += " include-statistics=true";
            }

            return(queryBody);
        }
Пример #2
0
        /// <summary>
        /// Executes the logs query.
        /// </summary>
        /// <param name="workspace">The workspace to include in the query.</param>
        /// <param name="query">The query text to execute.</param>
        /// <param name="timeRange">The timespan over which to query data. Logs would be filtered to include entries produced starting at <c>Now - timeSpan</c>. </param>
        /// <param name="options">The <see cref="LogsQueryOptions"/> to configure the query.</param>
        /// <param name="cancellationToken">The <see cref="CancellationToken"/> to use.</param>
        /// <returns>Query results mapped to a type <typeparamref name="T"/>.</returns>
        public virtual Response <IReadOnlyList <T> > Query <T>(string workspace, string query, DateTimeRange timeRange, LogsQueryOptions options = null, CancellationToken cancellationToken = default)
        {
            Response <LogsQueryResult> response = Query(workspace, query, timeRange, options, cancellationToken);

            return(Response.FromValue(_rowBinder.BindResults <T>(response.Value.Tables), response.GetRawResponse()));
        }
Пример #3
0
 /// <summary>
 /// Executes the logs query.
 /// </summary>
 /// <param name="workspace">The workspace to include in the query.</param>
 /// <param name="query">The query text to execute.</param>
 /// <param name="timeRange">The timespan over which to query data. Logs would be filtered to include entries produced starting at <c>Now - timeSpan</c>. </param>
 /// <param name="options">The <see cref="LogsQueryOptions"/> to configure the query.</param>
 /// <param name="cancellationToken">The <see cref="CancellationToken"/> to use.</param>
 /// <returns>The <see cref="LogsQueryResult"/> with the query results.</returns>
 public virtual async Task <Response <LogsQueryResult> > QueryAsync(string workspace, string query, DateTimeRange timeRange, LogsQueryOptions options = null, CancellationToken cancellationToken = default)
 {
     using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(LogsQueryClient)}.{nameof(Query)}");
     scope.Start();
     try
     {
         return(await ExecuteAsync(workspace, query, timeRange, options, true, cancellationToken).ConfigureAwait(false));
     }
     catch (Exception e)
     {
         scope.Failed(e);
         throw;
     }
 }
Пример #4
0
 /// <summary>
 /// Executes the logs query.
 /// </summary>
 /// <param name="workspace">The workspace to include in the query.</param>
 /// <param name="query">The query text to execute.</param>
 /// <param name="timeRange">The timespan over which to query data. Logs would be filtered to include entries produced starting at <c>Now - timeSpan</c>. </param>
 /// <param name="options">The <see cref="LogsQueryOptions"/> to configure the query.</param>
 /// <param name="cancellationToken">The <see cref="CancellationToken"/> to use.</param>
 /// <returns>The <see cref="LogsQueryResult"/> containing the query results.</returns>
 public virtual Response <LogsQueryResult> Query(string workspace, string query, DateTimeRange timeRange, LogsQueryOptions options = null, CancellationToken cancellationToken = default)
 {
     using DiagnosticScope scope = _clientDiagnostics.CreateScope($"{nameof(LogsQueryClient)}.{nameof(Query)}");
     scope.Start();
     try
     {
         return(ExecuteAsync(workspace, query, timeRange, options, false, cancellationToken).EnsureCompleted());
     }
     catch (Exception e)
     {
         scope.Failed(e);
         throw;
     }
 }
Пример #5
0
        /// <summary>
        /// Executes the logs query.
        /// </summary>
        /// <param name="workspace">The workspace to include in the query.</param>
        /// <param name="query">The query text to execute.</param>
        /// <param name="timeRange">The timespan over which to query data. Logs would be filtered to include entries produced starting at <c>Now - timeSpan</c>. </param>
        /// <param name="options">The <see cref="LogsQueryOptions"/> to configure the query.</param>
        /// <param name="cancellationToken">The <see cref="CancellationToken"/> to use.</param>
        /// <returns>Query results mapped to a type <typeparamref name="T"/>.</returns>
        public virtual async Task <Response <IReadOnlyList <T> > > QueryAsync <T>(string workspace, string query, DateTimeRange timeRange, LogsQueryOptions options = null, CancellationToken cancellationToken = default)
        {
            Response <LogsQueryResult> response = await QueryAsync(workspace, query, timeRange, options, cancellationToken).ConfigureAwait(false);

            return(Response.FromValue(_rowBinder.BindResults <T>(response.Value.Tables), response.GetRawResponse()));
        }
Пример #6
0
        /// <summary>
        /// Adds the specified query to the batch. Results can be retrieved after the query is submitted via the <see cref="LogsClient.QueryBatchAsync"/> call.
        /// </summary>
        /// <param name="workspaceId">The workspace to include in the query.</param>
        /// <param name="query">The query text to execute.</param>
        /// <param name="timeRange">The timespan over which to query data.</param>
        /// <param name="options">The <see cref="LogsQueryOptions"/> to configure the query.</param>
        /// <returns>The query identifier that has to be passed into <see cref="LogsBatchQueryResult.GetResult"/> to get the result.</returns>
        public virtual string AddQuery(string workspaceId, string query, DateTimeRange timeRange, LogsQueryOptions options = null)
        {
            var id = _counter.ToString("G", CultureInfo.InvariantCulture);

            _counter++;
            var logQueryRequest = new LogQueryRequest()
            {
                Id        = id,
                Body      = LogsClient.CreateQueryBody(query, timeRange, options, out string prefer),
                Workspace = workspaceId
            };

            if (prefer != null)
            {
                logQueryRequest.Headers.Add("prefer", prefer);
            }
            Batch.Requests.Add(logQueryRequest);
            return(id);
        }
    }
        private async Task <Response <LogsQueryResult> > ExecuteAsync(string workspaceId, string query, DateTimeRange timeRange, LogsQueryOptions options, bool async, CancellationToken cancellationToken = default)
        {
            if (workspaceId == null)
            {
                throw new ArgumentNullException(nameof(workspaceId));
            }

            QueryBody queryBody = CreateQueryBody(query, timeRange, options, out string prefer);

            using var message = _queryClient.CreateExecuteRequest(workspaceId, queryBody, prefer);

            // TODO: https://github.com/Azure/azure-sdk-for-net/issues/20859
            // if (options?.Timeout != null)
            // {
            //     message.NetworkTimeout = options.Timeout;
            // }

            if (async)
            {
                await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false);
            }
            else
            {
                _pipeline.Send(message, cancellationToken);
            }

            switch (message.Response.Status)
            {
            case 200:
            {
                using var document = async ?
                                     await JsonDocument.ParseAsync(message.Response.ContentStream, default, cancellationToken).ConfigureAwait(false) :
                                     JsonDocument.Parse(message.Response.ContentStream, default);

                LogsQueryResult value = LogsQueryResult.DeserializeLogsQueryResult(document.RootElement);
                return(Response.FromValue(value, message.Response));
            }

            default:
            {
                if (async)
                {
                    throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(false);
                }
                else
                {
                    throw _clientDiagnostics.CreateRequestFailedException(message.Response);
                }
            }
            }
        }
        internal static QueryBody CreateQueryBody(string query, DateTimeRange timeRange, LogsQueryOptions options, out string prefer)
        {
            var queryBody = new QueryBody(query);

            if (timeRange != DateTimeRange.MaxValue)
            {
                queryBody.Timespan = timeRange.ToString();
            }

            prefer = null;

            if (options?.Timeout is TimeSpan timeout)
            {
                prefer = "wait=" + (int)timeout.TotalSeconds;
            }

            if (options?.IncludeStatistics == true)
            {
                prefer += " include-statistics=true";
            }

            return(queryBody);
        }
        private async Task <Response <LogsQueryResult> > ExecuteAsync(string workspaceId, string query, DateTimeRange timeRange, LogsQueryOptions options, bool async, CancellationToken cancellationToken = default)
        {
            if (workspaceId == null)
            {
                throw new ArgumentNullException(nameof(workspaceId));
            }

            QueryBody queryBody = CreateQueryBody(query, timeRange, options, out string prefer);

            using var message = _queryClient.CreateExecuteRequest(workspaceId, queryBody, prefer);

            if (options?.ServerTimeout != null)
            {
                // Offset the service timeout a bit to make sure we have time to receive the response.
                message.NetworkTimeout = options.ServerTimeout.Value.Add(_networkTimeoutOffset);
            }

            if (async)
            {
                await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false);
            }
            else
            {
                _pipeline.Send(message, cancellationToken);
            }

            switch (message.Response.Status)
            {
            case 200:
            {
                using var document = async ?
                                     await JsonDocument.ParseAsync(message.Response.ContentStream, default, cancellationToken).ConfigureAwait(false) :
                                     JsonDocument.Parse(message.Response.ContentStream, default);

                LogsQueryResult value = LogsQueryResult.DeserializeLogsQueryResult(document.RootElement);
                return(Response.FromValue(value, message.Response));
            }

            default:
            {
                if (async)
                {
                    throw await _clientDiagnostics.CreateRequestFailedExceptionAsync(message.Response).ConfigureAwait(false);
                }
                else
                {
                    throw _clientDiagnostics.CreateRequestFailedException(message.Response);
                }
            }
            }
        }
Пример #10
0
        internal static QueryBody CreateQueryBody(string query, DateTimeRange timeRange, LogsQueryOptions options, out string prefer)
        {
            var queryBody = new QueryBody(query);

            if (timeRange != DateTimeRange.All)
            {
                queryBody.Timespan = timeRange.ToString();
            }

            if (options != null)
            {
                queryBody.Workspaces = options.AdditionalWorkspaces;
            }

            prefer = null;

            StringBuilder preferBuilder = null;

            if (options?.ServerTimeout is TimeSpan timeout)
            {
                preferBuilder ??= new();
                preferBuilder.Append("wait=");
                preferBuilder.Append((int)timeout.TotalSeconds);
            }

            if (options?.IncludeStatistics == true)
            {
                if (preferBuilder == null)
                {
                    preferBuilder = new();
                }
                else
                {
                    preferBuilder.Append(',');
                }

                preferBuilder.Append("include-statistics=true");
            }

            if (options?.IncludeVisualization == true)
            {
                if (preferBuilder == null)
                {
                    preferBuilder = new();
                }
                else
                {
                    preferBuilder.Append(',');
                }

                preferBuilder.Append("include-render=true");
            }

            prefer = preferBuilder?.ToString();

            return(queryBody);
        }
Пример #11
0
        public virtual Response <IReadOnlyList <T> > Query <T>(string workspaceId, string query, TimeSpan?timeSpan = null, LogsQueryOptions options = null, CancellationToken cancellationToken = default)
        {
            Response <LogsQueryResult> response = Query(workspaceId, query, timeSpan, options, cancellationToken);

            return(Response.FromValue(_rowBinder.BindResults <T>(response), response.GetRawResponse()));
        }