• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2019, 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 the License 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  */
16 
17 package io.opencensus.examples.spring.servlet;
18 
19 import io.opencensus.contrib.http.util.HttpViews;
20 import io.opencensus.exporter.stats.prometheus.PrometheusStatsCollector;
21 import io.opencensus.exporter.trace.logging.LoggingTraceExporter;
22 import io.opencensus.trace.Tracing;
23 import io.opencensus.trace.config.TraceConfig;
24 import io.opencensus.trace.samplers.Samplers;
25 import io.prometheus.client.exporter.HTTPServer;
26 import java.io.IOException;
27 import org.springframework.boot.SpringApplication;
28 import org.springframework.boot.autoconfigure.SpringBootApplication;
29 import org.springframework.context.ApplicationContext;
30 
31 @SpringBootApplication
32 public class Application {
33 
initStatsExporter()34   private static void initStatsExporter() throws IOException {
35     HttpViews.registerAllServerViews();
36     HttpViews.registerAllClientViews();
37 
38     // Register Prometheus exporters and export metrics to a Prometheus HTTPServer.
39     // Refer to https://prometheus.io/ to run Prometheus Server.
40     PrometheusStatsCollector.createAndRegister();
41     HTTPServer prometheusServer = new HTTPServer(9090, true);
42   }
43 
initTracingAndLoggingExporter()44   private static void initTracingAndLoggingExporter() {
45     TraceConfig traceConfig = Tracing.getTraceConfig();
46     traceConfig.updateActiveTraceParams(
47         traceConfig.getActiveTraceParams().toBuilder().setSampler(Samplers.alwaysSample()).build());
48 
49     LoggingTraceExporter.register();
50   }
51 
52   /** Main launcher for the SpringServletApplication. */
main(String[] args)53   public static void main(String[] args) throws IOException {
54     ApplicationContext ctx = SpringApplication.run(Application.class, args);
55 
56     initTracingAndLoggingExporter();
57     initStatsExporter();
58   }
59 }
60