Skip to content
This repository was archived by the owner on Dec 23, 2023. It is now read-only.

Commit 131b6f0

Browse files
authored
Add MessageEvent and deprecate NetworkEvent. (closes #858) (#894)
1 parent 490dfe8 commit 131b6f0

File tree

24 files changed

+698
-54
lines changed

24 files changed

+698
-54
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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.internal;
18+
19+
import static com.google.common.base.Preconditions.checkNotNull;
20+
21+
import io.opencensus.trace.MessageEvent;
22+
23+
/**
24+
* Helper class to convert/cast between for {@link MessageEvent} and {@link NetworkEvent}.
25+
*
26+
* @since 0.12
27+
*/
28+
@SuppressWarnings("deprecation")
29+
public final class BaseMessageEventUtil {
30+
/**
31+
* Cast or convert a {@link io.opencensus.trace.BaseMessageEvent} to {@link MessageEvent}.
32+
*
33+
* <p>Warning: if the input is a {@code io.opencensus.trace.NetworkEvent} and contains {@code
34+
* kernelTimestamp} information, this information will be dropped.
35+
*
36+
* @param event the {@code BaseMessageEvent} that is being cast or converted.
37+
* @return a {@code MessageEvent} representation of the input.
38+
*/
39+
public static MessageEvent asMessageEvent(io.opencensus.trace.BaseMessageEvent event) {
40+
checkNotNull(event);
41+
if (event instanceof MessageEvent) {
42+
return (MessageEvent) event;
43+
}
44+
io.opencensus.trace.NetworkEvent networkEvent = (io.opencensus.trace.NetworkEvent) event;
45+
MessageEvent.Type type =
46+
(networkEvent.getType() == io.opencensus.trace.NetworkEvent.Type.RECV)
47+
? MessageEvent.Type.RECEIVED
48+
: MessageEvent.Type.SENT;
49+
return MessageEvent.builder(type, networkEvent.getMessageId())
50+
.setUncompressedMessageSize(networkEvent.getUncompressedMessageSize())
51+
.setCompressedMessageSize(networkEvent.getCompressedMessageSize())
52+
.build();
53+
}
54+
55+
/**
56+
* Cast or convert a {@link io.opencensus.trace.BaseMessageEvent} to {@link
57+
* io.opencensus.trace.NetworkEvent}.
58+
*
59+
* @param event the {@code BaseMessageEvent} that is being cast or converted.
60+
* @return a {@code io.opencensus.trace.NetworkEvent} representation of the input.
61+
*/
62+
public static io.opencensus.trace.NetworkEvent asNetworkEvent(
63+
io.opencensus.trace.BaseMessageEvent event) {
64+
checkNotNull(event);
65+
if (event instanceof io.opencensus.trace.NetworkEvent) {
66+
return (io.opencensus.trace.NetworkEvent) event;
67+
}
68+
MessageEvent messageEvent = (MessageEvent) event;
69+
io.opencensus.trace.NetworkEvent.Type type =
70+
(messageEvent.getType() == MessageEvent.Type.RECEIVED)
71+
? io.opencensus.trace.NetworkEvent.Type.RECV
72+
: io.opencensus.trace.NetworkEvent.Type.SENT;
73+
return io.opencensus.trace.NetworkEvent.builder(type, messageEvent.getMessageId())
74+
.setUncompressedMessageSize(messageEvent.getUncompressedMessageSize())
75+
.setCompressedMessageSize(messageEvent.getCompressedMessageSize())
76+
.build();
77+
}
78+
79+
private BaseMessageEventUtil() {}
80+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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.trace;
18+
19+
/**
20+
* Superclass for {@link MessageEvent} and {@link NetworkEvent} to resolve API backward
21+
* compatibility issue.
22+
*
23+
* <p>{@code SpanData.create} can't be overloaded with parameter types that differ only in the type
24+
* of the TimedEvent, because the signatures are the same after generic type erasure. {@code
25+
* BaseMessageEvent} allows the same method to accept both {@code TimedEvents<NetworkEvent>} and
26+
* {@code TimedEvents<MessageEvent>}.
27+
*
28+
* <p>This class should only be extended by {@code NetworkEvent} and {@code MessageEvent}.
29+
*
30+
* @deprecated This class is for internal use only.
31+
* @since 0.12
32+
*/
33+
@Deprecated
34+
public abstract class BaseMessageEvent {
35+
// package protected to avoid users to extend it.
36+
BaseMessageEvent() {}
37+
}

api/src/main/java/io/opencensus/trace/BlankSpan.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,13 @@ public void addAnnotation(Annotation annotation) {}
5858

5959
/** No-op implementation of the {@link Span#addNetworkEvent(NetworkEvent)} method. */
6060
@Override
61+
@Deprecated
6162
public void addNetworkEvent(NetworkEvent networkEvent) {}
6263

64+
/** No-op implementation of the {@link Span#addMessageEvent(MessageEvent)} method. */
65+
@Override
66+
public void addMessageEvent(MessageEvent messageEvent) {}
67+
6368
/** No-op implementation of the {@link Span#addLink(Link)} method. */
6469
@Override
6570
public void addLink(Link link) {}
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
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.trace;
18+
19+
import static com.google.common.base.Preconditions.checkNotNull;
20+
21+
import com.google.auto.value.AutoValue;
22+
import javax.annotation.concurrent.Immutable;
23+
24+
/**
25+
* A class that represents a generic messaging event. This class can represent messaging happened in
26+
* any layer, especially higher application layer. Thus, it can be used when recording events in
27+
* pipeline works, in-process bidirectional streams and batch processing.
28+
*
29+
* <p>It requires a {@link Type type} and a message id that serves to uniquely identify each
30+
* message. It can optionally have information about the message size.
31+
*
32+
* @since 0.12
33+
*/
34+
@Immutable
35+
@AutoValue
36+
// Suppress Checker Framework warning about missing @Nullable in generated equals method.
37+
@AutoValue.CopyAnnotations
38+
@SuppressWarnings({"nullness", "deprecation"})
39+
public abstract class MessageEvent extends BaseMessageEvent {
40+
/**
41+
* Available types for a {@code MessageEvent}.
42+
*
43+
* @since 0.12
44+
*/
45+
public enum Type {
46+
/**
47+
* When the message was sent.
48+
*
49+
* @since 0.12
50+
*/
51+
SENT,
52+
/**
53+
* When the message was received.
54+
*
55+
* @since 0.12
56+
*/
57+
RECEIVED,
58+
}
59+
60+
/**
61+
* Returns a new {@link Builder} with default values.
62+
*
63+
* @param type designates whether this is a send or receive message.
64+
* @param messageId serves to uniquely identify each message.
65+
* @return a new {@code Builder} with default values.
66+
* @throws NullPointerException if {@code type} is {@code null}.
67+
* @since 0.12
68+
*/
69+
public static Builder builder(Type type, long messageId) {
70+
return new AutoValue_MessageEvent.Builder()
71+
.setType(checkNotNull(type, "type"))
72+
.setMessageId(messageId)
73+
// We need to set a value for the message size because the autovalue requires all
74+
// primitives to be initialized.
75+
.setUncompressedMessageSize(0)
76+
.setCompressedMessageSize(0);
77+
}
78+
79+
/**
80+
* Returns the type of the {@code MessageEvent}.
81+
*
82+
* @return the type of the {@code MessageEvent}.
83+
* @since 0.12
84+
*/
85+
public abstract Type getType();
86+
87+
/**
88+
* Returns the message id argument that serves to uniquely identify each message.
89+
*
90+
* @return the message id of the {@code MessageEvent}.
91+
* @since 0.12
92+
*/
93+
public abstract long getMessageId();
94+
95+
/**
96+
* Returns the uncompressed size in bytes of the {@code MessageEvent}.
97+
*
98+
* @return the uncompressed size in bytes of the {@code MessageEvent}.
99+
* @since 0.12
100+
*/
101+
public abstract long getUncompressedMessageSize();
102+
103+
/**
104+
* Returns the compressed size in bytes of the {@code MessageEvent}.
105+
*
106+
* @return the compressed size in bytes of the {@code MessageEvent}.
107+
* @since 0.12
108+
*/
109+
public abstract long getCompressedMessageSize();
110+
111+
/**
112+
* Builder class for {@link MessageEvent}.
113+
*
114+
* @since 0.12
115+
*/
116+
@AutoValue.Builder
117+
public abstract static class Builder {
118+
// Package protected methods because these values are mandatory and set only in the
119+
// MessageEvent#builder() function.
120+
abstract Builder setType(Type type);
121+
122+
abstract Builder setMessageId(long messageId);
123+
124+
/**
125+
* Sets the uncompressed message size.
126+
*
127+
* @param uncompressedMessageSize represents the uncompressed size in bytes of this message.
128+
* @return this.
129+
* @since 0.12
130+
*/
131+
public abstract Builder setUncompressedMessageSize(long uncompressedMessageSize);
132+
133+
/**
134+
* Sets the compressed message size.
135+
*
136+
* @param compressedMessageSize represents the compressed size in bytes of this message.
137+
* @return this.
138+
* @since 0.12
139+
*/
140+
public abstract Builder setCompressedMessageSize(long compressedMessageSize);
141+
142+
/**
143+
* Builds and returns a {@code MessageEvent} with the desired values.
144+
*
145+
* @return a {@code MessageEvent} with the desired values.
146+
* @since 0.12
147+
*/
148+
public abstract MessageEvent build();
149+
150+
Builder() {}
151+
}
152+
153+
MessageEvent() {}
154+
}

api/src/main/java/io/opencensus/trace/NetworkEvent.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,16 @@
2828
* serves to uniquely identify each network message. It can optionally can have information about
2929
* the kernel time and message size.
3030
*
31+
* @deprecated Use {@link MessageEvent}.
3132
* @since 0.5
3233
*/
3334
@Immutable
3435
@AutoValue
3536
// Suppress Checker Framework warning about missing @Nullable in generated equals method.
3637
@AutoValue.CopyAnnotations
3738
@SuppressWarnings("nullness")
38-
public abstract class NetworkEvent {
39+
@Deprecated
40+
public abstract class NetworkEvent extends io.opencensus.trace.BaseMessageEvent {
3941
/**
4042
* Available types for a {@code NetworkEvent}.
4143
*
@@ -131,9 +133,12 @@ public long getMessageSize() {
131133
/**
132134
* Builder class for {@link NetworkEvent}.
133135
*
136+
* @deprecated {@link NetworkEvent} is deprecated. Please use {@link MessageEvent} and its builder
137+
* {@link MessageEvent.Builder}.
134138
* @since 0.5
135139
*/
136140
@AutoValue.Builder
141+
@Deprecated
137142
public abstract static class Builder {
138143
// Package protected methods because these values are mandatory and set only in the
139144
// NetworkEvent#builder() function.

api/src/main/java/io/opencensus/trace/Span.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static com.google.common.base.Preconditions.checkArgument;
2020
import static com.google.common.base.Preconditions.checkNotNull;
2121

22+
import io.opencensus.internal.BaseMessageEventUtil;
2223
import java.util.Collections;
2324
import java.util.EnumSet;
2425
import java.util.Map;
@@ -159,9 +160,30 @@ public final void addAnnotation(String description) {
159160
* higher level applications.
160161
*
161162
* @param networkEvent the network event to add.
163+
* @deprecated Use {@link #addMessageEvent}.
162164
* @since 0.5
163165
*/
164-
public abstract void addNetworkEvent(NetworkEvent networkEvent);
166+
@Deprecated
167+
public void addNetworkEvent(NetworkEvent networkEvent) {
168+
addMessageEvent(BaseMessageEventUtil.asMessageEvent(networkEvent));
169+
}
170+
171+
/**
172+
* Adds a MessageEvent to the {@code Span}.
173+
*
174+
* <p>This function can be used by higher level applications to record messaging event.
175+
*
176+
* <p>This method should always be overridden by users whose API versions are larger or equal to
177+
* {@code 0.12}.
178+
*
179+
* @param messageEvent the message to add.
180+
* @since 0.12
181+
*/
182+
public void addMessageEvent(MessageEvent messageEvent) {
183+
// Default implementation by invoking addNetworkEvent() so that any existing derived classes,
184+
// including implementation and the mocked ones, do not need to override this method explicitly.
185+
addNetworkEvent(BaseMessageEventUtil.asNetworkEvent(messageEvent));
186+
}
165187

166188
/**
167189
* Adds a {@link Link} to the {@code Span}.

0 commit comments

Comments
 (0)