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.metrics; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import java.util.Collections; 22 import java.util.List; 23 import java.util.Map; 24 import org.junit.Rule; 25 import org.junit.Test; 26 import org.junit.rules.ExpectedException; 27 import org.junit.runner.RunWith; 28 import org.junit.runners.JUnit4; 29 30 /** Unit tests for {@link MetricRegistry}. */ 31 @RunWith(JUnit4.class) 32 public class MetricRegistryTest { 33 @Rule public ExpectedException thrown = ExpectedException.none(); 34 35 private static final String NAME = "test_name"; 36 private static final String NAME_2 = "test_name2"; 37 private static final String NAME_3 = "test_name3"; 38 private static final String NAME_4 = "test_name4"; 39 private static final String DESCRIPTION = "test_description"; 40 private static final String UNIT = "1"; 41 private static final LabelKey LABEL_KEY = LabelKey.create("test_key", "test key description"); 42 private static final List<LabelKey> LABEL_KEYS = Collections.singletonList(LABEL_KEY); 43 private static final LabelValue LABEL_VALUE = LabelValue.create("test_value"); 44 private static final LabelValue LABEL_VALUE_2 = LabelValue.create("test_value_2"); 45 private static final List<LabelValue> LABEL_VALUES = Collections.singletonList(LABEL_VALUE); 46 private static final Map<LabelKey, LabelValue> CONSTANT_LABELS = 47 Collections.singletonMap( 48 LabelKey.create("test_key_1", "test key description"), LABEL_VALUE_2); 49 private static final MetricOptions METRIC_OPTIONS = 50 MetricOptions.builder() 51 .setDescription(DESCRIPTION) 52 .setUnit(UNIT) 53 .setLabelKeys(LABEL_KEYS) 54 .setConstantLabels(CONSTANT_LABELS) 55 .build(); 56 private final MetricRegistry metricRegistry = 57 MetricsComponent.newNoopMetricsComponent().getMetricRegistry(); 58 59 @Test noopAddLongGauge_NullName()60 public void noopAddLongGauge_NullName() { 61 thrown.expect(NullPointerException.class); 62 thrown.expectMessage("name"); 63 metricRegistry.addLongGauge(null, METRIC_OPTIONS); 64 } 65 66 @Test noopAddDoubleGauge_NullName()67 public void noopAddDoubleGauge_NullName() { 68 thrown.expect(NullPointerException.class); 69 thrown.expectMessage("name"); 70 metricRegistry.addDoubleGauge(null, METRIC_OPTIONS); 71 } 72 73 @Test noopAddDerivedLongGauge_NullName()74 public void noopAddDerivedLongGauge_NullName() { 75 thrown.expect(NullPointerException.class); 76 thrown.expectMessage("name"); 77 metricRegistry.addDerivedLongGauge(null, METRIC_OPTIONS); 78 } 79 80 @Test noopAddDerivedDoubleGauge_NullName()81 public void noopAddDerivedDoubleGauge_NullName() { 82 thrown.expect(NullPointerException.class); 83 thrown.expectMessage("name"); 84 metricRegistry.addDerivedDoubleGauge(null, METRIC_OPTIONS); 85 } 86 87 @Test noopAddLongCumulative_NullName()88 public void noopAddLongCumulative_NullName() { 89 thrown.expect(NullPointerException.class); 90 thrown.expectMessage("name"); 91 metricRegistry.addLongCumulative(null, METRIC_OPTIONS); 92 } 93 94 @Test noopAddDoubleCumulative_NullName()95 public void noopAddDoubleCumulative_NullName() { 96 thrown.expect(NullPointerException.class); 97 thrown.expectMessage("name"); 98 metricRegistry.addDoubleCumulative(null, METRIC_OPTIONS); 99 } 100 101 @Test noopAddDerivedLongCumulative_NullName()102 public void noopAddDerivedLongCumulative_NullName() { 103 thrown.expect(NullPointerException.class); 104 thrown.expectMessage("name"); 105 metricRegistry.addDerivedLongCumulative(null, METRIC_OPTIONS); 106 } 107 108 @Test noopAddDerivedDoubleCumulative_NullName()109 public void noopAddDerivedDoubleCumulative_NullName() { 110 thrown.expect(NullPointerException.class); 111 thrown.expectMessage("name"); 112 metricRegistry.addDerivedDoubleCumulative(null, METRIC_OPTIONS); 113 } 114 115 @Test noopSameAs()116 public void noopSameAs() { 117 LongGauge longGauge = metricRegistry.addLongGauge(NAME, METRIC_OPTIONS); 118 assertThat(longGauge.getDefaultTimeSeries()).isSameInstanceAs(longGauge.getDefaultTimeSeries()); 119 assertThat(longGauge.getDefaultTimeSeries()) 120 .isSameInstanceAs(longGauge.getOrCreateTimeSeries(LABEL_VALUES)); 121 122 DoubleGauge doubleGauge = metricRegistry.addDoubleGauge(NAME_2, METRIC_OPTIONS); 123 assertThat(doubleGauge.getDefaultTimeSeries()) 124 .isSameInstanceAs(doubleGauge.getDefaultTimeSeries()); 125 assertThat(doubleGauge.getDefaultTimeSeries()) 126 .isSameInstanceAs(doubleGauge.getOrCreateTimeSeries(LABEL_VALUES)); 127 128 LongCumulative longCumulative = metricRegistry.addLongCumulative(NAME, METRIC_OPTIONS); 129 assertThat(longCumulative.getDefaultTimeSeries()) 130 .isSameInstanceAs(longCumulative.getDefaultTimeSeries()); 131 assertThat(longCumulative.getDefaultTimeSeries()) 132 .isSameInstanceAs(longCumulative.getOrCreateTimeSeries(LABEL_VALUES)); 133 134 DoubleCumulative doubleCumulative = metricRegistry.addDoubleCumulative(NAME_2, METRIC_OPTIONS); 135 assertThat(doubleCumulative.getDefaultTimeSeries()) 136 .isSameInstanceAs(doubleCumulative.getDefaultTimeSeries()); 137 assertThat(doubleCumulative.getDefaultTimeSeries()) 138 .isSameInstanceAs(doubleCumulative.getOrCreateTimeSeries(LABEL_VALUES)); 139 } 140 141 @Test noopInstanceOf()142 public void noopInstanceOf() { 143 assertThat(metricRegistry.addLongGauge(NAME, METRIC_OPTIONS)) 144 .isInstanceOf(LongGauge.newNoopLongGauge(NAME, DESCRIPTION, UNIT, LABEL_KEYS).getClass()); 145 assertThat(metricRegistry.addDoubleGauge(NAME_2, METRIC_OPTIONS)) 146 .isInstanceOf( 147 DoubleGauge.newNoopDoubleGauge(NAME_2, DESCRIPTION, UNIT, LABEL_KEYS).getClass()); 148 assertThat(metricRegistry.addDerivedLongGauge(NAME_3, METRIC_OPTIONS)) 149 .isInstanceOf( 150 DerivedLongGauge.newNoopDerivedLongGauge(NAME_3, DESCRIPTION, UNIT, LABEL_KEYS) 151 .getClass()); 152 assertThat(metricRegistry.addDerivedDoubleGauge(NAME_4, METRIC_OPTIONS)) 153 .isInstanceOf( 154 DerivedDoubleGauge.newNoopDerivedDoubleGauge(NAME_4, DESCRIPTION, UNIT, LABEL_KEYS) 155 .getClass()); 156 157 assertThat(metricRegistry.addLongCumulative(NAME, METRIC_OPTIONS)) 158 .isInstanceOf( 159 LongCumulative.newNoopLongCumulative(NAME, DESCRIPTION, UNIT, LABEL_KEYS).getClass()); 160 assertThat(metricRegistry.addDoubleCumulative(NAME_2, METRIC_OPTIONS)) 161 .isInstanceOf( 162 DoubleCumulative.newNoopDoubleCumulative(NAME_2, DESCRIPTION, UNIT, LABEL_KEYS) 163 .getClass()); 164 assertThat(metricRegistry.addDerivedLongCumulative(NAME_3, METRIC_OPTIONS)) 165 .isInstanceOf( 166 DerivedLongCumulative.newNoopDerivedLongCumulative( 167 NAME_3, DESCRIPTION, UNIT, LABEL_KEYS) 168 .getClass()); 169 assertThat(metricRegistry.addDerivedDoubleCumulative(NAME_4, METRIC_OPTIONS)) 170 .isInstanceOf( 171 DerivedDoubleCumulative.newNoopDerivedDoubleCumulative( 172 NAME_4, DESCRIPTION, UNIT, LABEL_KEYS) 173 .getClass()); 174 } 175 } 176