-
Notifications
You must be signed in to change notification settings - Fork 910
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Log current thread id and name as span attribute
* Bump opentelementry-java versions * Fix all compilation issues * Add a new `SpanProcessor` that appends thread attributes to each span
- Loading branch information
mateuszrzeszutek
committed
Aug 27, 2020
1 parent
7969a83
commit dc68094
Showing
4 changed files
with
103 additions
and
4 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
...oling/src/main/java/io/opentelemetry/javaagent/tooling/AddThreadDetailsSpanProcessor.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,50 @@ | ||
/* | ||
* Copyright The 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.javaagent.tooling; | ||
|
||
import io.opentelemetry.sdk.trace.ReadWriteSpan; | ||
import io.opentelemetry.sdk.trace.ReadableSpan; | ||
import io.opentelemetry.sdk.trace.SpanProcessor; | ||
import io.opentelemetry.trace.attributes.SemanticAttributes; | ||
|
||
public class AddThreadDetailsSpanProcessor implements SpanProcessor { | ||
@Override | ||
public void onStart(ReadWriteSpan span) { | ||
Thread currentThread = Thread.currentThread(); | ||
SemanticAttributes.THREAD_ID.set(span, currentThread.getId()); | ||
SemanticAttributes.THREAD_NAME.set(span, currentThread.getName()); | ||
} | ||
|
||
@Override | ||
public boolean isStartRequired() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public void onEnd(ReadableSpan span) {} | ||
|
||
@Override | ||
public boolean isEndRequired() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public void shutdown() {} | ||
|
||
@Override | ||
public void forceFlush() {} | ||
} |
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
45 changes: 45 additions & 0 deletions
45
...c/test/groovy/io/opentelemetry/javaagent/tooling/AddThreadDetailsSpanProcessorTest.groovy
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,45 @@ | ||
/* | ||
* Copyright The 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.javaagent.tooling | ||
|
||
import io.opentelemetry.sdk.trace.ReadWriteSpan | ||
import io.opentelemetry.trace.attributes.SemanticAttributes | ||
import spock.lang.Specification | ||
|
||
class AddThreadDetailsSpanProcessorTest extends Specification { | ||
def span = Mock(ReadWriteSpan) | ||
|
||
def processor = new AddThreadDetailsSpanProcessor() | ||
|
||
def "should require onStart call"() { | ||
expect: | ||
processor.isStartRequired() | ||
} | ||
|
||
def "should set thread attributes on span start"() { | ||
given: | ||
def currentThreadName = Thread.currentThread().name | ||
def currentThreadId = Thread.currentThread().id | ||
|
||
when: | ||
processor.onStart(span) | ||
|
||
then: | ||
1 * span.setAttribute(SemanticAttributes.THREAD_ID.key(), currentThreadId) | ||
1 * span.setAttribute(SemanticAttributes.THREAD_NAME.key(), currentThreadName) | ||
} | ||
} |
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