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

Latest commit

 

History

History
266 lines (221 loc) · 8.57 KB

File metadata and controls

266 lines (221 loc) · 8.57 KB
title Java
date 2018-07-25 09:38:03 -0700
draft false
class integration-page
aliases
/integrations/redis/java
logo /images/java-opencensus.png

Introduction

Some Redis clients were already instrumented to provide traces and metrics with OpenCensus

Packages Repository link
jedis https://github.com/opencensus-integrations/jedis

Prerequisites

You will need the following:

  • Redis
  • Google Stackdriver enabled on your project

{{% notice tip %}} For assistance installing Redis, please Click here to get started

For assistance setting up Stackdriver, Click here for a guided codelab. {{% /notice %}}

Generating the JAR

Clone this repository

git clone https://github.com/opencensus-integrations

Generate and install

Inside the cloned repository's directory run

mvn install:install-file -Dfile=$(pwd)/target/jedis-3.0.0-SNAPSHOT.jar \
-DgroupId=redis.clients -DartifactId=jedis -Dversion=3.0.0 \
-Dpackaging=jar -DgeneratePom=true

Enabling observability

To enable observability, we'll need to use Jedis normally but with one change

{{}} import redis.clients.jedis.Observability; {{}}

and then finally to enable metrics {{}} // Enable exporting of all the Jedis specific metrics and views Observability.registerAllViews(); {{}}

Available metrics

Metric search suffix Description
redis/bytes_read The number of bytes read from the Redis server
redis/bytes_written The number of bytes written out to the Redis server
redis/dials The number of connection dials made to the Redis server
redis/dial_latency_milliseconds The number of milliseconds spent performing Redis operations
redis/errors The number of errors encountered
redis/connections_opened The number of new connections
redis/roundtrip_latency The latency spent for various Redis operations
redis/reads The number of reads performed
redis/writes The number of writes performed

End to end example

{{}}

{{}} package io.opencensus.tutorials.jedis;

import io.opencensus.exporter.stats.stackdriver.StackdriverStatsConfiguration; import io.opencensus.exporter.stats.stackdriver.StackdriverStatsExporter; import io.opencensus.exporter.trace.stackdriver.StackdriverTraceConfiguration; import io.opencensus.exporter.trace.stackdriver.StackdriverTraceExporter; import io.opencensus.trace.Tracing; import io.opencensus.trace.config.TraceConfig; import io.opencensus.trace.config.TraceParams; import io.opencensus.trace.samplers.Samplers; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException;

import redis.clients.jedis.Jedis; import redis.clients.jedis.Observability;

public class JedisOpenCensus { private static final Jedis jedis = new Jedis("localhost");

public static void main(String ...args) {
    // Enable exporting of all the Jedis specific metrics and views.
    Observability.registerAllViews();

    // Now enable OpenCensus exporters
    setupOpenCensusExporters();

    // Now for the repl
    BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));

    while (true) {
        try {
            System.out.print("> ");
            System.out.flush();
            String query = stdin.readLine();

            // Check Redis if we've got a hit firstly
            String result = jedis.get(query);
            if (result == null || result == "") {
                // Cache miss so process it and memoize it
                result = "$" + query + "$";
                jedis.set(query, result);
            }
            System.out.println("< " + result + "\n");
        } catch (IOException e) {
            System.err.println("Exception "+ e);
        }
    }
}

private static void setupOpenCensusExporters() {
    String gcpProjectId = "census-demos";

    try {
        StackdriverTraceExporter.createAndRegister(
            StackdriverTraceConfiguration.builder()
            .setProjectId(gcpProjectId)
            .build());

        StackdriverStatsExporter.createAndRegister(
            StackdriverStatsConfiguration.builder()
            .setProjectId(gcpProjectId)
            .build());
    } catch (Exception e) {
        System.err.println("Failed to setup OpenCensus " + e);
    }

    // Change the sampling rate to always sample
    TraceConfig traceConfig = Tracing.getTraceConfig();
    traceConfig.updateActiveTraceParams(
            traceConfig.getActiveTraceParams().toBuilder().setSampler(Samplers.alwaysSample()).build());
}

} {{}}

{{}} 4.0.0 io.ocgrpc ocgrpc jar 1.0-SNAPSHOT ocgrpc http://maven.apache.org

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <opencensus.version>0.17.0</opencensus.version> <!-- The OpenCensus version to use -->
</properties>

<dependencies>
    <dependency>
        <groupId>io.opencensus</groupId>
        <artifactId>opencensus-exporter-stats-stackdriver</artifactId>
        <version>${opencensus.version}</version>
    </dependency>

    <dependency>
        <groupId>io.opencensus</groupId>
        <artifactId>opencensus-api</artifactId>
        <version>${opencensus.version}</version>
    </dependency>

    <dependency>
        <groupId>io.opencensus</groupId>
        <artifactId>opencensus-impl</artifactId>
        <version>${opencensus.version}</version>
        <scope>runtime</scope>
    </dependency>

    <dependency>
        <groupId>io.opencensus</groupId>
        <artifactId>opencensus-exporter-trace-stackdriver</artifactId>
        <version>${opencensus.version}</version>
    </dependency>

    <dependency>
        <groupId>redis.clients</groupId>
        <artifactId>jedis</artifactId>
        <version>3.0.0</version>
    </dependency>

</dependencies>

<build>
    <extensions>
        <extension>
            <groupId>kr.motd.maven</groupId>
            <artifactId>os-maven-plugin</artifactId>
            <version>1.5.0.Final</version>
        </extension>
    </extensions>

    <pluginManagement>
      <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.7.0</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
      </plugins>
    </pluginManagement>

    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>appassembler-maven-plugin</artifactId>
            <version>1.10</version>
            <configuration>
                <programs>
                    <program>
                        <id>JedisOpenCensus</id>
                        <mainClass>io.opencensus.tutorials.jedis.JedisOpenCensus</mainClass>
                    </program>
                </programs>
            </configuration>
        </plugin>

    </plugins>
</build>
{{}} {{}}

Running it

mvn install && mvn exec:java -Dexec.mainClass=io.opencensus.tutorials.jedis.JedisOpenCensus

Viewing your metrics

Please visit https://console.cloud.google.com/monitoring

Viewing your traces

Please visit https://console.cloud.google.com/traces/traces