|
| 1 | +// <copyright file="MetricsHttpServer.cs" company="OpenCensus Authors"> |
| 2 | +// Copyright 2018, OpenCensus Authors |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of theLicense at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | +// </copyright> |
| 16 | + |
| 17 | +namespace OpenCensus.Exporter.Prometheus.Implementation |
| 18 | +{ |
| 19 | + using System; |
| 20 | + using System.Diagnostics; |
| 21 | + using System.IO; |
| 22 | + using System.Net; |
| 23 | + using System.Threading; |
| 24 | + using OpenCensus.Stats; |
| 25 | + using OpenCensus.Stats.Aggregations; |
| 26 | + |
| 27 | + internal class MetricsHttpServer |
| 28 | + { |
| 29 | + private readonly IViewManager viewManager; |
| 30 | + |
| 31 | + private readonly CancellationToken token; |
| 32 | + |
| 33 | + private readonly HttpListener httpListener = new HttpListener(); |
| 34 | + |
| 35 | + public MetricsHttpServer(IViewManager viewManager, PrometheusExporterOptions options, CancellationToken token) |
| 36 | + { |
| 37 | + this.viewManager = viewManager; |
| 38 | + this.token = token; |
| 39 | + this.httpListener.Prefixes.Add(options.Url.ToString()); |
| 40 | + } |
| 41 | + |
| 42 | + public void WorkerThread() |
| 43 | + { |
| 44 | + this.httpListener.Start(); |
| 45 | + |
| 46 | + try |
| 47 | + { |
| 48 | + while (!this.token.IsCancellationRequested) |
| 49 | + { |
| 50 | + var ctxTask = this.httpListener.GetContextAsync(); |
| 51 | + ctxTask.Wait(this.token); |
| 52 | + |
| 53 | + var ctx = ctxTask.Result; |
| 54 | + |
| 55 | + ctx.Response.StatusCode = 200; |
| 56 | + ctx.Response.ContentType = PrometheusMetricBuilder.ContentType; |
| 57 | + |
| 58 | + using (var output = ctx.Response.OutputStream) |
| 59 | + { |
| 60 | + using (var writer = new StreamWriter(output)) |
| 61 | + { |
| 62 | + foreach (var view in this.viewManager.AllExportedViews) |
| 63 | + { |
| 64 | + var data = this.viewManager.GetView(view.Name); |
| 65 | + |
| 66 | + var builder = new PrometheusMetricBuilder() |
| 67 | + .WithName(data.View.Name.AsString) |
| 68 | + .WithDescription(data.View.Description); |
| 69 | + |
| 70 | + builder = data.View.Aggregation.Match<PrometheusMetricBuilder>( |
| 71 | + (agg) => { return builder.WithType("gauge"); }, // Func<ISum, M> p0 |
| 72 | + (agg) => { return builder.WithType("counter"); }, // Func< ICount, M > p1, |
| 73 | + (agg) => { return builder.WithType("histogram"); }, // Func<IMean, M> p2, |
| 74 | + (agg) => { return builder.WithType("histogram"); }, // Func< IDistribution, M > p3, |
| 75 | + (agg) => { return builder.WithType("gauge"); }, // Func<ILastValue, M> p4, |
| 76 | + (agg) => { return builder.WithType("gauge"); }); // Func< IAggregation, M > p6); |
| 77 | + |
| 78 | + foreach (var value in data.AggregationMap) |
| 79 | + { |
| 80 | + var metricValueBuilder = builder.AddValue(); |
| 81 | + |
| 82 | + // TODO: This is not optimal. Need to refactor to split builder into separate functions |
| 83 | + metricValueBuilder = value.Value.Match<PrometheusMetricBuilder.PrometheusMetricValueBuilder>( |
| 84 | + metricValueBuilder.WithValue, |
| 85 | + metricValueBuilder.WithValue, |
| 86 | + metricValueBuilder.WithValue, |
| 87 | + metricValueBuilder.WithValue, |
| 88 | + metricValueBuilder.WithValue, |
| 89 | + metricValueBuilder.WithValue, |
| 90 | + metricValueBuilder.WithValue, |
| 91 | + metricValueBuilder.WithValue); |
| 92 | + |
| 93 | + for (int i = 0; i < value.Key.Values.Count; i++) |
| 94 | + { |
| 95 | + metricValueBuilder.WithLabel(data.View.Columns[i].Name, value.Key.Values[i].AsString); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + builder.Write(writer); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + catch (OperationCanceledException) |
| 106 | + { |
| 107 | + // this will happen when cancellation will be requested |
| 108 | + } |
| 109 | + catch (Exception) |
| 110 | + { |
| 111 | + // TODO: report error |
| 112 | + } |
| 113 | + finally |
| 114 | + { |
| 115 | + this.httpListener.Stop(); |
| 116 | + this.httpListener.Close(); |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments