-
Notifications
You must be signed in to change notification settings - Fork 848
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rewritten for new context implementation
Signed-off-by: Staffan Friberg <[email protected]>
- Loading branch information
Staffan Friberg
committed
Oct 22, 2020
1 parent
f4a1587
commit f5a1cb7
Showing
11 changed files
with
407 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,4 @@ | ||
OpenTelemetry SDK Extension JFR Events | ||
====================================================== | ||
|
||
* Java 11 compatible. |
This file contains 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,19 @@ | ||
plugins { | ||
id 'java' | ||
|
||
id "ru.vyarus.animalsniffer" | ||
} | ||
|
||
description = 'OpenTelemetry SDK Extension JFR' | ||
ext.moduleName = 'io.opentelemetry.sdk.extension.jfr' | ||
|
||
dependencies { | ||
implementation project(':opentelemetry-api'), | ||
project(':opentelemetry-sdk') | ||
|
||
signature "org.codehaus.mojo.signature:java18:1.0@signature" | ||
} | ||
|
||
tasks.withType(JavaCompile) { | ||
it.options.release = 11 | ||
} |
Empty file.
47 changes: 47 additions & 0 deletions
47
...fr_events/src/main/java/io/opentelemetry/sdk/extension/jfr/JfrContextStorageProvider.java
This file contains 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,47 @@ | ||
/* | ||
* 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.extension.jfr; | ||
|
||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.context.ContextStorage; | ||
import io.opentelemetry.context.ContextStorageProvider; | ||
import io.opentelemetry.context.Scope; | ||
import io.opentelemetry.trace.Span; | ||
|
||
public class JfrContextStorageProvider implements ContextStorageProvider { | ||
|
||
@Override | ||
public ContextStorage get() { | ||
ContextStorage parentStorage = ContextStorage.get(); | ||
return new ContextStorage() { | ||
@Override | ||
public Scope attach(Context toAttach) { | ||
Scope scope = parentStorage.attach(toAttach); | ||
ScopeEvent event = new ScopeEvent(Span.fromContext(toAttach).getSpanContext()); | ||
event.begin(); | ||
return () -> { | ||
event.commit(); | ||
scope.close(); | ||
}; | ||
} | ||
|
||
@Override | ||
public Context current() { | ||
return parentStorage.current(); | ||
} | ||
}; | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
...ensions/jfr_events/src/main/java/io/opentelemetry/sdk/extension/jfr/JfrSpanProcessor.java
This file contains 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,79 @@ | ||
/* | ||
* 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.extension.jfr; | ||
|
||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.sdk.common.CompletableResultCode; | ||
import io.opentelemetry.sdk.trace.ReadWriteSpan; | ||
import static java.util.Objects.nonNull; | ||
|
||
import io.opentelemetry.sdk.trace.ReadableSpan; | ||
import io.opentelemetry.sdk.trace.SpanProcessor; | ||
import io.opentelemetry.trace.SpanContext; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
/** | ||
* Span processor to create new JFR events for the Span as they are started, and | ||
* commit on end. | ||
* | ||
* <p> | ||
* NOTE: JfrSpanProcessor must be running synchronously to ensure that duration | ||
* is correctly captured. | ||
*/ | ||
public class JfrSpanProcessor implements SpanProcessor { | ||
|
||
private final Map<SpanContext, SpanEvent> spanEvents = new ConcurrentHashMap<>(); | ||
|
||
@Override | ||
public void onStart(ReadWriteSpan span, Context parentContext) { | ||
if (span.getSpanContext().isValid()) { | ||
SpanEvent event = new SpanEvent(span.toSpanData()); | ||
event.begin(); | ||
spanEvents.put(span.getSpanContext(), event); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean isStartRequired() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public void onEnd(ReadableSpan rs) { | ||
SpanEvent event = spanEvents.remove(rs.getSpanContext()); | ||
if (nonNull(event) && event.shouldCommit()) { | ||
event.commit(); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean isEndRequired() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public CompletableResultCode shutdown() { | ||
spanEvents.forEach((id, event) -> event.commit()); | ||
spanEvents.clear(); | ||
return CompletableResultCode.ofSuccess(); | ||
} | ||
|
||
@Override | ||
public CompletableResultCode forceFlush() { | ||
return CompletableResultCode.ofSuccess(); | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
sdk_extensions/jfr_events/src/main/java/io/opentelemetry/sdk/extension/jfr/ScopeEvent.java
This file contains 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,52 @@ | ||
/* | ||
* 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.extension.jfr; | ||
|
||
import io.opentelemetry.trace.SpanContext; | ||
import jdk.jfr.Category; | ||
import jdk.jfr.Description; | ||
import jdk.jfr.Event; | ||
import jdk.jfr.Label; | ||
import jdk.jfr.Name; | ||
|
||
@Name("io.opentelemetry.context.Scope") | ||
@Label("Scope") | ||
@Category("Open Telemetry Tracing") | ||
@Description( | ||
"Open Telemetry trace event corresponding to the span currently " | ||
+ "in scope/active on this thread.") | ||
class ScopeEvent extends Event { | ||
|
||
@Label("Trace Id") | ||
private final String traceId; | ||
|
||
@Label("Span Id") | ||
private final String spanId; | ||
|
||
ScopeEvent(SpanContext spanContext) { | ||
this.traceId = spanContext.getTraceIdAsHexString(); | ||
this.spanId = spanContext.getSpanIdAsHexString(); | ||
} | ||
|
||
public String getTraceId() { | ||
return traceId; | ||
} | ||
|
||
public String getSpanId() { | ||
return spanId; | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
sdk_extensions/jfr_events/src/main/java/io/opentelemetry/sdk/extension/jfr/SpanEvent.java
This file contains 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,66 @@ | ||
/* | ||
* 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.extension.jfr; | ||
|
||
import io.opentelemetry.sdk.trace.data.SpanData; | ||
import jdk.jfr.Category; | ||
import jdk.jfr.Description; | ||
import jdk.jfr.Event; | ||
import jdk.jfr.Label; | ||
import jdk.jfr.Name; | ||
|
||
@Label("Span") | ||
@Name("io.opentelemetry.trace.Span") | ||
@Category("Open Telemetry Tracing") | ||
@Description("Open Telemetry trace event corresponding to a span.") | ||
class SpanEvent extends Event { | ||
|
||
SpanEvent(SpanData spanData) { | ||
this.operationName = spanData.getName(); | ||
this.traceId = spanData.getTraceId(); | ||
this.spanId = spanData.getSpanId(); | ||
this.parentId = spanData.getParentSpanId(); | ||
} | ||
|
||
@Label("Operation Name") | ||
private final String operationName; | ||
|
||
@Label("Trace Id") | ||
private final String traceId; | ||
|
||
@Label("Span Id") | ||
private final String spanId; | ||
|
||
@Label("Parent Id") | ||
private final String parentId; | ||
|
||
public String getOperationName() { | ||
return operationName; | ||
} | ||
|
||
public String getTraceId() { | ||
return traceId; | ||
} | ||
|
||
public String getSpanId() { | ||
return spanId; | ||
} | ||
|
||
public String getParentId() { | ||
return parentId; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
sdk_extensions/jfr_events/src/main/java/io/opentelemetry/sdk/extension/jfr/package-info.java
This file contains 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,14 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
/** | ||
* Capture Spans and Scopes as events in JFR recordings. | ||
* | ||
* @see io.opentelemetry.sdk.extension.jfr.JfrSpanProcessor | ||
*/ | ||
@ParametersAreNonnullByDefault | ||
package io.opentelemetry.sdk.extension.jfr; | ||
|
||
import javax.annotation.ParametersAreNonnullByDefault; |
1 change: 1 addition & 0 deletions
1
...ents/src/main/resources/META-INF/services/io.opentelemetry.context.ContextStorageProvider
This file contains 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 @@ | ||
io.opentelemetry.sdk.extension.jfr.JfrContextStorageProvider |
Oops, something went wrong.