Exemplo n.º 1
0
        public void HttpOutCallsAreCollectedSuccessfullyAsync(HttpTestData.HttpOutTestCase tc)
        {
            using var serverLifeTime = TestHttpServer.RunServer(
                      (ctx) =>
            {
                ctx.Response.StatusCode = tc.ResponseCode == 0 ? 200 : tc.ResponseCode;
                ctx.Response.OutputStream.Close();
            },
                      out var host,
                      out var port);

            var spanProcessor = new Mock <SpanProcessor>();
            var tracer        = TracerFactory.Create(b => b
                                                     .AddProcessorPipeline(p => p.AddProcessor(_ => spanProcessor.Object)))
                                .GetTracer(null);

            tc.Url = HttpTestData.NormalizeValues(tc.Url, host, port);

            using (new HttpWebRequestAdapter(tracer, new HttpClientAdapterOptions()
            {
                SetHttpFlavor = tc.SetHttpFlavor
            }))
            {
                try
                {
                    var request = (HttpWebRequest)WebRequest.Create(tc.Url);

                    request.Method = tc.Method;

                    if (tc.Headers != null)
                    {
                        foreach (var header in tc.Headers)
                        {
                            request.Headers.Add(header.Key, header.Value);
                        }
                    }

                    request.ContentLength = 0;

                    using var response = (HttpWebResponse)request.GetResponse();

                    new StreamReader(response.GetResponseStream()).ReadToEnd();
                }
                catch (Exception)
                {
                    //test case can intentionally send request that will result in exception
                }
            }

            Assert.Equal(2, spanProcessor.Invocations.Count); // begin and end was called
            var span = (SpanData)spanProcessor.Invocations[1].Arguments[0];

            Assert.Equal(tc.SpanName, span.Name);
            Assert.Equal(tc.SpanKind, span.Kind.ToString());

            var d = new Dictionary <StatusCanonicalCode, string>()
            {
                { StatusCanonicalCode.Ok, "OK" },
                { StatusCanonicalCode.Cancelled, "CANCELLED" },
                { StatusCanonicalCode.Unknown, "UNKNOWN" },
                { StatusCanonicalCode.InvalidArgument, "INVALID_ARGUMENT" },
                { StatusCanonicalCode.DeadlineExceeded, "DEADLINE_EXCEEDED" },
                { StatusCanonicalCode.NotFound, "NOT_FOUND" },
                { StatusCanonicalCode.AlreadyExists, "ALREADY_EXISTS" },
                { StatusCanonicalCode.PermissionDenied, "PERMISSION_DENIED" },
                { StatusCanonicalCode.ResourceExhausted, "RESOURCE_EXHAUSTED" },
                { StatusCanonicalCode.FailedPrecondition, "FAILED_PRECONDITION" },
                { StatusCanonicalCode.Aborted, "ABORTED" },
                { StatusCanonicalCode.OutOfRange, "OUT_OF_RANGE" },
                { StatusCanonicalCode.Unimplemented, "UNIMPLEMENTED" },
                { StatusCanonicalCode.Internal, "INTERNAL" },
                { StatusCanonicalCode.Unavailable, "UNAVAILABLE" },
                { StatusCanonicalCode.DataLoss, "DATA_LOSS" },
                { StatusCanonicalCode.Unauthenticated, "UNAUTHENTICATED" },
            };

            Assert.Equal(tc.SpanStatus, d[span.Status.CanonicalCode]);
            if (tc.SpanStatusHasDescription.HasValue)
            {
                Assert.Equal(tc.SpanStatusHasDescription.Value, !string.IsNullOrEmpty(span.Status.Description));
            }

            var normalizedAttributes = span.Attributes.ToDictionary(
                x => x.Key,
                x => x.Value.ToString());

            tc.SpanAttributes = tc.SpanAttributes.ToDictionary(
                x => x.Key,
                x =>
            {
                if (x.Key == "http.flavor" && x.Value == "2.0")
                {
                    return("1.1");
                }
                return(HttpTestData.NormalizeValues(x.Value, host, port));
            });

            Assert.Equal(tc.SpanAttributes, normalizedAttributes);
        }
        public async Task DebugIndividualTestAsync()
        {
            var serializer = new JsonSerializer();
            var input      = serializer.Deserialize <HttpTestData.HttpOutTestCase[]>(new JsonTextReader(new StringReader(@"
[
  {
    ""name"": ""Response code: 399"",
    ""method"": ""GET"",
    ""url"": ""http://{host}:{port}/"",
    ""responseCode"": 399,
    ""spanName"": ""HTTP GET"",
    ""spanStatus"": ""OK"",
    ""spanKind"": ""Client"",
    ""spanAttributes"": {
      ""component"": ""http"",
      ""http.method"": ""GET"",
      ""http.host"": ""{host}:{port}"",
      ""http.status_code"": ""399"",
      ""http.url"": ""http://{host}:{port}/""
    }
  }
]
")));

            var   t = (Task)this.GetType().InvokeMember(nameof(HttpOutCallsAreCollectedSuccessfullyAsync), BindingFlags.InvokeMethod, null, this, HttpTestData.GetArgumentsFromTestCaseObject(input).First());
            await t;
        }