Skip to content

Commit

Permalink
Rewritten for new context implementation
Browse files Browse the repository at this point in the history
Signed-off-by: Staffan Friberg <[email protected]>
  • Loading branch information
Staffan Friberg committed Oct 22, 2020
1 parent f4a1587 commit f5a1cb7
Show file tree
Hide file tree
Showing 11 changed files with 407 additions and 0 deletions.
4 changes: 4 additions & 0 deletions sdk_extensions/jfr_events/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
OpenTelemetry SDK Extension JFR Events
======================================================

* Java 11 compatible.
19 changes: 19 additions & 0 deletions sdk_extensions/jfr_events/build.gradle
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.
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();
}
};
}
}
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();
}
}
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;
}
}
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;
}
}
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;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.opentelemetry.sdk.extension.jfr.JfrContextStorageProvider
Loading

0 comments on commit f5a1cb7

Please sign in to comment.