public static AnalyticsRequest Create(string statement, AnalyticsOptions options) { return(new(options.ReadonlyValue) { RetryStrategy = options.RetryStrategyValue ?? new BestEffortRetryStrategy(), Timeout = options.TimeoutValue !.Value, ClientContextId = options.ClientContextIdValue, Statement = statement, Token = options.Token, Options = options });
private IRequestSpan RootSpan(string operation, AnalyticsOptions options) { var span = _tracer.RequestSpan(operation, options.RequestSpanValue); span.SetAttribute(OuterRequestSpans.Attributes.System.Key, OuterRequestSpans.Attributes.System.Value); span.SetAttribute(OuterRequestSpans.Attributes.Service, nameof(OuterRequestSpans.ServiceSpan.AnalyticsQuery).ToLowerInvariant()); span.SetAttribute(OuterRequestSpans.Attributes.BucketName, options.BucketName !); span.SetAttribute(OuterRequestSpans.Attributes.ScopeName, options.ScopeName !); span.SetAttribute(OuterRequestSpans.Attributes.Operation, operation); return(span); }
private IRequestSpan RootSpan(string operation, AnalyticsOptions options) { var span = _tracer.RequestSpan(operation, options.RequestSpanValue); if (span.CanWrite) { span.SetAttribute(OuterRequestSpans.Attributes.System.Key, OuterRequestSpans.Attributes.System.Value); span.SetAttribute(OuterRequestSpans.Attributes.Service, OuterRequestSpans.ServiceSpan.AnalyticsQuery); span.SetAttribute(OuterRequestSpans.Attributes.BucketName, options.BucketName !); span.SetAttribute(OuterRequestSpans.Attributes.ScopeName, options.ScopeName !); span.SetAttribute(OuterRequestSpans.Attributes.Operation, operation); } return(span); }
public async Task <IAnalyticsResult <T> > QueryAsync <T>(string statement, AnalyticsOptions options) { using var rootSpan = RootSpan(OuterRequestSpans.ServiceSpan.AnalyticsQuery, options) .WithOperationId(options) .WithLocalAddress(); // try get Analytics node var analyticsUri = _serviceUriProvider.GetRandomAnalyticsUri(); rootSpan.WithRemoteAddress(analyticsUri); _logger.LogDebug("Sending analytics query with a context id {contextId} to server {searchUri}", options.ClientContextIdValue, analyticsUri); using var encodingSpan = rootSpan.EncodingSpan(); AnalyticsResultBase <T> result; var body = options.GetFormValuesAsJson(statement); using (var content = new StringContent(body, Encoding.UTF8, MediaType.Json)) { try { var request = new HttpRequestMessage(HttpMethod.Post, analyticsUri) { Content = content }; if (options.PriorityValue != 0) { request.Headers.Add(AnalyticsPriorityHeaderName, new[] { options.PriorityValue.ToString() }); } encodingSpan.Dispose(); using var dispatchSpan = rootSpan.DispatchSpan(options); var response = await HttpClient.SendAsync(request, options.Token).ConfigureAwait(false); dispatchSpan.Dispose(); var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false); if (_typeSerializer is IStreamingTypeDeserializer streamingTypeDeserializer) { result = new StreamingAnalyticsResult <T>(stream, streamingTypeDeserializer) { HttpStatusCode = response.StatusCode }; } else { result = new BlockAnalyticsResult <T>(stream, _typeSerializer) { HttpStatusCode = response.StatusCode }; } await result.InitializeAsync(options.Token).ConfigureAwait(false); if (response.StatusCode != HttpStatusCode.OK) { if (result.ShouldRetry()) { UpdateLastActivity(); return(result); } var context = new AnalyticsErrorContext { ClientContextId = options.ClientContextIdValue, HttpStatus = response.StatusCode, Statement = statement, Parameters = options.GetParametersAsJson(), Errors = result.Errors }; if (result.LinkNotFound()) { throw new LinkNotFoundException(context); } if (result.DataverseExists()) { throw new DataverseExistsException(context); } if (result.DatasetExists()) { throw new DatasetExistsException(); } if (result.DataverseNotFound()) { throw new DataverseNotFoundException(context); } if (result.DataSetNotFound()) { throw new DatasetNotFoundException(context); } if (result.JobQueueFull()) { throw new JobQueueFullException(context); } if (result.CompilationFailure()) { throw new CompilationFailureException(context); } if (result.InternalServerFailure()) { throw new InternalServerFailureException(context); } if (result.AuthenticationFailure()) { throw new AuthenticationFailureException(context); } if (result.TemporaryFailure()) { throw new TemporaryFailureException(context); } if (result.ParsingFailure()) { throw new ParsingFailureException(context); } if (result.IndexNotFound()) { throw new IndexNotFoundException(context); } if (result.IndexExists()) { throw new IndexExistsException(context); } } } catch (OperationCanceledException e) { //treat as an orphaned response rootSpan.LogOrphaned(); var context = new AnalyticsErrorContext { ClientContextId = options.ClientContextIdValue, Statement = statement, Parameters = options.GetParametersAsJson() }; _logger.LogDebug(LoggingEvents.AnalyticsEvent, e, "Analytics request timeout."); if (options.ReadonlyValue) { throw new UnambiguousTimeoutException("The query was timed out via the Token.", e) { Context = context }; } throw new AmbiguousTimeoutException("The query was timed out via the Token.", e) { Context = context }; } catch (HttpRequestException e) { //treat as an orphaned response rootSpan.LogOrphaned(); var context = new AnalyticsErrorContext { ClientContextId = options.ClientContextIdValue, Statement = statement, Parameters = options.GetParametersAsJson() }; _logger.LogDebug(LoggingEvents.AnalyticsEvent, e, "Analytics request cancelled."); throw new RequestCanceledException("The query was canceled.", e) { Context = context }; } } UpdateLastActivity(); return(result); }