diff options
author | Yang Song <songy23@users.noreply.github.com> | 2018-08-13 11:36:27 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-08-13 11:36:27 -0700 |
commit | 775816d699bd4f02788db5609a87386940e34aff (patch) | |
tree | 021d47fb4c67d0441a5861a9e4a69b8d168e572d /impl_core | |
parent | 66ae2eed952579e6982b05c0bcebe0670afb0afe (diff) | |
download | opencensus-java-775816d699bd4f02788db5609a87386940e34aff.tar.gz |
Metrics: Implement ExportComponent, make ProducerManager abstract. (#1347)
* Metrics: Implement ExportComponent, make ProducerManager abstract.
* Metrics: Add impl and impllite.
* Resolve two TODOs.
* Fix import path, leave a TODO for getMetricRegistry.
* Make NoopMetricProducerManager private. Add missing since tag.
Diffstat (limited to 'impl_core')
6 files changed, 323 insertions, 0 deletions
diff --git a/impl_core/src/main/java/io/opencensus/implcore/metrics/MetricsComponentImplBase.java b/impl_core/src/main/java/io/opencensus/implcore/metrics/MetricsComponentImplBase.java new file mode 100644 index 00000000..312b702e --- /dev/null +++ b/impl_core/src/main/java/io/opencensus/implcore/metrics/MetricsComponentImplBase.java @@ -0,0 +1,42 @@ +/* + * Copyright 2018, OpenCensus Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.opencensus.implcore.metrics; + +import io.opencensus.implcore.metrics.export.ExportComponentImpl; +import io.opencensus.metrics.MetricRegistry; +import io.opencensus.metrics.MetricsComponent; + +/** Implementation of {@link MetricsComponent}. */ +public class MetricsComponentImplBase extends MetricsComponent { + + private final ExportComponentImpl exportComponent; + + @Override + public ExportComponentImpl getExportComponent() { + return exportComponent; + } + + @Override + public MetricRegistry getMetricRegistry() { + // TODO: implement this method + throw new UnsupportedOperationException("not implemented"); + } + + public MetricsComponentImplBase() { + exportComponent = new ExportComponentImpl(); + } +} diff --git a/impl_core/src/main/java/io/opencensus/implcore/metrics/export/ExportComponentImpl.java b/impl_core/src/main/java/io/opencensus/implcore/metrics/export/ExportComponentImpl.java new file mode 100644 index 00000000..173c3aec --- /dev/null +++ b/impl_core/src/main/java/io/opencensus/implcore/metrics/export/ExportComponentImpl.java @@ -0,0 +1,31 @@ +/* + * Copyright 2018, OpenCensus Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.opencensus.implcore.metrics.export; + +import io.opencensus.metrics.export.ExportComponent; +import io.opencensus.metrics.export.MetricProducerManager; + +/** Implementation of {@link ExportComponent}. */ +public final class ExportComponentImpl extends ExportComponent { + + private final MetricProducerManager metricProducerManager = new MetricProducerManagerImpl(); + + @Override + public MetricProducerManager getMetricProducerManager() { + return metricProducerManager; + } +} diff --git a/impl_core/src/main/java/io/opencensus/implcore/metrics/export/MetricProducerManagerImpl.java b/impl_core/src/main/java/io/opencensus/implcore/metrics/export/MetricProducerManagerImpl.java new file mode 100644 index 00000000..fc9db5c1 --- /dev/null +++ b/impl_core/src/main/java/io/opencensus/implcore/metrics/export/MetricProducerManagerImpl.java @@ -0,0 +1,64 @@ +/* + * Copyright 2018, OpenCensus Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.opencensus.implcore.metrics.export; + +import com.google.common.base.Preconditions; +import io.opencensus.metrics.MetricProducer; +import io.opencensus.metrics.export.MetricProducerManager; +import java.util.Collections; +import java.util.LinkedHashSet; +import java.util.Set; +import javax.annotation.concurrent.ThreadSafe; + +/** Implementation of {@link MetricProducerManager}. */ +@ThreadSafe +public final class MetricProducerManagerImpl extends MetricProducerManager { + + private volatile Set<MetricProducer> metricProducers = + Collections.unmodifiableSet(new LinkedHashSet<MetricProducer>()); + + @Override + public synchronized void add(MetricProducer metricProducer) { + Preconditions.checkNotNull(metricProducer, "metricProducer"); + // Updating the set of MetricProducers happens under a lock to avoid multiple add or remove + // operations to happen in the same time. + Set<MetricProducer> newMetricProducers = new LinkedHashSet<MetricProducer>(metricProducers); + if (!newMetricProducers.add(metricProducer)) { + // The element already present, no need to update the current set of MetricProducers. + return; + } + metricProducers = Collections.unmodifiableSet(newMetricProducers); + } + + @Override + public synchronized void remove(MetricProducer metricProducer) { + Preconditions.checkNotNull(metricProducer, "metricProducer"); + // Updating the set of MetricProducers happens under a lock to avoid multiple add or remove + // operations to happen in the same time. + Set<MetricProducer> newMetricProducers = new LinkedHashSet<MetricProducer>(metricProducers); + if (!newMetricProducers.remove(metricProducer)) { + // The element not present, no need to update the current set of MetricProducers. + return; + } + metricProducers = Collections.unmodifiableSet(newMetricProducers); + } + + @Override + public Set<MetricProducer> getAllMetricProducer() { + return metricProducers; + } +} diff --git a/impl_core/src/test/java/io/opencensus/implcore/metrics/MetricsComponentImplBaseTest.java b/impl_core/src/test/java/io/opencensus/implcore/metrics/MetricsComponentImplBaseTest.java new file mode 100644 index 00000000..a86a53d9 --- /dev/null +++ b/impl_core/src/test/java/io/opencensus/implcore/metrics/MetricsComponentImplBaseTest.java @@ -0,0 +1,36 @@ +/* + * Copyright 2018, OpenCensus Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.opencensus.implcore.metrics; + +import static com.google.common.truth.Truth.assertThat; + +import io.opencensus.implcore.metrics.export.ExportComponentImpl; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Unit tests for {@link MetricsComponentImplBase}. */ +@RunWith(JUnit4.class) +public class MetricsComponentImplBaseTest { + + @Test + public void getExportComponent() { + MetricsComponentImplBase metricsComponentImplBase = new MetricsComponentImplBase(); + assertThat(metricsComponentImplBase.getExportComponent()) + .isInstanceOf(ExportComponentImpl.class); + } +} diff --git a/impl_core/src/test/java/io/opencensus/implcore/metrics/export/ExportComponentImplTest.java b/impl_core/src/test/java/io/opencensus/implcore/metrics/export/ExportComponentImplTest.java new file mode 100644 index 00000000..fb91641c --- /dev/null +++ b/impl_core/src/test/java/io/opencensus/implcore/metrics/export/ExportComponentImplTest.java @@ -0,0 +1,35 @@ +/* + * Copyright 2018, OpenCensus Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.opencensus.implcore.metrics.export; + +import static com.google.common.truth.Truth.assertThat; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Unit tests for {@link ExportComponentImpl}. */ +@RunWith(JUnit4.class) +public class ExportComponentImplTest { + + @Test + public void getMetricProducerManager() { + ExportComponentImpl exportComponent = new ExportComponentImpl(); + assertThat(exportComponent.getMetricProducerManager()) + .isInstanceOf(MetricProducerManagerImpl.class); + } +} diff --git a/impl_core/src/test/java/io/opencensus/implcore/metrics/export/MetricProducerManagerImplTest.java b/impl_core/src/test/java/io/opencensus/implcore/metrics/export/MetricProducerManagerImplTest.java new file mode 100644 index 00000000..7f2ec153 --- /dev/null +++ b/impl_core/src/test/java/io/opencensus/implcore/metrics/export/MetricProducerManagerImplTest.java @@ -0,0 +1,115 @@ +/* + * Copyright 2018, OpenCensus Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.opencensus.implcore.metrics.export; + +import static com.google.common.truth.Truth.assertThat; + +import io.opencensus.metrics.MetricProducer; +import io.opencensus.metrics.export.MetricProducerManager; +import java.util.Set; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +/** Unit tests for {@link MetricProducerManagerImpl}. */ +@RunWith(JUnit4.class) +public class MetricProducerManagerImplTest { + + private final MetricProducerManager metricProducerManager = new MetricProducerManagerImpl(); + @Mock private MetricProducer metricProducer; + @Mock private MetricProducer metricProducerOther; + + @Rule public final ExpectedException thrown = ExpectedException.none(); + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + } + + @Test + public void add_DisallowsNull() { + thrown.expect(NullPointerException.class); + metricProducerManager.add(null); + } + + @Test + public void add() { + metricProducerManager.add(metricProducer); + assertThat(metricProducerManager.getAllMetricProducer()).containsExactly(metricProducer); + } + + @Test + public void add_DuplicateElement() { + metricProducerManager.add(metricProducer); + Set<MetricProducer> metricProducerSet = metricProducerManager.getAllMetricProducer(); + assertThat(metricProducerSet).containsExactly(metricProducer); + metricProducerManager.add(metricProducer); + // Returns the same object. + assertThat(metricProducerManager.getAllMetricProducer()).isSameAs(metricProducerSet); + } + + @Test + public void add_MultipleElements() { + metricProducerManager.add(metricProducer); + Set<MetricProducer> metricProducerSet = metricProducerManager.getAllMetricProducer(); + assertThat(metricProducerSet).containsExactly(metricProducer); + metricProducerManager.add(metricProducerOther); + // Returns the same object. + assertThat(metricProducerManager.getAllMetricProducer()) + .containsExactly(metricProducer, metricProducerOther); + } + + @Test + public void addAndRemove() { + metricProducerManager.add(metricProducer); + assertThat(metricProducerManager.getAllMetricProducer()).containsExactly(metricProducer); + metricProducerManager.remove(metricProducer); + assertThat(metricProducerManager.getAllMetricProducer()).isEmpty(); + } + + @Test + public void remove_DisallowsNull() { + thrown.expect(NullPointerException.class); + metricProducerManager.remove(null); + } + + @Test + public void remove_FromEmpty() { + metricProducerManager.remove(metricProducer); + assertThat(metricProducerManager.getAllMetricProducer()).isEmpty(); + } + + @Test + public void remove_NotPresent() { + metricProducerManager.add(metricProducer); + Set<MetricProducer> metricProducerSet = metricProducerManager.getAllMetricProducer(); + assertThat(metricProducerSet).containsExactly(metricProducer); + metricProducerManager.remove(metricProducerOther); + // Returns the same object. + assertThat(metricProducerManager.getAllMetricProducer()).isSameAs(metricProducerSet); + } + + @Test + public void getAllMetricProducer_empty() { + assertThat(metricProducerManager.getAllMetricProducer()).isEmpty(); + } +} |