Skip to content
This repository was archived by the owner on Dec 23, 2023. It is now read-only.

Commit 49bac50

Browse files
authored
Add observability ready util (#2002)
* Add observability ready util * add enableOpenCensus function fix format fix format fix format fix format fix format fix format * minor patch * minor patch * update README * fix code reviews * fetch SERVICE_NAME from env
1 parent 61a025e commit 49bac50

File tree

8 files changed

+199
-0
lines changed

8 files changed

+199
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
## Unreleased
22

3+
- Add an experimental artifact `opencensus-contrib-observability-ready-util`, that allows users to
4+
use OpenCensus easily.
5+
36
## 0.24.0 - 2019-08-27
47
- Remove `CONTEXT_SPAN_KEY` and `TAG_CONTEXT_KEY` from API. This will be a breaking change to those who
58
depend on these two keys, but anyone except gRPC shouldn't use it directly anyway.

all/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ def subprojects = [
1919
project(':opencensus-contrib-http-util'),
2020
project(':opencensus-contrib-log-correlation-log4j2'),
2121
project(':opencensus-contrib-log-correlation-stackdriver'),
22+
project(':opencensus-contrib-observability-ready-util'),
2223
project(':opencensus-contrib-resource-util'),
2324
project(':opencensus-contrib-spring'),
2425
project(':opencensus-contrib-spring-sleuth-v1x'),
@@ -54,6 +55,7 @@ def subprojects_javadoc = [
5455
project(':opencensus-contrib-http-util'),
5556
project(':opencensus-contrib-log-correlation-log4j2'),
5657
project(':opencensus-contrib-log-correlation-stackdriver'),
58+
project(':opencensus-contrib-observability-ready-util'),
5759
project(':opencensus-contrib-resource-util'),
5860
project(':opencensus-contrib-spring'),
5961
project(':opencensus-contrib-spring-sleuth-v1x'),

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,7 @@ subprojects {
445445
'opencensus-contrib-http-util',
446446
'opencensus-contrib-log-correlation-log4j2',
447447
'opencensus-contrib-log-correlation-stackdriver',
448+
'opencensus-contrib-observability-ready-util',
448449
'opencensus-contrib-resource-util',
449450
'opencensus-contrib-spring',
450451
'opencensus-contrib-spring-sleuth-v1x',

buildscripts/import-control.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,12 @@ General guidelines on imports:
173173
<allow pkg="io.opencensus.tags"/>
174174
<allow pkg="io.opencensus.trace"/>
175175
</subpackage>
176+
<subpackage name="observability.ready.util">
177+
<allow pkg="io.opencensus.contrib.grpc.metrics"/>
178+
<allow pkg="io.opencensus.trace"/>
179+
<allow pkg="io.opencensus.exporter.metrics.ocagent"/>
180+
<allow pkg="io.opencensus.exporter.trace.ocagent"/>
181+
</subpackage>
176182
<subpackage name="resource.util">
177183
<allow pkg="io.opencensus.contrib.resource.util"/>
178184
<allow pkg="io.opencensus.resource"/>
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# OpenCensus Observability Ready Util for Java
2+
3+
[![Build Status][travis-image]][travis-url]
4+
[![Windows Build Status][appveyor-image]][appveyor-url]
5+
6+
The *OpenCensus Observability Ready Util for Java* allows users to use OpenCensus easily.
7+
8+
It provides a wrapper that
9+
* Enables [Basic RPC views](https://github.com/census-instrumentation/opencensus-java/blob/2a17c8482ffb04540ea4ac0a5f746ad8d536c996/contrib/grpc_metrics/src/main/java/io/opencensus/contrib/grpc/metrics/RpcViews.java#L219)
10+
* Sets Probabilistic sampling rate to `0.0001`
11+
* Creates and Registers OCAgent Exporter to collect traces and metrics
12+
13+
## Quickstart
14+
15+
### Add the dependencies to your project
16+
17+
For Maven add to your `pom.xml`:
18+
```xml
19+
<dependencies>
20+
<dependency>
21+
<groupId>io.opencensus</groupId>
22+
<artifactId>opencensus-contrib-observability-ready-util</artifactId>
23+
<version>0.25.0</version>
24+
</dependency>
25+
</dependencies>
26+
```
27+
28+
For Gradle add to your dependencies:
29+
```groovy
30+
compile 'io.opencensus:opencensus-contrib-observability-ready-util:0.25.0'
31+
```
32+
33+
### Enable OpenCensus:
34+
35+
```java
36+
import io.opencensus.contrib.observability.ready.util.BasicSetup;
37+
38+
public class YourClass {
39+
public static void main(String[] args) {
40+
// It is recommended to call this method before doing any RPC call to avoid missing stats.
41+
BasicSetup.enableOpenCensus("with-service-name");
42+
}
43+
}
44+
```
45+
46+
> If Agent is not yet up and running, Exporter will just retry connection.
47+
48+
### And deploy OpenCensus Agent:
49+
50+
It will require you to deploy the [OpenCensus-Agent](https://github.com/census-instrumentation/opencensus-service#opencensus-agent) in order to export and examine the stats and traces.
51+
The OpenCensus Agent exporter aka “ocagent-exporter” enables your applications to send the
52+
observability that they’ve collected using OpenCensus to the OpenCensus Agent.
53+
54+
55+
[travis-image]: https://travis-ci.org/census-instrumentation/opencensus-java.svg?branch=master
56+
[travis-url]: https://travis-ci.org/census-instrumentation/opencensus-java
57+
[appveyor-image]: https://ci.appveyor.com/api/projects/status/hxthmpkxar4jq4be/branch/master?svg=true
58+
[appveyor-url]: https://ci.appveyor.com/project/opencensusjavateam/opencensus-java/branch/master
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
description = 'OpenCensus Observability Ready Util'
2+
3+
apply plugin: 'java'
4+
5+
[compileJava, compileTestJava].each() {
6+
it.sourceCompatibility = 1.8
7+
it.targetCompatibility = 1.8
8+
}
9+
10+
dependencies {
11+
compile project(':opencensus-api'),
12+
project(':opencensus-impl-core'),
13+
project(':opencensus-contrib-grpc-metrics'),
14+
project(':opencensus-exporter-metrics-ocagent'),
15+
project(':opencensus-exporter-trace-ocagent')
16+
17+
signature "org.codehaus.mojo.signature:java18:+@signature"
18+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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+
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+
*/
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+
*/
105+
public static void enableOpenCensus() {
106+
enableOpenCensus(DEAFULT_ENDPOINT, DEAFULT_SAMPLING_RATE);
107+
}
108+
}

settings.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ include ":opencensus-contrib-http-servlet"
3434
include ":opencensus-contrib-http-util"
3535
include ":opencensus-contrib-log-correlation-log4j2"
3636
include ":opencensus-contrib-log-correlation-stackdriver"
37+
include ":opencensus-contrib-observability-ready-util"
3738
include ":opencensus-contrib-resource-util"
3839
include ":opencensus-contrib-spring"
3940
include ":opencensus-contrib-spring-sleuth-v1x"
@@ -64,6 +65,8 @@ project(':opencensus-contrib-log-correlation-log4j2').projectDir =
6465
"$rootDir/contrib/log_correlation/log4j2" as File
6566
project(':opencensus-contrib-log-correlation-stackdriver').projectDir =
6667
"$rootDir/contrib/log_correlation/stackdriver" as File
68+
project(':opencensus-contrib-observability-ready-util').projectDir =
69+
"$rootDir/contrib/observability_ready_util" as File
6770
project(':opencensus-contrib-resource-util').projectDir = "$rootDir/contrib/resource_util" as File
6871
project(':opencensus-contrib-spring').projectDir = "$rootDir/contrib/spring" as File
6972
project(':opencensus-contrib-spring-sleuth-v1x').projectDir =

0 commit comments

Comments
 (0)