Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add initial implementation for Observers #818

Merged
merged 2 commits into from
Feb 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 3 additions & 18 deletions sdk/src/main/java/io/opentelemetry/sdk/metrics/BaseInstrument.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@

package io.opentelemetry.sdk.metrics;

import io.opentelemetry.metrics.InstrumentWithBinding;
import io.opentelemetry.metrics.LabelSet;
import io.opentelemetry.metrics.Instrument;
import java.util.List;
import java.util.Map;

// TODO: add a BaseInstrument and a BaseInstrumentWithBounds.
abstract class BaseInstrument<B> implements InstrumentWithBinding<B> {
abstract class BaseInstrument implements Instrument {

private final String name;
private final String description;
Expand All @@ -37,19 +35,6 @@ abstract class BaseInstrument<B> implements InstrumentWithBinding<B> {
this.labelKeys = labelKeys;
}

abstract B createBoundInstrument(LabelSet labelSet);

@Override
public B bind(LabelSet labelSet) {
return createBoundInstrument(labelSet);
// todo: associate with an aggregator/accumulator
}

@Override
public void unbind(B boundInstrument) {
// todo: pass through to an aggregator/accumulator
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand All @@ -59,7 +44,7 @@ public boolean equals(Object o) {
return false;
}

BaseInstrument<?> that = (BaseInstrument<?>) o;
BaseInstrument that = (BaseInstrument) o;

if (name != null ? !name.equals(that.name) : that.name != null) {
return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2020, OpenTelemetry 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.opentelemetry.sdk.metrics;

import io.opentelemetry.metrics.InstrumentWithBinding;
import io.opentelemetry.metrics.LabelSet;
import java.util.List;
import java.util.Map;

abstract class BaseInstrumentWithBinding<B> extends BaseInstrument
implements InstrumentWithBinding<B> {

BaseInstrumentWithBinding(
String name, String description, Map<String, String> constantLabels, List<String> labelKeys) {
super(name, description, constantLabels, labelKeys);
}

abstract B createBoundInstrument(LabelSet labelSet);

@Override
public B bind(LabelSet labelSet) {
return createBoundInstrument(labelSet);
// todo: associate with an aggregator/accumulator
}

@Override
public void unbind(B boundInstrument) {
// todo: pass through to an aggregator/accumulator
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
import java.util.List;
import java.util.Map;

final class DoubleCounterSdk extends BaseInstrument<BoundDoubleCounter> implements DoubleCounter {
final class DoubleCounterSdk extends BaseInstrumentWithBinding<BoundDoubleCounter>
implements DoubleCounter {

private final boolean monotonic;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
import java.util.List;
import java.util.Map;

final class DoubleMeasureSdk extends BaseInstrument<BoundDoubleMeasure> implements DoubleMeasure {
final class DoubleMeasureSdk extends BaseInstrumentWithBinding<BoundDoubleMeasure>
implements DoubleMeasure {

private final boolean absolute;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright 2020, OpenTelemetry 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.opentelemetry.sdk.metrics;

import io.opentelemetry.metrics.DoubleObserver;
import java.util.List;
import java.util.Map;

final class DoubleObserverSdk extends BaseInstrument implements DoubleObserver {
private final boolean monotonic;

DoubleObserverSdk(
String name,
String description,
Map<String, String> constantLabels,
List<String> labelKeys,
boolean monotonic) {
super(name, description, constantLabels, labelKeys);
this.monotonic = monotonic;
}

@Override
public void setCallback(Callback<DoubleObserver.ResultDoubleObserver> metricUpdater) {
throw new UnsupportedOperationException("to be implemented");
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof DoubleObserverSdk)) {
return false;
}
if (!super.equals(o)) {
return false;
}

DoubleObserverSdk that = (DoubleObserverSdk) o;

return monotonic == that.monotonic;
}

@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + (monotonic ? 1 : 0);
return result;
}

static DoubleObserver.Builder builder(String name) {
return new Builder(name);
}

private static final class Builder
extends AbstractObserverBuilder<DoubleObserver.Builder, DoubleObserver>
implements DoubleObserver.Builder {

private Builder(String name) {
super(name);
}

@Override
Builder getThis() {
return this;
}

@Override
public DoubleObserver build() {
return new DoubleObserverSdk(
getName(), getDescription(), getConstantLabels(), getLabelKeys(), isMonotonic());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
import java.util.List;
import java.util.Map;

final class LongCounterSdk extends BaseInstrument<BoundLongCounter> implements LongCounter {
final class LongCounterSdk extends BaseInstrumentWithBinding<BoundLongCounter>
implements LongCounter {

private final boolean monotonic;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
import java.util.List;
import java.util.Map;

final class LongMeasureSdk extends BaseInstrument<BoundLongMeasure> implements LongMeasure {
final class LongMeasureSdk extends BaseInstrumentWithBinding<BoundLongMeasure>
implements LongMeasure {

private final boolean absolute;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright 2020, OpenTelemetry 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.opentelemetry.sdk.metrics;

import io.opentelemetry.metrics.LongObserver;
import java.util.List;
import java.util.Map;

final class LongObserverSdk extends BaseInstrument implements LongObserver {
private final boolean monotonic;

LongObserverSdk(
String name,
String description,
Map<String, String> constantLabels,
List<String> labelKeys,
boolean monotonic) {
super(name, description, constantLabels, labelKeys);
this.monotonic = monotonic;
}

@Override
public void setCallback(Callback<LongObserver.ResultLongObserver> metricUpdater) {
throw new UnsupportedOperationException("to be implemented");
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof LongObserverSdk)) {
return false;
}
if (!super.equals(o)) {
return false;
}

LongObserverSdk that = (LongObserverSdk) o;

return monotonic == that.monotonic;
}

@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + (monotonic ? 1 : 0);
return result;
}

static LongObserver.Builder builder(String name) {
return new Builder(name);
}

private static final class Builder
extends AbstractObserverBuilder<LongObserver.Builder, LongObserver>
implements LongObserver.Builder {

private Builder(String name) {
super(name);
}

@Override
Builder getThis() {
return this;
}

@Override
public LongObserver build() {
return new LongObserverSdk(
getName(), getDescription(), getConstantLabels(), getLabelKeys(), isMonotonic());
}
}
}
4 changes: 2 additions & 2 deletions sdk/src/main/java/io/opentelemetry/sdk/metrics/MeterSdk.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,12 @@ public LongMeasure.Builder longMeasureBuilder(String name) {

@Override
public DoubleObserver.Builder doubleObserverBuilder(String name) {
throw new UnsupportedOperationException("to be implemented");
return DoubleObserverSdk.builder(name);
}

@Override
public LongObserver.Builder longObserverBuilder(String name) {
throw new UnsupportedOperationException("to be implemented");
return LongObserverSdk.builder(name);
}

@Override
Expand Down
36 changes: 36 additions & 0 deletions sdk/src/test/java/io/opentelemetry/sdk/metrics/MeterSdkTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
import com.google.common.collect.ImmutableMap;
import io.opentelemetry.metrics.DoubleCounter;
import io.opentelemetry.metrics.DoubleMeasure;
import io.opentelemetry.metrics.DoubleObserver;
import io.opentelemetry.metrics.LongCounter;
import io.opentelemetry.metrics.LongMeasure;
import io.opentelemetry.metrics.LongObserver;
import java.util.Collections;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -68,6 +70,23 @@ public void testLongMeasure() {
// todo: verify that the MeterSdk has kept track of what has been created, once that's in place
}

@Test
public void testLongObserver() {
LongObserver longObserver =
testSdk
.longObserverBuilder("testCounter")
.setConstantLabels(ImmutableMap.of("sk1", "sv1"))
.setLabelKeys(Collections.singletonList("sk1"))
.setDescription("My very own counter")
.setUnit("metric tonnes")
.setMonotonic(true)
.build();
assertThat(longObserver).isNotNull();
assertThat(longObserver).isInstanceOf(LongObserverSdk.class);

// todo: verify that the MeterSdk has kept track of what has been created, once that's in place
}

@Test
public void testDoubleCounter() {
DoubleCounter doubleCounter =
Expand Down Expand Up @@ -102,6 +121,23 @@ public void testDoubleMeasure() {
// todo: verify that the MeterSdk has kept track of what has been created, once that's in place
}

@Test
public void testDoubleObserver() {
DoubleObserver doubleObserver =
testSdk
.doubleObserverBuilder("testCounter")
.setConstantLabels(ImmutableMap.of("sk1", "sv1"))
.setLabelKeys(Collections.singletonList("sk1"))
.setDescription("My very own counter")
.setUnit("metric tonnes")
.setMonotonic(true)
.build();
assertThat(doubleObserver).isNotNull();
assertThat(doubleObserver).isInstanceOf(DoubleObserverSdk.class);

// todo: verify that the MeterSdk has kept track of what has been created, once that's in place
}

@Test
public void testLabelSets() {
assertThat(testSdk.createLabelSet()).isSameInstanceAs(testSdk.createLabelSet());
Expand Down