• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2018, 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.helloworld;
18 
19 import io.opencensus.common.Scope;
20 import io.opencensus.exporter.trace.logging.LoggingTraceExporter;
21 import io.opencensus.stats.Aggregation;
22 import io.opencensus.stats.BucketBoundaries;
23 import io.opencensus.stats.Measure.MeasureLong;
24 import io.opencensus.stats.Stats;
25 import io.opencensus.stats.StatsRecorder;
26 import io.opencensus.stats.View;
27 import io.opencensus.stats.ViewData;
28 import io.opencensus.stats.ViewManager;
29 import io.opencensus.tags.TagContextBuilder;
30 import io.opencensus.tags.TagKey;
31 import io.opencensus.tags.TagValue;
32 import io.opencensus.tags.Tagger;
33 import io.opencensus.tags.Tags;
34 import io.opencensus.trace.SpanBuilder;
35 import io.opencensus.trace.Status;
36 import io.opencensus.trace.Tracer;
37 import io.opencensus.trace.Tracing;
38 import io.opencensus.trace.samplers.Samplers;
39 import java.util.Arrays;
40 import java.util.Collections;
41 import java.util.Random;
42 import java.util.logging.Logger;
43 
44 /** Simple program that collects data for video size. */
45 public final class QuickStart {
46 
47   private static final Logger logger = Logger.getLogger(QuickStart.class.getName());
48 
49   private static final Tagger tagger = Tags.getTagger();
50   private static final ViewManager viewManager = Stats.getViewManager();
51   private static final StatsRecorder statsRecorder = Stats.getStatsRecorder();
52   private static final Tracer tracer = Tracing.getTracer();
53 
54   // frontendKey allows us to break down the recorded data.
55   private static final TagKey FRONTEND_KEY = TagKey.create("my.org/keys/frontend");
56 
57   // videoSize will measure the size of processed videos.
58   private static final MeasureLong VIDEO_SIZE =
59       MeasureLong.create("my.org/measure/video_size", "size of processed videos", "By");
60 
61   private static final long MiB = 1 << 20;
62 
63   // Create view to see the processed video size distribution broken down by frontend.
64   // The view has bucket boundaries (0, 16 * MiB, 65536 * MiB) that will group measure
65   // values into histogram buckets.
66   private static final View.Name VIDEO_SIZE_VIEW_NAME = View.Name.create("my.org/views/video_size");
67   private static final View VIDEO_SIZE_VIEW =
68       View.create(
69           VIDEO_SIZE_VIEW_NAME,
70           "processed video size over time",
71           VIDEO_SIZE,
72           Aggregation.Distribution.create(
73               BucketBoundaries.create(Arrays.asList(0.0, 16.0 * MiB, 256.0 * MiB))),
74           Collections.singletonList(FRONTEND_KEY));
75 
76   /** Main launcher for the QuickStart example. */
main(String[] args)77   public static void main(String[] args) throws InterruptedException {
78     TagContextBuilder tagContextBuilder =
79         tagger.currentBuilder().put(FRONTEND_KEY, TagValue.create("mobile-ios9.3.5"));
80     SpanBuilder spanBuilder =
81         tracer
82             .spanBuilder("my.org/ProcessVideo")
83             .setRecordEvents(true)
84             .setSampler(Samplers.alwaysSample());
85     viewManager.registerView(VIDEO_SIZE_VIEW);
86     LoggingTraceExporter.register();
87 
88     // Process video.
89     // Record the processed video size.
90     try (Scope scopedTags = tagContextBuilder.buildScoped();
91         Scope scopedSpan = spanBuilder.startScopedSpan()) {
92       tracer.getCurrentSpan().addAnnotation("Start processing video.");
93       // Sleep for [0,10] milliseconds to fake work.
94       Thread.sleep(new Random().nextInt(10) + 1);
95       statsRecorder.newMeasureMap().put(VIDEO_SIZE, 25 * MiB).record();
96       tracer.getCurrentSpan().addAnnotation("Finished processing video.");
97     } catch (Exception e) {
98       tracer.getCurrentSpan().addAnnotation("Exception thrown when processing video.");
99       tracer.getCurrentSpan().setStatus(Status.UNKNOWN);
100       logger.severe(e.getMessage());
101     }
102 
103     logger.info("Wait longer than the reporting duration...");
104     // Wait for a duration longer than reporting duration (5s) to ensure spans are exported.
105     // TODO(songya): remove the gap once we add a shutdown hook for exporting unflushed spans.
106     Thread.sleep(5100);
107     ViewData viewData = viewManager.getView(VIDEO_SIZE_VIEW_NAME);
108     logger.info(
109         String.format("Recorded stats for %s:\n %s", VIDEO_SIZE_VIEW_NAME.asString(), viewData));
110   }
111 }
112