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

Implemented base zPages classes and TraceZ zPage #1380

Merged
merged 41 commits into from
Jul 15, 2020
Merged
Show file tree
Hide file tree
Changes from 36 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
9ca81bc
Implemented a span processor for the TraceZ zPage
williamhu99 Jun 2, 2020
86c6db8
Implemented aggregation logic for running and latency based spans,
williamhu99 Jun 4, 2020
66bafe2
Implemented aggregation logic for error based spans
williamhu99 Jun 16, 2020
af80d27
Finished implementation of TraceZ zPage summary table
wty27 Jun 18, 2020
d2eb7be
Finished implementation of HttpHandler, HttpServer, and TraceZ zPage
wty27 Jun 20, 2020
219d383
Refactored the sdk_contrib folder to follow updated syntax
williamhu99 Jun 29, 2020
febc453
Merge branch 'master' into master
williamhu99 Jun 29, 2020
425e436
Removed duplicate function
williamhu99 Jun 29, 2020
0f282f2
Changed inline base64 images
wty27 Jun 30, 2020
a2ef65c
Fixed copyright statement
wty27 Jun 30, 2020
d7f4904
Modified the TracezSpanProcessor to only allow for a limited number o…
williamhu99 Jul 1, 2020
50fe3f1
Fixed typos and turned SpanProcessor and DataAggregator to package pr…
williamhu99 Jul 1, 2020
12c333a
Separated SpanBuckets and LatencyBoundaries from TracezDataAggregator
williamhu99 Jul 1, 2020
1f409ad
Switched to PrintStream, removed bufferWritter, removed factory and c…
wty27 Jul 2, 2020
db2b2fa
Fixed package name
wty27 Jul 2, 2020
6922488
Changed way of registering handlers, changed atomicBoolean to final, …
wty27 Jul 2, 2020
63bc912
Changed registerTracezZPageHandler to package private
wty27 Jul 2, 2020
8ed29a7
Fixed javadoc styling and wording
wty27 Jul 3, 2020
be0d55d
Added constants for splitters, changed httpserver to compileOnly depe…
wty27 Jul 6, 2020
ff9247b
Removed FQNs from backend files
williamhu99 Jul 6, 2020
bd28217
Removed style errors
williamhu99 Jul 6, 2020
fb41a8b
Updated TracezSpanProcessor to maintain a set of span names increment…
williamhu99 Jul 6, 2020
8b46dd9
Added visibleForTesting tag, adjusted HTML expression
wty27 Jul 7, 2020
99497ac
Merge branch 'master' of github.com:williamhu99/opentelemetry-java in…
williamhu99 Jul 7, 2020
9ee432f
Removed unused functions in the data aggregator
williamhu99 Jul 7, 2020
dcaf730
Made TracezSpanBuckets thread-safe
williamhu99 Jul 7, 2020
c5e819e
Changed test to use Mockitorule, removed unnecessary Formatter, unrol…
wty27 Jul 8, 2020
19b16d7
Changed test with @Mock to use Mockito test runner
wty27 Jul 8, 2020
2647c45
Renamed LatencyBoundaries to LatencyBoundary and cleaned up code
williamhu99 Jul 8, 2020
eea8895
Merge branch 'master' of github.com:williamhu99/opentelemetry-java in…
williamhu99 Jul 8, 2020
88a703b
Replaced the EvictingQueue with a faster SpanBucket class and modifie…
williamhu99 Jul 8, 2020
fee6378
Addressed additional comments
williamhu99 Jul 9, 2020
a393431
Migrated images to resources, used logger to log errors, and other mi…
wty27 Jul 9, 2020
0b2357f
Moved a variable to within a class in TracezZPageHandler
williamhu99 Jul 9, 2020
be61de2
Made a small fix to scope
williamhu99 Jul 9, 2020
2495cf9
Removed printStackTrace, added test for query parameter
wty27 Jul 13, 2020
648a8b4
Changed logger to static field, used log method to log detailed stack…
wty27 Jul 13, 2020
d6113ea
Made minor fixes
williamhu99 Jul 13, 2020
23e1728
Resolved unmodifiable list error with getOkSpans and getErrorSpans
williamhu99 Jul 13, 2020
317876f
Changed map @Mock to empty map, added more test
wty27 Jul 14, 2020
842629c
Changed test runner
wty27 Jul 15, 2020
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
9 changes: 9 additions & 0 deletions sdk_extensions/zpages/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# OpenTelemetry SDK Contrib - zPages

