From e76f543b004899d378bfd205bb59d24184a6321d Mon Sep 17 00:00:00 2001 From: Yang Song Date: Thu, 30 May 2019 10:44:35 -0700 Subject: [PATCH] Implement TimedEvent in SDK. (#354) * Implement Event in SDK. * Make EventSdk package-private. * EventSdk -> TimedEvent. --- .../opentelemetry/sdk/trace/TimedEvent.java | 92 +++++++++++++++++++ .../sdk/trace/TimedEventTest.java | 75 +++++++++++++++ 2 files changed, 167 insertions(+) create mode 100644 sdk/src/main/java/io/opentelemetry/sdk/trace/TimedEvent.java create mode 100644 sdk/src/test/java/io/opentelemetry/sdk/trace/TimedEventTest.java diff --git a/sdk/src/main/java/io/opentelemetry/sdk/trace/TimedEvent.java b/sdk/src/main/java/io/opentelemetry/sdk/trace/TimedEvent.java new file mode 100644 index 0000000000..85190a80d5 --- /dev/null +++ b/sdk/src/main/java/io/opentelemetry/sdk/trace/TimedEvent.java @@ -0,0 +1,92 @@ +/* + * Copyright 2019, 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.trace; + +import com.google.auto.value.AutoValue; +import io.opentelemetry.trace.AttributeValue; +import io.opentelemetry.trace.Event; +import java.util.Collections; +import java.util.Map; +import javax.annotation.concurrent.Immutable; + +/** Timed event that uses nanoTime to represent the Timestamp. */ +@Immutable +abstract class TimedEvent { + + private static final Map EMPTY_ATTRIBUTES = + Collections.unmodifiableMap(Collections.emptyMap()); + + abstract long getNanotime(); + + abstract String getName(); + + abstract Map getAttributes(); + + TimedEvent() {} + + /** + * Creates an {@link TimedEvent} with the given time, name and empty attributes. + * + * @param nanoTime time in nanos. + * @param name the name of this {@code TimedEvent}. + * @return an {@code TimedEvent}. + */ + static TimedEvent create(long nanoTime, String name) { + return create(nanoTime, name, EMPTY_ATTRIBUTES); + } + + /** + * Creates an {@link TimedEvent} with the given time, name and attributes. + * + * @param nanoTime time in nanos. + * @param name the name of this {@code TimedEvent}. + * @param attributes the attributes of this {@code TimedEvent}. + * @return an {@code TimedEvent}. + */ + static TimedEvent create(long nanoTime, String name, Map attributes) { + return new AutoValue_TimedEvent_RawTimedEvent(nanoTime, name, attributes); + } + + /** + * Creates an {@link TimedEvent} with the given time and event. + * + * @param nanoTime time in nanos. + * @param event the event. + * @return an {@code TimedEvent}. + */ + static TimedEvent create(long nanoTime, Event event) { + return new AutoValue_TimedEvent_TimedEventWithEvent(nanoTime, event); + } + + @AutoValue + abstract static class TimedEventWithEvent extends TimedEvent { + abstract Event getEvent(); + + @Override + String getName() { + return getEvent().getName(); + } + + @Override + Map getAttributes() { + return getEvent().getAttributes(); + } + } + + @AutoValue + abstract static class RawTimedEvent extends TimedEvent {} +} diff --git a/sdk/src/test/java/io/opentelemetry/sdk/trace/TimedEventTest.java b/sdk/src/test/java/io/opentelemetry/sdk/trace/TimedEventTest.java new file mode 100644 index 0000000000..c10f234b6c --- /dev/null +++ b/sdk/src/test/java/io/opentelemetry/sdk/trace/TimedEventTest.java @@ -0,0 +1,75 @@ +/* + * Copyright 2019, 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.trace; + +import static com.google.common.truth.Truth.assertThat; + +import io.opentelemetry.trace.AttributeValue; +import io.opentelemetry.trace.Event; +import java.util.Collections; +import java.util.Map; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Unit tests for {@link TimedEvent}. */ +@RunWith(JUnit4.class) +public class TimedEventTest { + + private static final String NAME = "event"; + private static final String NAME_2 = "event2"; + private static final Map ATTRIBUTES = + Collections.singletonMap("attribute", AttributeValue.stringAttributeValue("value")); + private static final Map ATTRIBUTES_2 = + Collections.singletonMap("attribute2", AttributeValue.stringAttributeValue("value2")); + private static final Event EVENT = + new Event() { + @Override + public String getName() { + return NAME_2; + } + + @Override + public Map getAttributes() { + return ATTRIBUTES_2; + } + }; + + @Test + public void rawTimedEventWithName() { + TimedEvent event = TimedEvent.create(1000, NAME); + assertThat(event.getNanotime()).isEqualTo(1000); + assertThat(event.getName()).isEqualTo(NAME); + assertThat(event.getAttributes()).isEmpty(); + } + + @Test + public void rawTimedEventWithNameAndAttributes() { + TimedEvent event = TimedEvent.create(2000, NAME, ATTRIBUTES); + assertThat(event.getNanotime()).isEqualTo(2000); + assertThat(event.getName()).isEqualTo(NAME); + assertThat(event.getAttributes()).isEqualTo(ATTRIBUTES); + } + + @Test + public void timedEventWithEvent() { + TimedEvent event = TimedEvent.create(3000, EVENT); + assertThat(event.getNanotime()).isEqualTo(3000); + assertThat(event.getName()).isEqualTo(NAME_2); + assertThat(event.getAttributes()).isEqualTo(ATTRIBUTES_2); + } +}