public void ParseTags_ReturnsExpected()
        {
            var opts  = new MetricsOptions();
            var stats = new OpenCensusStats();

            var ep     = new MetricsEndpoint(opts, stats);
            var middle = new MetricsEndpointOwinMiddleware(null, ep);

            var context1 = CreateRequest("GET", "/metrics/Foo.Bar.Class", "foo=key:value");
            var result   = middle.ParseTags(context1.Request.Query);

            Assert.NotNull(result);
            Assert.Empty(result);

            var context2 = CreateRequest("GET", "/metrics/Foo.Bar.Class", "tag=key:value");

            result = middle.ParseTags(context2.Request.Query);
            Assert.NotNull(result);
            Assert.Contains(new KeyValuePair <string, string>("key", "value"), result);

            var context3 = CreateRequest("GET", "/metrics/Foo.Bar.Class", "tag=key:value&foo=key:value&tag=key1:value1");

            result = middle.ParseTags(context3.Request.Query);
            Assert.NotNull(result);
            Assert.Contains(new KeyValuePair <string, string>("key", "value"), result);
            Assert.Contains(new KeyValuePair <string, string>("key1", "value1"), result);
            Assert.Equal(2, result.Count);

            var context4 = CreateRequest("GET", "/metrics/Foo.Bar.Class", "tag=key:value&foo=key:value&tag=key:value");

            result = middle.ParseTags(context4.Request.Query);
            Assert.NotNull(result);
            Assert.Contains(new KeyValuePair <string, string>("key", "value"), result);
            Assert.Single(result);
        }
Exemple #2
0
        public async void HandleMetricsRequestAsync_GetSpecificExistingMetric_ReturnsExpected()
        {
            var opts          = new MetricsEndpointOptions();
            var mopts         = TestHelpers.GetManagementOptions(opts);
            var stats         = new OpenCensusStats();
            var tagsComponent = new TagsComponent();
            var tagger        = tagsComponent.Tagger;
            var ep            = new MetricsEndpoint(opts, stats);

            SetupTestView(stats);

            var middle = new MetricsEndpointOwinMiddleware(null, ep, mopts);

            var context = CreateRequest("GET", "/cloudfoundryapplication/metrics/test.test", "?tag=a:v1");

            await middle.HandleMetricsRequestAsync(context);

            Assert.Equal(200, context.Response.StatusCode);

            context.Response.Body.Seek(0, SeekOrigin.Begin);
            StreamReader rdr  = new StreamReader(context.Response.Body);
            string       json = await rdr.ReadToEndAsync();

            Assert.Equal("{\"name\":\"test.test\",\"measurements\":[{\"statistic\":\"TOTAL\",\"value\":45.0}],\"availableTags\":[{\"tag\":\"a\",\"values\":[\"v1\"]},{\"tag\":\"b\",\"values\":[\"v1\"]},{\"tag\":\"c\",\"values\":[\"v1\"]}]}", json);
        }
        public void ParseTag_ReturnsExpected()
        {
            var opts  = new MetricsOptions();
            var stats = new OpenCensusStats();

            var ep     = new MetricsEndpoint(opts, stats);
            var middle = new MetricsEndpointOwinMiddleware(null, ep);

            Assert.Null(middle.ParseTag("foobar"));
            Assert.Equal(new KeyValuePair <string, string>("foo", "bar"), middle.ParseTag("foo:bar"));
            Assert.Equal(new KeyValuePair <string, string>("foo", "bar:bar"), middle.ParseTag("foo:bar:bar"));
            Assert.Null(middle.ParseTag("foo,bar"));
        }
        public async void HandleMetricsRequestAsync_GetSpecificNonExistingMetric_ReturnsExpected()
        {
            var opts  = new MetricsOptions();
            var stats = new OpenCensusStats();

            var ep     = new MetricsEndpoint(opts, stats);
            var middle = new MetricsEndpointOwinMiddleware(null, ep);

            var context = CreateRequest("GET", "/metrics/foo.bar");

            await middle.HandleMetricsRequestAsync(context);

            Assert.Equal(404, context.Response.StatusCode);
        }
        public void MetricsEndpointMiddleware_PathAndVerbMatching_ReturnsExpected()
        {
            var opts   = new MetricsOptions();
            var stats  = new OpenCensusStats();
            var ep     = new MetricsEndpoint(opts, stats);
            var middle = new MetricsEndpointOwinMiddleware(null, ep);

            Assert.True(middle.RequestVerbAndPathMatch("GET", "/metrics"));
            Assert.False(middle.RequestVerbAndPathMatch("PUT", "/metrics"));
            Assert.False(middle.RequestVerbAndPathMatch("GET", "/badpath"));
            Assert.False(middle.RequestVerbAndPathMatch("POST", "/metrics"));
            Assert.False(middle.RequestVerbAndPathMatch("DELETE", "/metrics"));
            Assert.True(middle.RequestVerbAndPathMatch("GET", "/metrics/Foo.Bar.Class"));
            Assert.True(middle.RequestVerbAndPathMatch("GET", "/metrics/Foo.Bar.Class?tag=key:value&tag=key1:value1"));
            Assert.True(middle.RequestVerbAndPathMatch("GET", "/metrics?tag=key:value&tag=key1:value1"));
        }
        public async void HandleMetricsRequestAsync_GetMetricsNames_ReturnsExpected()
        {
            var opts  = new MetricsOptions();
            var stats = new OpenCensusStats();

            var ep     = new MetricsEndpoint(opts, stats);
            var middle = new MetricsEndpointOwinMiddleware(null, ep);

            var context = CreateRequest("GET", "/metrics");

            await middle.HandleMetricsRequestAsync(context);

            context.Response.Body.Seek(0, SeekOrigin.Begin);
            StreamReader rdr  = new StreamReader(context.Response.Body);
            string       json = await rdr.ReadToEndAsync();

            Assert.Equal("{\"names\":[]}", json);
        }
        public void GetMetricName_ReturnsExpected()
        {
            var opts  = new MetricsOptions();
            var stats = new OpenCensusStats();

            var ep     = new MetricsEndpoint(opts, stats);
            var middle = new MetricsEndpointOwinMiddleware(null, ep);

            var context1 = CreateRequest("GET", "/metrics");

            Assert.Null(middle.GetMetricName(context1.Request));

            var context2 = CreateRequest("GET", "/metrics/Foo.Bar.Class");

            Assert.Equal("Foo.Bar.Class", middle.GetMetricName(context2.Request));

            var context3 = CreateRequest("GET", "/metrics", "tag=key:value&tag=key1:value1");

            Assert.Null(middle.GetMetricName(context3.Request));
        }