Java Code Examples for io.opencensus.stats.View#create()
The following examples show how to use
io.opencensus.stats.View#create() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ViewManagerImplTest.java From opencensus-java with Apache License 2.0 | 6 votes |
@Test public void preventRegisteringDifferentViewWithSameName() { View view1 = View.create( VIEW_NAME, "View description.", MEASURE_DOUBLE, DISTRIBUTION, Arrays.asList(KEY), CUMULATIVE); View view2 = View.create( VIEW_NAME, "This is a different description.", MEASURE_DOUBLE, DISTRIBUTION, Arrays.asList(KEY), CUMULATIVE); testFailedToRegisterView( view1, view2, "A different view with the same name is already registered"); }
Example 2
Source File: ViewManagerImplTest.java From opencensus-java with Apache License 2.0 | 6 votes |
@Test @SuppressWarnings("deprecation") public void registerDifferentViewWithSameNameWithStatsDisabled() { statsComponent.setState(StatsCollectionState.DISABLED); View view1 = View.create( VIEW_NAME, "View description.", MEASURE_DOUBLE, DISTRIBUTION, Arrays.asList(KEY), CUMULATIVE); View view2 = View.create( VIEW_NAME, "This is a different description.", MEASURE_DOUBLE, DISTRIBUTION, Arrays.asList(KEY), CUMULATIVE); testFailedToRegisterView( view1, view2, "A different view with the same name is already registered"); }
Example 3
Source File: StatsRecorderImplTest.java From opencensus-java with Apache License 2.0 | 6 votes |
@Test public void record_CurrentContextSet() { View view = View.create( VIEW_NAME, "description", MEASURE_DOUBLE, Sum.create(), Arrays.asList(KEY), Cumulative.create()); viewManager.registerView(view); TagContext tags = new SimpleTagContext(Tag.create(KEY, VALUE)); Context orig = ContextUtils.withValue(Context.current(), tags).attach(); try { statsRecorder.newMeasureMap().put(MEASURE_DOUBLE, 1.0).record(); } finally { Context.current().detach(orig); } ViewData viewData = viewManager.getView(VIEW_NAME); // record() should have used the given TagContext. assertThat(viewData.getAggregationMap().keySet()).containsExactly(Arrays.asList(VALUE)); }
Example 4
Source File: StatsRecorderImplTest.java From opencensus-java with Apache License 2.0 | 6 votes |
@Test public void record_WithAttachments_DistributionNoHistogram() { testClock.setTime(START_TIME); View view = View.create( VIEW_NAME, "description", MEASURE_DOUBLE, DISTRIBUTION_NO_HISTOGRAM, Arrays.asList(KEY)); viewManager.registerView(view); recordWithAttachments(); ViewData viewData = viewManager.getView(VIEW_NAME); assertThat(viewData).isNotNull(); DistributionData distributionData = (DistributionData) viewData.getAggregationMap().get(Collections.singletonList(VALUE)); // Recording exemplar has no effect if there's no histogram. assertThat(distributionData.getExemplars()).isEmpty(); }
Example 5
Source File: StatsRecorderImplTest.java From opencensus-java with Apache License 2.0 | 6 votes |
@Test public void recordTwice() { View view = View.create( VIEW_NAME, "description", MEASURE_DOUBLE, Sum.create(), Arrays.asList(KEY), Cumulative.create()); viewManager.registerView(view); MeasureMap statsRecord = statsRecorder.newMeasureMap().put(MEASURE_DOUBLE, 1.0); statsRecord.record(new SimpleTagContext(Tag.create(KEY, VALUE))); statsRecord.record(new SimpleTagContext(Tag.create(KEY, VALUE_2))); ViewData viewData = viewManager.getView(VIEW_NAME); // There should be two entries. StatsTestUtil.assertAggregationMapEquals( viewData.getAggregationMap(), ImmutableMap.of( Arrays.asList(VALUE), StatsTestUtil.createAggregationData(Sum.create(), MEASURE_DOUBLE, 1.0), Arrays.asList(VALUE_2), StatsTestUtil.createAggregationData(Sum.create(), MEASURE_DOUBLE, 1.0)), 1e-6); }
Example 6
Source File: StatsRecorderImplTest.java From opencensus-java with Apache License 2.0 | 6 votes |
@Test public void record_MapDeprecatedRpcConstants() { View view = View.create( VIEW_NAME, "description", MEASURE_DOUBLE, Sum.create(), Arrays.asList(RecordUtils.RPC_METHOD)); viewManager.registerView(view); MeasureMap statsRecord = statsRecorder.newMeasureMap().put(MEASURE_DOUBLE, 1.0); statsRecord.record(new SimpleTagContext(Tag.create(RecordUtils.GRPC_CLIENT_METHOD, VALUE))); ViewData viewData = viewManager.getView(VIEW_NAME); // There should be two entries. StatsTestUtil.assertAggregationMapEquals( viewData.getAggregationMap(), ImmutableMap.of( Arrays.asList(VALUE), StatsTestUtil.createAggregationData(Sum.create(), MEASURE_DOUBLE, 1.0)), 1e-6); }
Example 7
Source File: ViewManagerImplTest.java From opencensus-java with Apache License 2.0 | 5 votes |
@Test public void preventRegisteringDifferentMeasureWithSameName() { MeasureDouble measure1 = MeasureDouble.create("measure", "description", "1"); MeasureLong measure2 = MeasureLong.create("measure", "description", "1"); View view1 = View.create( VIEW_NAME, VIEW_DESCRIPTION, measure1, DISTRIBUTION, Arrays.asList(KEY), CUMULATIVE); View view2 = View.create( VIEW_NAME_2, VIEW_DESCRIPTION, measure2, DISTRIBUTION, Arrays.asList(KEY), CUMULATIVE); testFailedToRegisterView( view1, view2, "A different measure with the same name is already registered"); }
Example 8
Source File: StatsRecorderImplTest.java From opencensus-java with Apache License 2.0 | 5 votes |
@Test public void record_UnregisteredMeasure() { View view = View.create( VIEW_NAME, "description", MEASURE_DOUBLE, Sum.create(), Arrays.asList(KEY), Cumulative.create()); viewManager.registerView(view); statsRecorder .newMeasureMap() .put(MEASURE_DOUBLE_NO_VIEW_1, 1.0) .put(MEASURE_DOUBLE, 2.0) .put(MEASURE_DOUBLE_NO_VIEW_2, 3.0) .record(new SimpleTagContext(Tag.create(KEY, VALUE))); ViewData viewData = viewManager.getView(VIEW_NAME); // There should be one entry. StatsTestUtil.assertAggregationMapEquals( viewData.getAggregationMap(), ImmutableMap.of( Arrays.asList(VALUE), StatsTestUtil.createAggregationData(Sum.create(), MEASURE_DOUBLE, 2.0)), 1e-6); }
Example 9
Source File: ViewManagerImplTest.java From opencensus-java with Apache License 2.0 | 5 votes |
@Test public void settingStateToDisabledWillClearStats_Interval() { View intervalView = View.create( VIEW_NAME_2, VIEW_DESCRIPTION, MEASURE_DOUBLE, MEAN, Arrays.asList(KEY), Interval.create(Duration.create(60, 0))); settingStateToDisabledWillClearStats(intervalView); }
Example 10
Source File: GoogleDtpInternalMetricRecorder.java From data-transfer-project with Apache License 2.0 | 5 votes |
private void setupView(Measure measure, TagKey... keys) { // Register the view. It is imperative that this step exists, // otherwise recorded metrics will be dropped and never exported. View view = View.create( View.Name.create(NAME_BASE + measure.getName()), measure.getDescription(), measure, Aggregation.Count.create(), ImmutableList.copyOf(keys)); viewManager.registerView(view); }
Example 11
Source File: Quickstart.java From java-docs-samples with Apache License 2.0 | 5 votes |
public static void main(String[] args) throws IOException, InterruptedException { // Register the view. It is imperative that this step exists, // otherwise recorded metrics will be dropped and never exported. View view = View.create( Name.create("task_latency_distribution"), "The distribution of the task latencies.", LATENCY_MS, Aggregation.Distribution.create(LATENCY_BOUNDARIES), Collections.emptyList()); ViewManager viewManager = Stats.getViewManager(); viewManager.registerView(view); // [START setup_exporter] // Enable OpenCensus exporters to export metrics to Stackdriver Monitoring. // Exporters use Application Default Credentials to authenticate. // See https://developers.google.com/identity/protocols/application-default-credentials // for more details. StackdriverStatsExporter.createAndRegister(); // [END setup_exporter] // Record 100 fake latency values between 0 and 5 seconds. Random rand = new Random(); for (int i = 0; i < 100; i++) { long ms = (long) (TimeUnit.MILLISECONDS.convert(5, TimeUnit.SECONDS) * rand.nextDouble()); System.out.println(String.format("Latency %d: %d", i, ms)); STATS_RECORDER.newMeasureMap().put(LATENCY_MS, ms).record(); } // The default export interval is 60 seconds. The thread with the StackdriverStatsExporter must // live for at least the interval past any metrics that must be collected, or some risk being // lost if they are recorded after the last export. System.out.println( String.format( "Sleeping %d seconds before shutdown to ensure all records are flushed.", EXPORT_INTERVAL)); Thread.sleep(TimeUnit.MILLISECONDS.convert(EXPORT_INTERVAL, TimeUnit.SECONDS)); }
Example 12
Source File: StatsRecorderImplTest.java From opencensus-java with Apache License 2.0 | 5 votes |
@Test @SuppressWarnings("deprecation") public void record_StatsReenabled() { View view = View.create( VIEW_NAME, "description", MEASURE_DOUBLE, Sum.create(), Arrays.asList(KEY), Cumulative.create()); viewManager.registerView(view); statsComponent.setState(StatsCollectionState.DISABLED); statsRecorder .newMeasureMap() .put(MEASURE_DOUBLE, 1.0) .record(new SimpleTagContext(Tag.create(KEY, VALUE))); assertThat(viewManager.getView(VIEW_NAME)).isEqualTo(createEmptyViewData(view)); statsComponent.setState(StatsCollectionState.ENABLED); assertThat(viewManager.getView(VIEW_NAME).getAggregationMap()).isEmpty(); assertThat(viewManager.getView(VIEW_NAME).getWindowData()) .isNotEqualTo(CumulativeData.create(ZERO_TIMESTAMP, ZERO_TIMESTAMP)); statsRecorder .newMeasureMap() .put(MEASURE_DOUBLE, 4.0) .record(new SimpleTagContext(Tag.create(KEY, VALUE))); StatsTestUtil.assertAggregationMapEquals( viewManager.getView(VIEW_NAME).getAggregationMap(), ImmutableMap.of( Arrays.asList(VALUE), StatsTestUtil.createAggregationData(Sum.create(), MEASURE_DOUBLE, 4.0)), 1e-6); }
Example 13
Source File: OpenCensusMetricExporterSpi.java From ignite with Apache License 2.0 | 4 votes |
/** */ private void addView(Measure msr) { View v = View.create(Name.create(msr.getName()), msr.getDescription(), msr, LastValue.create(), tags); Stats.getViewManager().registerView(v); }
Example 14
Source File: OpenCensusPluginSink.java From ffwd with Apache License 2.0 | 4 votes |
public void sendMetric(Metric metric) { try { String metricName = getOutputMetricName(metric); MeasureLong measure = measures.get(metricName); if (measure == null) { if (measures.size() > maxViews) { throw new RuntimeException("maxViews exceeded. " + "Please increase in configuration or decrease number of metrics."); } measure = MeasureLong.create("Events", "Number of Events", "1"); measures.put(metricName, measure); // Stackdriver expects each metric to have the same set of tags so metrics // missing tags will be rejected. NB by default stackdriver will create // the metricDescription based on the first metric received so be consistant // from the start. final List<TagKey> columns = new ArrayList<TagKey>(metric.getTags().size()); metric.getTags().keySet().forEach(tagName -> { columns.add(TagKey.create(sanitizeName(tagName))); }); final View view = View.create( Name.create(metricName), metricName, measure, Sum.create(), columns); Stats.getViewManager().registerView(view); } final TagContextBuilder builder = tagger.emptyBuilder(); metric.getTags().forEach((k, v) -> { builder.putPropagating(TagKey.create(sanitizeName(k)), TagValue.create(v)); }); final TagContext context = builder.build(); statsRecorder.newMeasureMap().put(measure, (long) metric.getValue()).record(context); } catch (Exception ex) { log.error("Couldn't send metric %s", ex); throw ex; } }
Example 15
Source File: ViewManagerImplTest.java From opencensus-java with Apache License 2.0 | 4 votes |
@Test public void testRecordIntervalMultipleTagValues() { // The interval is 10 seconds, i.e. values should expire after 10 seconds. View view = View.create( VIEW_NAME, VIEW_DESCRIPTION, MEASURE_DOUBLE, DISTRIBUTION, Arrays.asList(KEY), Interval.create(TEN_SECONDS)); clock.setTime(Timestamp.create(10, 0)); // Start at 10s viewManager.registerView(view); // record for TagValue1 at 11s clock.setTime(Timestamp.fromMillis(11 * MILLIS_PER_SECOND)); statsRecorder .newMeasureMap() .put(MEASURE_DOUBLE, 10.0) .record(tagger.emptyBuilder().put(KEY, VALUE).build()); // record for TagValue2 at 15s clock.setTime(Timestamp.fromMillis(15 * MILLIS_PER_SECOND)); statsRecorder .newMeasureMap() .put(MEASURE_DOUBLE, 30.0) .record(tagger.emptyBuilder().put(KEY, VALUE_2).build()); statsRecorder .newMeasureMap() .put(MEASURE_DOUBLE, 50.0) .record(tagger.emptyBuilder().put(KEY, VALUE_2).build()); // get ViewData at 19s, no stats should have expired. clock.setTime(Timestamp.fromMillis(19 * MILLIS_PER_SECOND)); ViewData viewData1 = viewManager.getView(VIEW_NAME); StatsTestUtil.assertAggregationMapEquals( viewData1.getAggregationMap(), ImmutableMap.of( Arrays.asList(VALUE), StatsTestUtil.createAggregationData(DISTRIBUTION, MEASURE_DOUBLE, 10.0), Arrays.asList(VALUE_2), StatsTestUtil.createAggregationData(DISTRIBUTION, MEASURE_DOUBLE, 30.0, 50.0)), EPSILON); // record for TagValue2 again at 20s clock.setTime(Timestamp.fromMillis(20 * MILLIS_PER_SECOND)); statsRecorder .newMeasureMap() .put(MEASURE_DOUBLE, 40.0) .record(tagger.emptyBuilder().put(KEY, VALUE_2).build()); // get ViewData at 25s, stats for TagValue1 should have expired. clock.setTime(Timestamp.fromMillis(25 * MILLIS_PER_SECOND)); ViewData viewData2 = viewManager.getView(VIEW_NAME); StatsTestUtil.assertAggregationMapEquals( viewData2.getAggregationMap(), ImmutableMap.of( Arrays.asList(VALUE_2), StatsTestUtil.createAggregationData(DISTRIBUTION, MEASURE_DOUBLE, 30.0, 50.0, 40.0)), EPSILON); // get ViewData at 30s, the first two values for TagValue2 should have expired. clock.setTime(Timestamp.fromMillis(30 * MILLIS_PER_SECOND)); ViewData viewData3 = viewManager.getView(VIEW_NAME); StatsTestUtil.assertAggregationMapEquals( viewData3.getAggregationMap(), ImmutableMap.of( Arrays.asList(VALUE_2), StatsTestUtil.createAggregationData(DISTRIBUTION, MEASURE_DOUBLE, 40.0)), EPSILON); // get ViewData at 40s, all stats should have expired. clock.setTime(Timestamp.fromMillis(40 * MILLIS_PER_SECOND)); ViewData viewData4 = viewManager.getView(VIEW_NAME); assertThat(viewData4.getAggregationMap()).isEmpty(); }
Example 16
Source File: ViewManagerImplTest.java From opencensus-java with Apache License 2.0 | 4 votes |
private static View createCumulativeView( View.Name name, Measure measure, Aggregation aggregation, List<TagKey> keys) { return View.create(name, VIEW_DESCRIPTION, measure, aggregation, keys, CUMULATIVE); }
Example 17
Source File: StatsBenchmarksUtil.java From opencensus-java with Apache License 2.0 | 4 votes |
static View createView(String name, Measure measure, Aggregation aggregation, TagKey... keys) { return View.create(View.Name.create(name), "", measure, aggregation, Arrays.asList(keys)); }
Example 18
Source File: StackdriverQuickstart.java From opencensus-java with Apache License 2.0 | 4 votes |
/** Main launcher for the Stackdriver example. */ public static void main(String[] args) throws IOException, InterruptedException { // Register the view. It is imperative that this step exists, // otherwise recorded metrics will be dropped and never exported. View view = View.create( Name.create("task_latency_distribution"), "The distribution of the task latencies.", LATENCY_MS, Aggregation.Distribution.create(LATENCY_BOUNDARIES), Collections.<TagKey>emptyList()); // Create the view manager ViewManager viewManager = Stats.getViewManager(); // Then finally register the views viewManager.registerView(view); // [START setup_exporter] // Enable OpenCensus exporters to export metrics to Stackdriver Monitoring. // Exporters use Application Default Credentials to authenticate. // See https://developers.google.com/identity/protocols/application-default-credentials // for more details. StackdriverStatsExporter.createAndRegister(); // [END setup_exporter] // Record 100 fake latency values between 0 and 5 seconds. Random rand = new Random(); for (int i = 0; i < 100; i++) { long ms = (long) (TimeUnit.MILLISECONDS.convert(5, TimeUnit.SECONDS) * rand.nextDouble()); System.out.println(String.format("Latency %d: %d", i, ms)); STATS_RECORDER.newMeasureMap().put(LATENCY_MS, ms).record(); } // The default export interval is 60 seconds. The thread with the StackdriverStatsExporter must // live for at least the interval past any metrics that must be collected, or some risk being // lost if they are recorded after the last export. System.out.println( String.format( "Sleeping %d seconds before shutdown to ensure all records are flushed.", EXPORT_INTERVAL)); Thread.sleep(TimeUnit.MILLISECONDS.convert(EXPORT_INTERVAL, TimeUnit.SECONDS)); }
Example 19
Source File: Repl.java From opencensus-java with Apache License 2.0 | 4 votes |
private static void registerAllViews() { // Defining the distribution aggregations Aggregation latencyDistribution = Distribution.create( BucketBoundaries.create( Arrays.asList( // [>=0ms, >=25ms, >=50ms, >=75ms, >=100ms, >=200ms, >=400ms, >=600ms, >=800ms, // >=1s, >=2s, >=4s, >=6s] 0.0, 25.0, 50.0, 75.0, 100.0, 200.0, 400.0, 600.0, 800.0, 1000.0, 2000.0, 4000.0, 6000.0))); Aggregation lengthsDistribution = Distribution.create( BucketBoundaries.create( Arrays.asList( // [>=0B, >=5B, >=10B, >=20B, >=40B, >=60B, >=80B, >=100B, >=200B, >=400B, // >=600B, // >=800B, >=1000B] 0.0, 5.0, 10.0, 20.0, 40.0, 60.0, 80.0, 100.0, 200.0, 400.0, 600.0, 800.0, 1000.0))); // Define the count aggregation Aggregation countAggregation = Aggregation.Count.create(); // So tagKeys List<TagKey> noKeys = new ArrayList<TagKey>(); // Define the views View[] views = new View[] { View.create( Name.create("ocjavametrics/latency"), "The distribution of latencies", M_LATENCY_MS, latencyDistribution, Collections.singletonList(KEY_METHOD)), View.create( Name.create("ocjavametrics/lines_in"), "The number of lines read in from standard input", M_LINES_IN, countAggregation, noKeys), View.create( Name.create("ocjavametrics/errors"), "The number of errors encountered", M_ERRORS, countAggregation, Collections.singletonList(KEY_METHOD)), View.create( Name.create("ocjavametrics/line_lengths"), "The distribution of line lengths", M_LINE_LENGTHS, lengthsDistribution, noKeys) }; // Create the view manager ViewManager vmgr = Stats.getViewManager(); // Then finally register the views for (View view : views) { vmgr.registerView(view); } }
Example 20
Source File: TelemetryUtils.java From meghanada-server with GNU General Public License v3.0 | 4 votes |
private static void registerAllViews() { Aggregation commandLatencyDistribution = Aggregation.Distribution.create( BucketBoundaries.create( Arrays.asList( 0.0, // >=0ms 25.0, // >=25ms 50.0, // >=50ms 100.0, // >=100ms 200.0, // >=200ms 400.0, // >=400ms 800.0, // >=800ms 1000.0, // >=1s 2000.0, // >=2s 5000.0, // >=5s 8000.0, // >=8s 10000.0 // >=10s ))); View[] views; views = new View[] { View.create( View.Name.create("meghanada/command_latency"), "The distribution of the command latencies", M_COMMAND_LATENCY_MS, commandLatencyDistribution, Collections.unmodifiableList(Arrays.asList(KEY_UID, KEY_COMMAND))), View.create( View.Name.create("meghanada/class_index_size"), "The number of class indexes", M_CLASS_INDEX, Aggregation.LastValue.create(), Collections.unmodifiableList(Collections.singletonList(KEY_UID))), View.create( View.Name.create("meghanada/autocomplete"), "The number of autocomplete count", M_AUTOCOMPLETE_COUNT, Aggregation.Sum.create(), Collections.unmodifiableList(Arrays.asList(KEY_UID, KEY_DESCRIPTION))), View.create( View.Name.create("meghanada/member_cache_hit_rate"), "The member cache hit rate", M_MEMBER_CACHE_HIT_RATE, Aggregation.LastValue.create(), Collections.unmodifiableList(Collections.singletonList(KEY_UID))), View.create( View.Name.create("meghanada/member_cache_load_exception_rate"), "The member cache load exception rate", M_MEMBER_CACHE_LOAD_ERROR_RATE, Aggregation.LastValue.create(), Collections.unmodifiableList(Collections.singletonList(KEY_UID))), View.create( View.Name.create("meghanada/member_cache_miss_rate"), "The member cache miss rate", M_MEMBER_CACHE_MISS_RATE, Aggregation.LastValue.create(), Collections.unmodifiableList(Collections.singletonList(KEY_UID))), View.create( View.Name.create("meghanada/vm_memory"), "The vm memory", M_MEMORY, Aggregation.LastValue.create(), Collections.unmodifiableList(Collections.singletonList(KEY_UID))), }; ViewManager vmgr = Stats.getViewManager(); for (View view : views) { vmgr.registerView(view); } }