• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2020, 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.contrib.observability.ready.util;
18 
19 import static com.google.common.base.MoreObjects.firstNonNull;
20 
21 import io.opencensus.common.ExperimentalApi;
22 import io.opencensus.contrib.grpc.metrics.RpcViews;
23 import io.opencensus.exporter.metrics.ocagent.OcAgentMetricsExporter;
24 import io.opencensus.exporter.metrics.ocagent.OcAgentMetricsExporterConfiguration;
25 import io.opencensus.exporter.trace.ocagent.OcAgentTraceExporter;
26 import io.opencensus.exporter.trace.ocagent.OcAgentTraceExporterConfiguration;
27 import io.opencensus.trace.Tracing;
28 import io.opencensus.trace.config.TraceConfig;
29 import io.opencensus.trace.config.TraceParams;
30 import io.opencensus.trace.samplers.Samplers;
31 
32 /**
33  * Setup class to enable OpenCensus stats and metrics collections easily.
34  *
35  * @since 0.25
36  */
37 @ExperimentalApi
38 public final class BasicSetup {
39   private static final double DEAFULT_SAMPLING_RATE = 0.0001;
40   private static final String DEAFULT_ENDPOINT = "localhost:55678";
41   private static final String DEAFULT_SERVICE_NAME = "OpenCensus";
42 
BasicSetup()43   private BasicSetup() {}
44 
45   /**
46    * Enables OpenCensus metric and traces.
47    *
48    * <p>This will register all basic {@link io.opencensus.stats.View}s. When coupled with an agent,
49    * it allows users to monitor application behavior.
50    *
51    * <p>Example usage for maven:
52    *
53    * <pre>{@code
54    * <dependency>
55    *   <groupId>io.opencensus</groupId>
56    *   <artifactId>opencensus-contrib-observability-ready-util</artifactId>
57    *   <version>${opencensus.version}</version>
58    * </dependency>
59    * }</pre>
60    *
61    * <p>It is recommended to call this method before doing any RPC call to avoid missing stats.
62    *
63    * <pre>{@code
64    * BasicSetup.enableOpenCensus();
65    * }</pre>
66    *
67    * @param endPoint the end point of OC-Agent.
68    * @param probability the desired probability of sampling. Must be within [0.0, 1.0].
69    * @since 0.25
70    */
enableOpenCensus(String endPoint, double probability)71   public static void enableOpenCensus(String endPoint, double probability) {
72     // register basic rpc views
73     RpcViews.registerAllGrpcBasicViews();
74 
75     // set sampling rate
76     TraceConfig traceConfig = Tracing.getTraceConfig();
77     TraceParams activeTraceParams = traceConfig.getActiveTraceParams();
78     traceConfig.updateActiveTraceParams(
79         activeTraceParams.toBuilder().setSampler(Samplers.probabilitySampler(probability)).build());
80 
81     String serviceName = firstNonNull(System.getenv("SERVICE_NAME"), DEAFULT_SERVICE_NAME);
82     // create and register Trace Agent Exporter
83     OcAgentTraceExporter.createAndRegister(
84         OcAgentTraceExporterConfiguration.builder()
85             .setEndPoint(endPoint)
86             .setServiceName(serviceName)
87             .setUseInsecure(true)
88             .setEnableConfig(false)
89             .build());
90 
91     // create and register Metrics Agent Exporter
92     OcAgentMetricsExporter.createAndRegister(
93         OcAgentMetricsExporterConfiguration.builder()
94             .setEndPoint(endPoint)
95             .setServiceName(serviceName)
96             .setUseInsecure(true)
97             .build());
98   }
99 
100   /**
101    * Enables OpenCensus metric and traces with default endPoint and Sampling rate.
102    *
103    * @since 0.25
104    */
enableOpenCensus()105   public static void enableOpenCensus() {
106     enableOpenCensus(DEAFULT_ENDPOINT, DEAFULT_SAMPLING_RATE);
107   }
108 }
109