[InlineData("C:\\")] // no host (Basic host name type) public void TryExtractDestinationInfo_invalid_input(string inputUrl) { var mockLogger = new TestLogger(LogLevel.Trace); UrlUtils.ExtractDestination(new Uri(inputUrl), mockLogger).Should().BeNull(); mockLogger.Lines.Should().Contain(line => line.Contains(nameof(UrlUtils)) && line.Contains(nameof(UrlUtils.ExtractDestination))); }
[InlineData("http://Хост", "Хост", DefaultHttpPort)] // Host in Russian // ReSharper restore StringLiteralTypo public void TryExtractDestinationInfo_valid_input(string inputUrl, string expectedHost, int?expectedPort) { var actualDestination = UrlUtils.ExtractDestination(new Uri(inputUrl), new NoopLogger()); actualDestination.Should().NotBeNull(); actualDestination.Address.Should().Be(expectedHost); actualDestination.Port.Should().Be(expectedPort); }
protected override void HandleOnNext(KeyValuePair <string, object> kv) { Logger.Trace() ?.Log("{currentClassName} received diagnosticsource event: {eventKey}", nameof(GrpcClientDiagnosticListener), kv.Key); var currentActivity = Activity.Current; if (kv.Key == "Grpc.Net.Client.GrpcOut.Start") { if (kv.Value.GetType().GetTypeInfo().GetDeclaredProperty("Request")?.GetValue(kv.Value) is HttpRequestMessage requestObject) { var currentTransaction = ApmAgent?.Tracer.CurrentTransaction; if (currentTransaction != null) { var grpcMethodName = currentActivity?.Tags.FirstOrDefault(n => n.Key == "grpc.method").Value; if (string.IsNullOrEmpty(grpcMethodName)) { grpcMethodName = "unknown"; } Logger.Trace()?.Log("Starting span for gRPC call, method:{methodName}", grpcMethodName); var newSpan = currentTransaction.StartSpan(grpcMethodName, ApiConstants.TypeExternal, ApiConstants.SubTypeGrpc, isExitSpan: true); ProcessingRequests.TryAdd(requestObject, newSpan); } } } if (kv.Key == "Grpc.Net.Client.GrpcOut.Stop") { if (kv.Value.GetType().GetTypeInfo().GetDeclaredProperty("Request")?.GetValue(kv.Value) is not HttpRequestMessage requestObject) { return; } if (!ProcessingRequests.TryRemove(requestObject, out var span)) { return; } Logger.Trace()?.Log("Ending span for gRPC call, span:{span}", span); var grpcStatusCode = currentActivity?.Tags?.Where(n => n.Key == "grpc.status_code").FirstOrDefault().Value; if (grpcStatusCode != null) { span.Outcome = GrpcHelper.GrpcClientReturnCodeToOutcome(GrpcHelper.GrpcReturnCodeToString(grpcStatusCode)); } span.Context.Destination = UrlUtils.ExtractDestination(requestObject.RequestUri, Logger); span.Context.Destination.Service = new() { Resource = UrlUtils.ExtractService(requestObject.RequestUri, span) }; span.End(); } } }
private Destination DeduceHttpDestination() { try { return(UrlUtils.ExtractDestination(Context.Http.OriginalUrl ?? new Uri(Context.Http.Url), _logger)); } catch (Exception ex) { _logger.Trace()?.LogException(ex, "Failed to deduce destination info from Context.Http." + " Original URL: {OriginalUrl}. Context.Http.Url: {Context.Http.Url}." , Context.Http.OriginalUrl, Context.Http.Url); return(null); } }