1 /* 2 * Copyright 2017, 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.export; 18 19 import com.google.auto.value.AutoValue; 20 import io.opencensus.common.Timestamp; 21 import io.opencensus.internal.Utils; 22 import io.opencensus.trace.Annotation; 23 import io.opencensus.trace.AttributeValue; 24 import io.opencensus.trace.Link; 25 import io.opencensus.trace.MessageEvent; 26 import io.opencensus.trace.Span; 27 import io.opencensus.trace.Span.Kind; 28 import io.opencensus.trace.SpanContext; 29 import io.opencensus.trace.SpanId; 30 import io.opencensus.trace.Status; 31 import io.opencensus.trace.internal.BaseMessageEventUtils; 32 import java.util.ArrayList; 33 import java.util.Collections; 34 import java.util.HashMap; 35 import java.util.List; 36 import java.util.Map; 37 import javax.annotation.Nullable; 38 import javax.annotation.concurrent.Immutable; 39 40 /*>>> 41 import org.checkerframework.dataflow.qual.Deterministic; 42 */ 43 44 /** 45 * Immutable representation of all data collected by the {@link Span} class. 46 * 47 * @since 0.5 48 */ 49 @Immutable 50 @AutoValue 51 public abstract class SpanData { 52 53 /** 54 * Returns a new immutable {@code SpanData}. 55 * 56 * @deprecated Use {@link #create(SpanContext, SpanId, Boolean, String, Kind, Timestamp, 57 * Attributes, TimedEvents, TimedEvents, Links, Integer, Status, Timestamp)}. 58 */ 59 @Deprecated 60 public static SpanData create( 61 SpanContext context, 62 @Nullable SpanId parentSpanId, 63 @Nullable Boolean hasRemoteParent, 64 String name, 65 Timestamp startTimestamp, 66 Attributes attributes, 67 TimedEvents<Annotation> annotations, 68 TimedEvents<? extends io.opencensus.trace.BaseMessageEvent> messageOrNetworkEvents, 69 Links links, 70 @Nullable Integer childSpanCount, 71 @Nullable Status status, 72 @Nullable Timestamp endTimestamp) { 73 return create( 74 context, 75 parentSpanId, 76 hasRemoteParent, 77 name, 78 null, 79 startTimestamp, 80 attributes, 81 annotations, 82 messageOrNetworkEvents, 83 links, 84 childSpanCount, 85 status, 86 endTimestamp); 87 } 88 89 /** 90 * Returns a new immutable {@code SpanData}. 91 * 92 * @param context the {@code SpanContext} of the {@code Span}. 93 * @param parentSpanId the parent {@code SpanId} of the {@code Span}. {@code null} if the {@code 94 * Span} is a root. 95 * @param hasRemoteParent {@code true} if the parent {@code Span} is remote. {@code null} if this 96 * is a root span. 97 * @param name the name of the {@code Span}. 98 * @param kind the kind of the {@code Span}. 99 * @param startTimestamp the start {@code Timestamp} of the {@code Span}. 100 * @param attributes the attributes associated with the {@code Span}. 101 * @param annotations the annotations associated with the {@code Span}. 102 * @param messageOrNetworkEvents the message events (or network events for backward compatibility) 103 * associated with the {@code Span}. 104 * @param links the links associated with the {@code Span}. 105 * @param childSpanCount the number of child spans that were generated while the span was active. 106 * @param status the {@code Status} of the {@code Span}. {@code null} if the {@code Span} is still 107 * active. 108 * @param endTimestamp the end {@code Timestamp} of the {@code Span}. {@code null} if the {@code 109 * Span} is still active. 110 * @return a new immutable {@code SpanData}. 111 * @since 0.14 112 */ 113 @SuppressWarnings({"deprecation", "InconsistentOverloads"}) 114 public static SpanData create( 115 SpanContext context, 116 @Nullable SpanId parentSpanId, 117 @Nullable Boolean hasRemoteParent, 118 String name, 119 @Nullable Kind kind, 120 Timestamp startTimestamp, 121 Attributes attributes, 122 TimedEvents<Annotation> annotations, 123 TimedEvents<? extends io.opencensus.trace.BaseMessageEvent> messageOrNetworkEvents, 124 Links links, 125 @Nullable Integer childSpanCount, 126 @Nullable Status status, 127 @Nullable Timestamp endTimestamp) { 128 Utils.checkNotNull(messageOrNetworkEvents, "messageOrNetworkEvents"); 129 List<TimedEvent<MessageEvent>> messageEventsList = new ArrayList<TimedEvent<MessageEvent>>(); 130 for (TimedEvent<? extends io.opencensus.trace.BaseMessageEvent> timedEvent : 131 messageOrNetworkEvents.getEvents()) { 132 io.opencensus.trace.BaseMessageEvent event = timedEvent.getEvent(); 133 if (event instanceof MessageEvent) { 134 @SuppressWarnings("unchecked") 135 TimedEvent<MessageEvent> timedMessageEvent = (TimedEvent<MessageEvent>) timedEvent; 136 messageEventsList.add(timedMessageEvent); 137 } else { 138 messageEventsList.add( 139 TimedEvent.<MessageEvent>create( 140 timedEvent.getTimestamp(), BaseMessageEventUtils.asMessageEvent(event))); 141 } 142 } 143 TimedEvents<MessageEvent> messageEvents = 144 TimedEvents.<MessageEvent>create( 145 messageEventsList, messageOrNetworkEvents.getDroppedEventsCount()); 146 return new AutoValue_SpanData( 147 context, 148 parentSpanId, 149 hasRemoteParent, 150 name, 151 kind, 152 startTimestamp, 153 attributes, 154 annotations, 155 messageEvents, 156 links, 157 childSpanCount, 158 status, 159 endTimestamp); 160 } 161 162 /** 163 * Returns the {@code SpanContext} associated with this {@code Span}. 164 * 165 * @return the {@code SpanContext} associated with this {@code Span}. 166 * @since 0.5 167 */ 168 public abstract SpanContext getContext(); 169 170 /** 171 * Returns the parent {@code SpanId} or {@code null} if the {@code Span} is a root {@code Span}. 172 * 173 * @return the parent {@code SpanId} or {@code null} if the {@code Span} is a root {@code Span}. 174 * @since 0.5 175 */ 176 @Nullable 177 /*@Deterministic*/ 178 public abstract SpanId getParentSpanId(); 179 180 /** 181 * Returns {@code true} if the parent is on a different process. {@code null} if this is a root 182 * span. 183 * 184 * @return {@code true} if the parent is on a different process. {@code null} if this is a root 185 * span. 186 * @since 0.5 187 */ 188 @Nullable 189 public abstract Boolean getHasRemoteParent(); 190 191 /** 192 * Returns the name of this {@code Span}. 193 * 194 * @return the name of this {@code Span}. 195 * @since 0.5 196 */ 197 public abstract String getName(); 198 199 /** 200 * Returns the kind of this {@code Span}. 201 * 202 * @return the kind of this {@code Span}. 203 * @since 0.14 204 */ 205 @Nullable 206 public abstract Kind getKind(); 207 208 /** 209 * Returns the start {@code Timestamp} of this {@code Span}. 210 * 211 * @return the start {@code Timestamp} of this {@code Span}. 212 * @since 0.5 213 */ 214 public abstract Timestamp getStartTimestamp(); 215 216 /** 217 * Returns the attributes recorded for this {@code Span}. 218 * 219 * @return the attributes recorded for this {@code Span}. 220 * @since 0.5 221 */ 222 public abstract Attributes getAttributes(); 223 224 /** 225 * Returns the annotations recorded for this {@code Span}. 226 * 227 * @return the annotations recorded for this {@code Span}. 228 * @since 0.5 229 */ 230 public abstract TimedEvents<Annotation> getAnnotations(); 231 232 /** 233 * Returns network events recorded for this {@code Span}. 234 * 235 * @return network events recorded for this {@code Span}. 236 * @deprecated Use {@link #getMessageEvents}. 237 * @since 0.5 238 */ 239 @Deprecated 240 @SuppressWarnings({"deprecation"}) 241 public TimedEvents<io.opencensus.trace.NetworkEvent> getNetworkEvents() { 242 TimedEvents<MessageEvent> timedEvents = getMessageEvents(); 243 List<TimedEvent<io.opencensus.trace.NetworkEvent>> networkEventsList = 244 new ArrayList<TimedEvent<io.opencensus.trace.NetworkEvent>>(); 245 for (TimedEvent<MessageEvent> timedEvent : timedEvents.getEvents()) { 246 networkEventsList.add( 247 TimedEvent.<io.opencensus.trace.NetworkEvent>create( 248 timedEvent.getTimestamp(), 249 BaseMessageEventUtils.asNetworkEvent(timedEvent.getEvent()))); 250 } 251 return TimedEvents.<io.opencensus.trace.NetworkEvent>create( 252 networkEventsList, timedEvents.getDroppedEventsCount()); 253 } 254 255 /** 256 * Returns message events recorded for this {@code Span}. 257 * 258 * @return message events recorded for this {@code Span}. 259 * @since 0.12 260 */ 261 public abstract TimedEvents<MessageEvent> getMessageEvents(); 262 263 /** 264 * Returns links recorded for this {@code Span}. 265 * 266 * @return links recorded for this {@code Span}. 267 * @since 0.5 268 */ 269 public abstract Links getLinks(); 270 271 /** 272 * Returns the number of child spans that were generated while the {@code Span} was running. If 273 * not {@code null} allows service implementations to detect missing child spans. 274 * 275 * <p>This information is not always available. 276 * 277 * @return the number of child spans that were generated while the {@code Span} was running. 278 * @since 0.5 279 */ 280 @Nullable 281 public abstract Integer getChildSpanCount(); 282 283 /** 284 * Returns the {@code Status} or {@code null} if {@code Span} is still active. 285 * 286 * @return the {@code Status} or {@code null} if {@code Span} is still active. 287 * @since 0.5 288 */ 289 @Nullable 290 /*@Deterministic*/ 291 public abstract Status getStatus(); 292 293 /** 294 * Returns the end {@code Timestamp} or {@code null} if the {@code Span} is still active. 295 * 296 * @return the end {@code Timestamp} or {@code null} if the {@code Span} is still active. 297 * @since 0.5 298 */ 299 @Nullable 300 /*@Deterministic*/ 301 public abstract Timestamp getEndTimestamp(); 302 303 SpanData() {} 304 305 /** 306 * A timed event representation. 307 * 308 * @param <T> the type of value that is timed. 309 * @since 0.5 310 */ 311 @Immutable 312 @AutoValue 313 public abstract static class TimedEvent<T> { 314 /** 315 * Returns a new immutable {@code TimedEvent<T>}. 316 * 317 * @param timestamp the {@code Timestamp} of this event. 318 * @param event the event. 319 * @param <T> the type of value that is timed. 320 * @return a new immutable {@code TimedEvent<T>} 321 * @since 0.5 322 */ 323 public static <T> TimedEvent<T> create(Timestamp timestamp, T event) { 324 return new AutoValue_SpanData_TimedEvent<T>(timestamp, event); 325 } 326 327 /** 328 * Returns the {@code Timestamp} of this event. 329 * 330 * @return the {@code Timestamp} of this event. 331 * @since 0.5 332 */ 333 public abstract Timestamp getTimestamp(); 334 335 /** 336 * Returns the event. 337 * 338 * @return the event. 339 * @since 0.5 340 */ 341 /*@Deterministic*/ 342 public abstract T getEvent(); 343 344 TimedEvent() {} 345 } 346 347 /** 348 * A list of timed events and the number of dropped events representation. 349 * 350 * @param <T> the type of value that is timed. 351 * @since 0.5 352 */ 353 @Immutable 354 @AutoValue 355 public abstract static class TimedEvents<T> { 356 /** 357 * Returns a new immutable {@code TimedEvents<T>}. 358 * 359 * @param events the list of events. 360 * @param droppedEventsCount the number of dropped events. 361 * @param <T> the type of value that is timed. 362 * @return a new immutable {@code TimedEvents<T>} 363 * @since 0.5 364 */ 365 public static <T> TimedEvents<T> create(List<TimedEvent<T>> events, int droppedEventsCount) { 366 return new AutoValue_SpanData_TimedEvents<T>( 367 Collections.unmodifiableList( 368 new ArrayList<TimedEvent<T>>(Utils.checkNotNull(events, "events"))), 369 droppedEventsCount); 370 } 371 372 /** 373 * Returns the list of events. 374 * 375 * @return the list of events. 376 * @since 0.5 377 */ 378 public abstract List<TimedEvent<T>> getEvents(); 379 380 /** 381 * Returns the number of dropped events. 382 * 383 * @return the number of dropped events. 384 * @since 0.5 385 */ 386 public abstract int getDroppedEventsCount(); 387 388 TimedEvents() {} 389 } 390 391 /** 392 * A set of attributes and the number of dropped attributes representation. 393 * 394 * @since 0.5 395 */ 396 @Immutable 397 @AutoValue 398 public abstract static class Attributes { 399 /** 400 * Returns a new immutable {@code Attributes}. 401 * 402 * @param attributeMap the set of attributes. 403 * @param droppedAttributesCount the number of dropped attributes. 404 * @return a new immutable {@code Attributes}. 405 * @since 0.5 406 */ 407 public static Attributes create( 408 Map<String, AttributeValue> attributeMap, int droppedAttributesCount) { 409 // TODO(bdrutu): Consider to use LinkedHashMap here and everywhere else, less test flakes 410 // for others on account of determinism. 411 return new AutoValue_SpanData_Attributes( 412 Collections.unmodifiableMap( 413 new HashMap<String, AttributeValue>( 414 Utils.checkNotNull(attributeMap, "attributeMap"))), 415 droppedAttributesCount); 416 } 417 418 /** 419 * Returns the set of attributes. 420 * 421 * @return the set of attributes. 422 * @since 0.5 423 */ 424 public abstract Map<String, AttributeValue> getAttributeMap(); 425 426 /** 427 * Returns the number of dropped attributes. 428 * 429 * @return the number of dropped attributes. 430 * @since 0.5 431 */ 432 public abstract int getDroppedAttributesCount(); 433 434 Attributes() {} 435 } 436 437 /** 438 * A list of links and the number of dropped links representation. 439 * 440 * @since 0.5 441 */ 442 @Immutable 443 @AutoValue 444 public abstract static class Links { 445 /** 446 * Returns a new immutable {@code Links}. 447 * 448 * @param links the list of links. 449 * @param droppedLinksCount the number of dropped links. 450 * @return a new immutable {@code Links}. 451 * @since 0.5 452 */ 453 public static Links create(List<Link> links, int droppedLinksCount) { 454 return new AutoValue_SpanData_Links( 455 Collections.unmodifiableList(new ArrayList<Link>(Utils.checkNotNull(links, "links"))), 456 droppedLinksCount); 457 } 458 459 /** 460 * Returns the list of links. 461 * 462 * @return the list of links. 463 * @since 0.5 464 */ 465 public abstract List<Link> getLinks(); 466 467 /** 468 * Returns the number of dropped links. 469 * 470 * @return the number of dropped links. 471 * @since 0.5 472 */ 473 public abstract int getDroppedLinksCount(); 474 475 Links() {} 476 } 477 } 478