aboutsummaryrefslogtreecommitdiff
path: root/examples/java/com/google/census/CensusRunner.java
blob: 6b8a51ff804bb030706e6f0084de008172c53630 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
 * Copyright 2016, Google Inc.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.census;

import io.grpc.Context;

/**
 * Simple program that uses Census contexts.
 */
public class CensusRunner {
  private static final TagKey K1 = new TagKey("k1");
  private static final TagKey K2 = new TagKey("k2");
  private static final TagKey K3 = new TagKey("k3");
  private static final TagKey K4 = new TagKey("k4");

  private static final TagValue V1 = new TagValue("v1");
  private static final TagValue V2 = new TagValue("v2");
  private static final TagValue V3 = new TagValue("v3");
  private static final TagValue V4 = new TagValue("v4");

  private static final MetricName M1 = new MetricName("m1");
  private static final MetricName M2 = new MetricName("m2");

  public static void main(String[] args) {
    System.out.println("Hello Census World");
    System.out.println("Default Tags: " + DEFAULT);
    System.out.println("Current Tags: " + getCurrentCensusContext());
    Context context1 = withCurrent(DEFAULT.with(K1, V1, K2, V2));
    Context original = context1.attach();
    try {
        System.out.println("  Current Tags: " + getCurrentCensusContext());
        System.out.println("  Current == Default + tags1: "
            + getCurrentCensusContext().equals(getCensusContext(context1)));
        Context context2 = withCurrent(getCurrentCensusContext().with(K3, V3, K4, V4));
        context2.attach();
        try {
          System.out.println("    Current Tags: " + getCurrentCensusContext());
          System.out.println("    Current == Default + tags1 + tags2: "
              + getCurrentCensusContext().equals(getCensusContext(context2)));
          getCurrentCensusContext().record(MetricMap.of(M1, 0.2, M2, 0.4));
        } finally {
          context2.detach(context1);
        }
    } finally {
      context1.detach(original);
    }
    System.out.println("Current == Default: "
        + getCurrentCensusContext().equals(DEFAULT));
  }

  private static final CensusContext DEFAULT = Census.getCensusContextFactory().getDefault();

  private static final CensusContext getCurrentCensusContext() {
    return getCensusContext(Context.current());
  }

  private static final CensusContext getCensusContext(Context context) {
    return CensusGrpcContext.getInstance().get(context);
  }

  private static final Context withCurrent(CensusContext context) {
    return CensusGrpcContext.getInstance().withCensusContext(Context.current(), context);
  }
}