[![Javadocs][javadoc-image]][javadoc-url]

This module contains code for OpenTelemetry's Java zPages.

<!--- TODO: Update javadoc -->
[javadoc-image]: https://www.javadoc.io/badge/io.opentelemetry/opentelemetry-sdk-contrib-auto-config.svg
[javadoc-url]: https://www.javadoc.io/doc/io.opentelemetry/opentelemetry-sdk-contrib-auto-config
wangty27 marked this conversation as resolved.
Show resolved Hide resolved
19 changes: 19 additions & 0 deletions sdk_extensions/zpages/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
plugins {
id "java"
id "maven-publish"

id "ru.vyarus.animalsniffer"
}

description = 'OpenTelemetry - zPages'
ext.moduleName = "io.opentelemetry.sdk.extension.zpages"

dependencies {
api project(':opentelemetry-api'),
wangty27 marked this conversation as resolved.
Show resolved Hide resolved
project(':opentelemetry-sdk')

implementation libraries.guava
compileOnly 'com.sun.net.httpserver:http:20070405'

signature "org.codehaus.mojo.signature:java17:1.0@signature"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* 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.extensions.zpages;

import java.util.concurrent.TimeUnit;

/**
* A class of boundaries for the latency buckets. The completed spans with a status of {@link
* io.opentelemetry.trace.Status#OK} are categorized into one of these buckets om the traceZ zPage.
*/
enum LatencyBoundary {
/** Stores finished successful requests of duration within the interval [0, 10us). */
ZERO_MICROSx10(0, TimeUnit.MICROSECONDS.toNanos(10)),

/** Stores finished successful requests of duration within the interval [10us, 100us). */
MICROSx10_MICROSx100(TimeUnit.MICROSECONDS.toNanos(10), TimeUnit.MICROSECONDS.toNanos(100)),

/** Stores finished successful requests of duration within the interval [100us, 1ms). */
MICROSx100_MILLIx1(TimeUnit.MICROSECONDS.toNanos(100), TimeUnit.MILLISECONDS.toNanos(1)),

/** Stores finished successful requests of duration within the interval [1ms, 10ms). */
MILLIx1_MILLIx10(TimeUnit.MILLISECONDS.toNanos(1), TimeUnit.MILLISECONDS.toNanos(10)),

/** Stores finished successful requests of duration within the interval [10ms, 100ms). */
MILLIx10_MILLIx100(TimeUnit.MILLISECONDS.toNanos(10), TimeUnit.MILLISECONDS.toNanos(100)),

/** Stores finished successful requests of duration within the interval [100ms, 1sec). */
MILLIx100_SECONDx1(TimeUnit.MILLISECONDS.toNanos(100), TimeUnit.SECONDS.toNanos(1)),

/** Stores finished successful requests of duration within the interval [1sec, 10sec). */
SECONDx1_SECONDx10(TimeUnit.SECONDS.toNanos(1), TimeUnit.SECONDS.toNanos(10)),

/** Stores finished successful requests of duration within the interval [10sec, 100sec). */
SECONDx10_SECONDx100(TimeUnit.SECONDS.toNanos(10), TimeUnit.SECONDS.toNanos(100)),

/** Stores finished successful requests of duration greater than or equal to 100sec. */
SECONDx100_MAX(TimeUnit.SECONDS.toNanos(100), Long.MAX_VALUE);

private final long latencyLowerBound;
private final long latencyUpperBound;

/**
* Constructs a {@code LatencyBoundaries} with the given boundaries and label.
*
* @param latencyLowerBound the latency lower bound of the bucket.
* @param latencyUpperBound the latency upper bound of the bucket.
*/
LatencyBoundary(long latencyLowerBound, long latencyUpperBound) {
this.latencyLowerBound = latencyLowerBound;
this.latencyUpperBound = latencyUpperBound;
}

/**
* Returns the latency lower bound of the bucket.
*
* @return the latency lower bound of the bucket.
*/
long getLatencyLowerBound() {
return latencyLowerBound;
}

/**
* Returns the latency upper bound of the bucket.
*
* @return the latency upper bound of the bucket.
*/
long getLatencyUpperBound() {
return latencyUpperBound;
}

/**
* Returns the LatencyBoundary that the argument falls into.
*
* @param latencyNanos latency in nanoseconds.
* @return the LatencyBoundary that latencyNanos falls into.
*/
static LatencyBoundary getBoundary(long latencyNanos) {
for (LatencyBoundary bucket : LatencyBoundary.values()) {
if (latencyNanos >= bucket.getLatencyLowerBound()
&& latencyNanos < bucket.getLatencyUpperBound()) {
return bucket;
}
}
return ZERO_MICROSx10;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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.extensions.zpages;

import com.google.common.primitives.UnsignedInts;
import io.opentelemetry.sdk.trace.ReadableSpan;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

final class SpanBucket {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd love to have unit tests on this class specifically, but that can be in a follow-up PR.

// A power of 2 means Integer.MAX_VALUE % bucketSize = bucketSize - 1, so the index will always
// loop back to 0.
private static final int LATENCY_BUCKET_SIZE = 16;
private static final int ERROR_BUCKET_SIZE = 8;

private final ReadableSpan[] spans;
private final AtomicInteger index;
private final int bucketSize;

SpanBucket(boolean isLatencyBucket) {
bucketSize = isLatencyBucket ? LATENCY_BUCKET_SIZE : ERROR_BUCKET_SIZE;
spans = new ReadableSpan[bucketSize];
wangty27 marked this conversation as resolved.
Show resolved Hide resolved
index = new AtomicInteger();
}

void add(ReadableSpan span) {
spans[UnsignedInts.remainder(index.getAndIncrement(), bucketSize)] = span;
}

int size() {
for (int i = bucketSize - 1; i >= 0; i--) {
if (spans[i] != null) {
return i + 1;
}
}
return 0;
}

void addTo(List<ReadableSpan> result) {
for (int i = 0; i < bucketSize; i++) {
ReadableSpan span = spans[i];
if (span != null) {
result.add(span);
} else {
break;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
/*
* 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.extensions.zpages;

import io.opentelemetry.sdk.trace.ReadableSpan;
import io.opentelemetry.sdk.trace.data.SpanData;
import io.opentelemetry.trace.Status;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeSet;
import javax.annotation.concurrent.ThreadSafe;

/**
* A data aggregator for the traceZ zPage.
*
* <p>The traceZ data aggregator compiles information about the running spans, span latencies, and
* error spans for the frontend of the zPage.
*/
@ThreadSafe
final class TracezDataAggregator {
private final TracezSpanProcessor spanProcessor;

/**
* Constructor for {@link TracezDataAggregator}.
*
* @param spanProcessor collects span data.
*/
TracezDataAggregator(TracezSpanProcessor spanProcessor) {
this.spanProcessor = spanProcessor;
}

/**
* Returns a Set of running and completed span names for {@link TracezDataAggregator}.
*
* @return a Set of {@link String}.
*/
Set<String> getSpanNames() {
Set<String> spanNames = new TreeSet<>();
wangty27 marked this conversation as resolved.
Show resolved Hide resolved
Collection<ReadableSpan> allRunningSpans = spanProcessor.getRunningSpans();
wangty27 marked this conversation as resolved.
Show resolved Hide resolved
for (ReadableSpan span : allRunningSpans) {
spanNames.add(span.getName());
}
spanNames.addAll(spanProcessor.getCompletedSpanCache().keySet());
return spanNames;
}

/**
* Returns a Map of the running span counts for {@link TracezDataAggregator}.
*
* @return a Map of span counts for each span name.
*/
Map<String, Integer> getRunningSpanCounts() {
Collection<ReadableSpan> allRunningSpans = spanProcessor.getRunningSpans();
Map<String, Integer> numSpansPerName = new HashMap<>();
for (ReadableSpan span : allRunningSpans) {
Integer prevValue = numSpansPerName.get(span.getName());
numSpansPerName.put(span.getName(), prevValue != null ? prevValue + 1 : 1);
}
return numSpansPerName;
}

/**
* Returns a List of all running spans with a given span name for {@link TracezDataAggregator}.
*
* @param spanName name to filter returned spans.
* @return a List of {@link SpanData}.
*/
List<SpanData> getRunningSpans(String spanName) {
Collection<ReadableSpan> allRunningSpans = spanProcessor.getRunningSpans();
List<SpanData> filteredSpans = new ArrayList<>();
for (ReadableSpan span : allRunningSpans) {
if (span.getName().equals(spanName)) {
filteredSpans.add(span.toSpanData());
}
}
return filteredSpans;
}

/**
* Returns a Map of span names to counts for all {@link Status#OK} spans in {@link
* TracezDataAggregator}.
*
* @return a Map of span names to counts, where the counts are further indexed by the latency
* boundaries.
*/
Map<String, Map<LatencyBoundary, Integer>> getSpanLatencyCounts() {
Map<String, TracezSpanBuckets> completedSpanCache = spanProcessor.getCompletedSpanCache();
Map<String, Map<LatencyBoundary, Integer>> numSpansPerName = new HashMap<>();
for (Entry<String, TracezSpanBuckets> cacheEntry : completedSpanCache.entrySet()) {
numSpansPerName.put(
cacheEntry.getKey(), cacheEntry.getValue().getLatencyBoundaryToCountMap());
}
return numSpansPerName;
}

/**
* Returns a List of all {@link Status#OK} spans with a given span name between [lowerBound,
* upperBound) for {@link TracezDataAggregator}.
*
* @param spanName name to filter returned spans.
* @param lowerBound latency lower bound (inclusive)
* @param upperBound latency upper bound (exclusive)
* @return a List of {@link SpanData}.
*/
List<SpanData> getOkSpans(String spanName, long lowerBound, long upperBound) {
Map<String, TracezSpanBuckets> completedSpanCache = spanProcessor.getCompletedSpanCache();
TracezSpanBuckets buckets = completedSpanCache.get(spanName);
if (buckets == null) {
return new ArrayList<>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Collections.emptyList

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's an error with immutability vs mutability if we revert back to Collections.emptyList. Should I change the function signature to return an immutable list?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're ok with guava, then yes. 👍

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, turns out there's another way to do it as well! You can resolve this comment along with the one below.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll wait to resolve until it's been changed. ;)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good. I already pushed the change, so I hope it updates in the repo soon.

}
Collection<ReadableSpan> allOkSpans = buckets.getOkSpans();
List<SpanData> filteredSpans = new ArrayList<>();
for (ReadableSpan span : allOkSpans) {
if (span.getLatencyNanos() >= lowerBound && span.getLatencyNanos() < upperBound) {
filteredSpans.add(span.toSpanData());
}
}
return filteredSpans;
}

/**
* Returns a Map of error span counts for {@link TracezDataAggregator}.
*
* @return a Map of error span counts for each span name.
*/
Map<String, Integer> getErrorSpanCounts() {
Map<String, TracezSpanBuckets> completedSpanCache = spanProcessor.getCompletedSpanCache();
Map<String, Integer> numErrorsPerName = new HashMap<>();
for (Entry<String, TracezSpanBuckets> cacheEntry : completedSpanCache.entrySet()) {
numErrorsPerName.put(cacheEntry.getKey(), cacheEntry.getValue().getErrorSpans().size());
}
return numErrorsPerName;
}

/**
* Returns a List of error spans with a given span name for {@link TracezDataAggregator}.
*
* @param spanName name to filter returned spans.
* @return a List of {@link SpanData}.
*/
List<SpanData> getErrorSpans(String spanName) {
Map<String, TracezSpanBuckets> completedSpanCache = spanProcessor.getCompletedSpanCache();
TracezSpanBuckets buckets = completedSpanCache.get(spanName);
if (buckets == null) {
return new ArrayList<>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Collections.emptyList

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same response as above

}
Collection<ReadableSpan> allErrorSpans = buckets.getErrorSpans();
List<SpanData> errorSpans = new ArrayList<>();
for (ReadableSpan span : allErrorSpans) {
errorSpans.add(span.toSpanData());
}
return errorSpans;
}
}
Loading