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.export; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import com.google.common.testing.EqualsTester; 22 import io.opencensus.common.Timestamp; 23 import io.opencensus.metrics.LabelKey; 24 import io.opencensus.metrics.LabelValue; 25 import io.opencensus.metrics.export.MetricDescriptor.Type; 26 import java.util.ArrayList; 27 import java.util.Arrays; 28 import java.util.Collections; 29 import java.util.List; 30 import org.junit.Rule; 31 import org.junit.Test; 32 import org.junit.rules.ExpectedException; 33 import org.junit.runner.RunWith; 34 import org.junit.runners.JUnit4; 35 36 /** Unit tests for {@link Metric}. */ 37 @RunWith(JUnit4.class) 38 public class MetricTest { 39 40 @Rule public final ExpectedException thrown = ExpectedException.none(); 41 42 private static final String METRIC_NAME_1 = "metric1"; 43 private static final String METRIC_NAME_2 = "metric2"; 44 private static final String DESCRIPTION = "Metric description."; 45 private static final String UNIT = "kb/s"; 46 private static final LabelKey KEY_1 = LabelKey.create("key1", "some key"); 47 private static final LabelKey KEY_2 = LabelKey.create("key1", "some other key"); 48 private static final MetricDescriptor METRIC_DESCRIPTOR_1 = 49 MetricDescriptor.create( 50 METRIC_NAME_1, DESCRIPTION, UNIT, Type.GAUGE_DOUBLE, Arrays.asList(KEY_1, KEY_2)); 51 private static final MetricDescriptor METRIC_DESCRIPTOR_2 = 52 MetricDescriptor.create( 53 METRIC_NAME_2, 54 DESCRIPTION, 55 UNIT, 56 Type.CUMULATIVE_INT64, 57 Collections.singletonList(KEY_1)); 58 private static final LabelValue LABEL_VALUE_1 = LabelValue.create("value1"); 59 private static final LabelValue LABEL_VALUE_2 = LabelValue.create("value1"); 60 private static final LabelValue LABEL_VALUE_EMPTY = LabelValue.create(""); 61 private static final Value VALUE_LONG = Value.longValue(12345678); 62 private static final Value VALUE_DOUBLE_1 = Value.doubleValue(-345.77); 63 private static final Value VALUE_DOUBLE_2 = Value.doubleValue(133.79); 64 private static final Timestamp TIMESTAMP_1 = Timestamp.fromMillis(1000); 65 private static final Timestamp TIMESTAMP_2 = Timestamp.fromMillis(2000); 66 private static final Timestamp TIMESTAMP_3 = Timestamp.fromMillis(3000); 67 private static final Point POINT_1 = Point.create(VALUE_DOUBLE_1, TIMESTAMP_2); 68 private static final Point POINT_2 = Point.create(VALUE_DOUBLE_2, TIMESTAMP_3); 69 private static final Point POINT_3 = Point.create(VALUE_LONG, TIMESTAMP_3); 70 private static final TimeSeries GAUGE_TIME_SERIES_1 = 71 TimeSeries.create( 72 Arrays.asList(LABEL_VALUE_1, LABEL_VALUE_2), Collections.singletonList(POINT_1), null); 73 private static final TimeSeries GAUGE_TIME_SERIES_2 = 74 TimeSeries.create( 75 Arrays.asList(LABEL_VALUE_1, LABEL_VALUE_2), Collections.singletonList(POINT_2), null); 76 private static final TimeSeries CUMULATIVE_TIME_SERIES = 77 TimeSeries.create( 78 Collections.singletonList(LABEL_VALUE_EMPTY), 79 Collections.singletonList(POINT_3), 80 TIMESTAMP_1); 81 82 @Test 83 public void testGet() { 84 Metric metric = 85 Metric.create(METRIC_DESCRIPTOR_1, Arrays.asList(GAUGE_TIME_SERIES_1, GAUGE_TIME_SERIES_2)); 86 assertThat(metric.getMetricDescriptor()).isEqualTo(METRIC_DESCRIPTOR_1); 87 assertThat(metric.getTimeSeriesList()) 88 .containsExactly(GAUGE_TIME_SERIES_1, GAUGE_TIME_SERIES_2) 89 .inOrder(); 90 } 91 92 @Test 93 public void typeMismatch_GaugeDouble_Long() { 94 typeMismatch( 95 METRIC_DESCRIPTOR_1, 96 Collections.singletonList(CUMULATIVE_TIME_SERIES), 97 String.format("Type mismatch: %s, %s.", Type.GAUGE_DOUBLE, "ValueLong")); 98 } 99 100 @Test 101 public void typeMismatch_CumulativeInt64_Double() { 102 typeMismatch( 103 METRIC_DESCRIPTOR_2, 104 Collections.singletonList(GAUGE_TIME_SERIES_1), 105 String.format("Type mismatch: %s, %s.", Type.CUMULATIVE_INT64, "ValueDouble")); 106 } 107 108 private void typeMismatch( 109 MetricDescriptor metricDescriptor, List<TimeSeries> timeSeriesList, String errorMessage) { 110 thrown.expect(IllegalArgumentException.class); 111 thrown.expectMessage(errorMessage); 112 Metric.create(metricDescriptor, timeSeriesList); 113 } 114 115 @Test 116 public void create_WithNullMetricDescriptor() { 117 thrown.expect(NullPointerException.class); 118 thrown.expectMessage("metricDescriptor"); 119 Metric.create(null, Collections.<TimeSeries>emptyList()); 120 } 121 122 @Test 123 public void create_WithNullTimeSeriesList() { 124 thrown.expect(NullPointerException.class); 125 thrown.expectMessage("timeSeriesList"); 126 Metric.create(METRIC_DESCRIPTOR_1, null); 127 } 128 129 @Test 130 public void create_WithNullTimeSeries() { 131 thrown.expect(NullPointerException.class); 132 thrown.expectMessage("timeSeries"); 133 Metric.create(METRIC_DESCRIPTOR_1, Arrays.asList(GAUGE_TIME_SERIES_1, null)); 134 } 135 136 @Test 137 public void immutableTimeSeriesList() { 138 List<TimeSeries> timeSeriesList = new ArrayList<TimeSeries>(); 139 timeSeriesList.add(GAUGE_TIME_SERIES_1); 140 Metric metric = Metric.create(METRIC_DESCRIPTOR_1, timeSeriesList); 141 timeSeriesList.add(GAUGE_TIME_SERIES_2); 142 assertThat(metric.getTimeSeriesList()).containsExactly(GAUGE_TIME_SERIES_1); 143 } 144 145 @Test 146 public void createWithOneTimeSeries_WithNullTimeSeries() { 147 thrown.expect(NullPointerException.class); 148 thrown.expectMessage("timeSeries"); 149 Metric.createWithOneTimeSeries(METRIC_DESCRIPTOR_1, null); 150 } 151 152 @Test 153 public void createWithOneTimeSeries_WithNullMetricDescriptor() { 154 thrown.expect(NullPointerException.class); 155 thrown.expectMessage("metricDescriptor"); 156 Metric.createWithOneTimeSeries(null, GAUGE_TIME_SERIES_1); 157 } 158 159 @Test 160 public void testGet_WithOneTimeSeries() { 161 Metric metric = Metric.createWithOneTimeSeries(METRIC_DESCRIPTOR_1, GAUGE_TIME_SERIES_1); 162 assertThat(metric.getMetricDescriptor()).isEqualTo(METRIC_DESCRIPTOR_1); 163 assertThat(metric.getTimeSeriesList()).containsExactly(GAUGE_TIME_SERIES_1); 164 } 165 166 @Test 167 public void testEquals() { 168 new EqualsTester() 169 .addEqualityGroup( 170 Metric.create( 171 METRIC_DESCRIPTOR_1, Arrays.asList(GAUGE_TIME_SERIES_1, GAUGE_TIME_SERIES_2)), 172 Metric.create( 173 METRIC_DESCRIPTOR_1, Arrays.asList(GAUGE_TIME_SERIES_1, GAUGE_TIME_SERIES_2))) 174 .addEqualityGroup(Metric.create(METRIC_DESCRIPTOR_1, Collections.<TimeSeries>emptyList())) 175 .addEqualityGroup( 176 Metric.createWithOneTimeSeries(METRIC_DESCRIPTOR_2, CUMULATIVE_TIME_SERIES)) 177 .addEqualityGroup(Metric.create(METRIC_DESCRIPTOR_2, Collections.<TimeSeries>emptyList())) 178 .testEquals(); 179 } 180 } 181