1 /* 2 * Copyright 2017, 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.zpages; 18 19 import io.opencensus.common.Scope; 20 import io.opencensus.contrib.grpc.metrics.RpcMeasureConstants; 21 import io.opencensus.contrib.grpc.metrics.RpcViews; 22 import io.opencensus.contrib.zpages.ZPageHandlers; 23 import io.opencensus.stats.MeasureMap; 24 import io.opencensus.stats.Stats; 25 import io.opencensus.stats.StatsRecorder; 26 import io.opencensus.tags.TagValue; 27 import io.opencensus.tags.Tagger; 28 import io.opencensus.tags.Tags; 29 import io.opencensus.trace.SpanBuilder; 30 import io.opencensus.trace.Tracer; 31 import io.opencensus.trace.Tracing; 32 import io.opencensus.trace.samplers.Samplers; 33 import java.util.Collections; 34 35 /** Testing only class for the UI. */ 36 public class ZPagesTester { 37 ZPagesTester()38 private ZPagesTester() {} 39 40 private static final Tagger tagger = Tags.getTagger(); 41 private static final Tracer tracer = Tracing.getTracer(); 42 private static final StatsRecorder statsRecorder = Stats.getStatsRecorder(); 43 44 private static final String SPAN_NAME = "ExampleSpan"; 45 private static final TagValue METHOD = TagValue.create("ExampleMethod"); 46 recordExampleData()47 private static void recordExampleData() throws InterruptedException { 48 Tracing.getExportComponent() 49 .getSampledSpanStore() 50 .registerSpanNamesForCollection(Collections.singletonList(SPAN_NAME)); 51 RpcViews.registerAllViews(); // Use old RPC constants to get interval stats. 52 SpanBuilder spanBuilder = 53 tracer.spanBuilder(SPAN_NAME).setRecordEvents(true).setSampler(Samplers.alwaysSample()); 54 55 try (Scope scope = spanBuilder.startScopedSpan()) { 56 tracer.getCurrentSpan().addAnnotation("Starts recording."); 57 MeasureMap measureMap = 58 statsRecorder 59 .newMeasureMap() 60 // Client measurements. 61 .put(RpcMeasureConstants.RPC_CLIENT_STARTED_COUNT, 1) 62 .put(RpcMeasureConstants.RPC_CLIENT_FINISHED_COUNT, 1) 63 .put(RpcMeasureConstants.RPC_CLIENT_ROUNDTRIP_LATENCY, 1.0) 64 .put(RpcMeasureConstants.RPC_CLIENT_REQUEST_COUNT, 1) 65 .put(RpcMeasureConstants.RPC_CLIENT_RESPONSE_COUNT, 1) 66 .put(RpcMeasureConstants.RPC_CLIENT_REQUEST_BYTES, 1e5) 67 .put(RpcMeasureConstants.RPC_CLIENT_RESPONSE_BYTES, 1e5) 68 .put(RpcMeasureConstants.RPC_CLIENT_UNCOMPRESSED_REQUEST_BYTES, 1e5) 69 .put(RpcMeasureConstants.RPC_CLIENT_UNCOMPRESSED_RESPONSE_BYTES, 1e5) 70 // Server measurements. 71 .put(RpcMeasureConstants.RPC_SERVER_STARTED_COUNT, 1) 72 .put(RpcMeasureConstants.RPC_SERVER_FINISHED_COUNT, 1) 73 .put(RpcMeasureConstants.RPC_SERVER_SERVER_LATENCY, 1.0) 74 .put(RpcMeasureConstants.RPC_SERVER_REQUEST_COUNT, 1) 75 .put(RpcMeasureConstants.RPC_SERVER_RESPONSE_COUNT, 1) 76 .put(RpcMeasureConstants.RPC_SERVER_REQUEST_BYTES, 1e5) 77 .put(RpcMeasureConstants.RPC_SERVER_RESPONSE_BYTES, 1e5) 78 .put(RpcMeasureConstants.RPC_SERVER_UNCOMPRESSED_REQUEST_BYTES, 1e5) 79 .put(RpcMeasureConstants.RPC_SERVER_UNCOMPRESSED_RESPONSE_BYTES, 1e5); 80 measureMap.record( 81 tagger 82 .currentBuilder() 83 .put(RpcMeasureConstants.RPC_STATUS, TagValue.create("OK")) 84 .put(RpcMeasureConstants.RPC_METHOD, METHOD) 85 .build()); 86 MeasureMap measureMapErrors = 87 statsRecorder 88 .newMeasureMap() 89 .put(RpcMeasureConstants.RPC_CLIENT_ERROR_COUNT, 1) 90 .put(RpcMeasureConstants.RPC_SERVER_ERROR_COUNT, 1); 91 measureMapErrors.record( 92 tagger 93 .currentBuilder() 94 .put(RpcMeasureConstants.RPC_STATUS, TagValue.create("UNKNOWN")) 95 .put(RpcMeasureConstants.RPC_METHOD, METHOD) 96 .build()); 97 98 Thread.sleep(200); // sleep for fake work. 99 tracer.getCurrentSpan().addAnnotation("Finish recording."); 100 } 101 } 102 103 /** Main method. */ main(String[] args)104 public static void main(String[] args) throws Exception { 105 ZPageHandlers.startHttpServerAndRegisterAll(8080); 106 recordExampleData(); 107 } 108 } 109