-
Notifications
You must be signed in to change notification settings - Fork 324
Timing breakdown #588
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
Merged
Merged
Timing breakdown #588
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
953c909
Recycle via reference counting
felixbarny 847aecd
Revert unrelated changes
felixbarny 8bc8dd1
Introduce Labels
felixbarny d0e5d51
Add breakdown
felixbarny dffbba0
Make tracking breakdown metrics garbage free
felixbarny 576f2da
Merge remote-tracking branch 'origin/master' into timing-breakdown
felixbarny 790f2e2
Update to new metric names
felixbarny 2c8d6d2
Merge remote-tracking branch 'origin/master' into timing-breakdown
felixbarny 33bfcae
Thread safe publication of immutable labels
felixbarny b5395a1
Add span.subtype to breakdown metrics
felixbarny a609d71
Disable all breakdown code paths when disable_metrics=span.self_time
felixbarny dc701f7
Document metrics
felixbarny 280f663
Clarify docs
felixbarny a336827
Another docs clarification
felixbarny 833bbe1
Fix tests
felixbarny 5639197
Guard agains concurrent reads/writes
felixbarny a0d167d
Limit number of metricsets
felixbarny 9aca16f
Add some Javadocs
felixbarny 253ad87
Eliminate allocations
felixbarny dbf5884
Addressed some comments
felixbarny 6549546
Merge remote-tracking branch 'origin/master' into timing-breakdown
felixbarny d8394c3
Report timers in microseconds and use .us suffix
felixbarny 308f519
Merge remote-tracking branch 'origin/master' into timing-breakdown
felixbarny 580bf1d
Merge remote-tracking branch 'origin/master' into timing-breakdown
felixbarny bf6c221
Fix race condition between trackMetrics and incrementTimer
felixbarny f84852a
Renaming and add comments
felixbarny File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
259 changes: 259 additions & 0 deletions
259
apm-agent-core/src/main/java/co/elastic/apm/agent/metrics/Labels.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,259 @@ | ||
/*- | ||
* #%L | ||
* Elastic APM Java agent | ||
* %% | ||
* Copyright (C) 2018 - 2019 Elastic and contributors | ||
* %% | ||
* 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. | ||
* #L% | ||
*/ | ||
package co.elastic.apm.agent.metrics; | ||
|
||
import co.elastic.apm.agent.objectpool.Recyclable; | ||
|
||
import javax.annotation.Nullable; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
public class Labels implements Recyclable { | ||
|
||
private static final Labels EMPTY = Labels.of().immutableCopy(); | ||
private final List<String> keys = new ArrayList<>(); | ||
private final List<CharSequence> values = new ArrayList<>(); | ||
private final boolean immutable; | ||
@Nullable | ||
private CharSequence transactionName; | ||
@Nullable | ||
private String transactionType; | ||
@Nullable | ||
private String spanType; | ||
private int cachedHash; | ||
|
||
public Labels() { | ||
this(Collections.<String>emptyList(), Collections.<CharSequence>emptyList(), false); | ||
} | ||
|
||
private Labels(List<String> keys, List<? extends CharSequence> values, boolean immutable) { | ||
this.keys.addAll(keys); | ||
this.values.addAll(values); | ||
this.immutable = immutable; | ||
} | ||
|
||
public static Labels of() { | ||
return new Labels(); | ||
} | ||
|
||
public static Labels of(String key, CharSequence value) { | ||
final Labels labels = new Labels(); | ||
labels.add(key, value); | ||
return labels; | ||
} | ||
|
||
public static Labels of(Map<String, ? extends CharSequence> labelMap) { | ||
felixbarny marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Labels labels = new Labels(); | ||
for (Map.Entry<String, ? extends CharSequence> entry : labelMap.entrySet()) { | ||
labels.add(entry.getKey(), entry.getValue()); | ||
} | ||
return labels; | ||
} | ||
|
||
public static Labels empty() { | ||
return EMPTY; | ||
} | ||
|
||
public Labels add(String key, CharSequence value) { | ||
assertMutable(); | ||
keys.add(key); | ||
values.add(value); | ||
return this; | ||
} | ||
|
||
public Labels transactionName(CharSequence transactionName) { | ||
assertMutable(); | ||
this.transactionName = transactionName; | ||
return this; | ||
} | ||
|
||
public Labels transactionType(String transactionType) { | ||
assertMutable(); | ||
this.transactionType = transactionType; | ||
return this; | ||
} | ||
|
||
public Labels spanType(String spanType) { | ||
assertMutable(); | ||
this.spanType = spanType; | ||
return this; | ||
} | ||
|
||
private void assertMutable() { | ||
if (immutable) { | ||
throw new UnsupportedOperationException("This Labels instance is immutable"); | ||
} | ||
} | ||
|
||
@Nullable | ||
public CharSequence getTransactionName() { | ||
return transactionName; | ||
} | ||
|
||
@Nullable | ||
public String getTransactionType() { | ||
return transactionType; | ||
} | ||
|
||
@Nullable | ||
public String getSpanType() { | ||
return spanType; | ||
} | ||
|
||
public Labels immutableCopy() { | ||
List<String> immutableValues = new ArrayList<>(values.size()); | ||
for (int i = 0; i < keys.size(); i++) { | ||
immutableValues.add(values.get(i).toString()); | ||
} | ||
final Labels labels = new Labels(keys, immutableValues, true); | ||
labels.transactionName = this.transactionName != null ? this.transactionName.toString() : null; | ||
labels.transactionType = this.transactionType; | ||
labels.spanType = this.spanType; | ||
labels.cachedHash = labels.hashCode(); | ||
return labels; | ||
} | ||
|
||
public List<String> getKeys() { | ||
return keys; | ||
} | ||
|
||
public List<CharSequence> getValues() { | ||
return values; | ||
} | ||
|
||
public boolean isEmpty() { | ||
return keys.isEmpty(); | ||
} | ||
|
||
public int size() { | ||
return keys.size(); | ||
} | ||
|
||
public String getKey(int i) { | ||
return keys.get(i); | ||
} | ||
|
||
public CharSequence getValue(int i) { | ||
return values.get(i); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
Labels labels = (Labels) o; | ||
return keys.equals(labels.keys) && | ||
isEqual(values, labels.values) && | ||
contentEquals(transactionName, labels.transactionName) && | ||
Objects.equals(transactionType, labels.transactionType) && | ||
Objects.equals(spanType, labels.spanType); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
if (cachedHash != 0) { | ||
return cachedHash; | ||
} | ||
int h = 0; | ||
for (int i = 0; i < values.size(); i++) { | ||
h = 31 * h + hash(i); | ||
} | ||
h = 31 * h + hash(transactionName); | ||
h = 31 * h + Objects.hashCode(transactionType); | ||
h = 31 * h + Objects.hashCode(spanType); | ||
return h; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
StringBuilder sb = new StringBuilder(); | ||
for (int i = 0; i < keys.size(); i++) { | ||
if (i > 0) { | ||
sb.append(", "); | ||
} | ||
sb.append(keys.get(i)).append("=").append(values.get(i)); | ||
|
||
} | ||
return sb.toString(); | ||
} | ||
|
||
private int hash(int i) { | ||
return keys.get(i).hashCode() * 31 + hash(values.get(i)); | ||
} | ||
|
||
private static boolean isEqual(List<CharSequence> values, List<CharSequence> otherValues) { | ||
if (values.size() != otherValues.size()) { | ||
return false; | ||
} | ||
for (int i = 0; i < values.size(); i++) { | ||
if (!contentEquals(values.get(i), otherValues.get(i))) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
private static boolean contentEquals(@Nullable CharSequence cs1, @Nullable CharSequence cs2) { | ||
if (cs1 == null || cs2 == null) { | ||
return cs1 == cs2; | ||
} | ||
if (cs1 instanceof String) { | ||
return ((String) cs1).contentEquals(cs2); | ||
} else if (cs2 instanceof String) { | ||
return ((String) cs2).contentEquals(cs1); | ||
} else { | ||
if (cs1.length() == cs2.length()) { | ||
for (int i = 0; i < cs1.length(); i++) { | ||
if (cs1.charAt(i) != cs2.charAt(i)) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
static int hash(@Nullable CharSequence cs) { | ||
if (cs == null) { | ||
return 0; | ||
} | ||
// this is safe as the hash code calculation is well defined | ||
// (see javadoc for String.hashCode()) | ||
if (cs instanceof String) return cs.hashCode(); | ||
int h = 0; | ||
for (int i = 0; i < cs.length(); i++) { | ||
h = 31 * h + cs.charAt(i); | ||
} | ||
return h; | ||
} | ||
|
||
@Override | ||
public void resetState() { | ||
keys.clear(); | ||
values.clear(); | ||
transactionName = null; | ||
transactionType = null; | ||
spanType = null; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.