Skip to content

Commit

Permalink
Move span operation to Scope from Tracer (opensearch-project#8411)
Browse files Browse the repository at this point in the history
* Move span operation to Scope from Tracer

Signed-off-by: suranjay <[email protected]>

* Add changelog entry

Signed-off-by: suranjay <[email protected]>

* Change method to setError

Signed-off-by: suranjay <[email protected]>

* Removed unused classes

Signed-off-by: suranjay <[email protected]>

* Fix merge issue

Signed-off-by: suranjay <[email protected]>

---------

Signed-off-by: suranjay <[email protected]>
  • Loading branch information
suranjay committed Jul 7, 2023
1 parent 56a24bb commit 4bb25b4
Show file tree
Hide file tree
Showing 15 changed files with 309 additions and 224 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Move ZSTD compression codecs out of the sandbox ([#7908](https://github.com/opensearch-project/OpenSearch/pull/7908))
- Update ZSTD default compression level ([#8471](https://github.com/opensearch-project/OpenSearch/pull/8471))
- Improved performance of parsing floating point numbers ([#8467](https://github.com/opensearch-project/OpenSearch/pull/8467))
- Move span actions to Scope ([#8411](https://github.com/opensearch-project/OpenSearch/pull/8411))

### Deprecated

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.telemetry.tracing;

import java.util.function.Consumer;

/**
* Default implementation of Scope
*/
public class DefaultSpanScope implements SpanScope {

private final Span span;

private final Consumer<Span> onCloseConsumer;

/**
* Creates Scope instance for the given span
*
* @param span underlying span
* @param onCloseConsumer consumer to execute on scope close
*/
public DefaultSpanScope(Span span, Consumer<Span> onCloseConsumer) {
this.span = span;
this.onCloseConsumer = onCloseConsumer;
}

@Override
public void addSpanAttribute(String key, String value) {
span.addAttribute(key, value);
}

@Override
public void addSpanAttribute(String key, long value) {
span.addAttribute(key, value);
}

@Override
public void addSpanAttribute(String key, double value) {
span.addAttribute(key, value);
}

@Override
public void addSpanAttribute(String key, boolean value) {
span.addAttribute(key, value);
}

@Override
public void addSpanEvent(String event) {
span.addEvent(event);
}

@Override
public void setError(Exception exception) {
span.setError(exception);
}

/**
* Executes the runnable to end the scope
*/
@Override
public void close() {
onCloseConsumer.accept(span);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

/**
*
* The default tracer implementation. This class implements the basic logic for span lifecycle and its state management.
* It also handles tracing context propagation between spans.
* The default tracer implementation. It handles tracing context propagation between spans by maintaining
* current active span in its storage
*
*
*/
Expand All @@ -36,41 +36,11 @@ public DefaultTracer(TracingTelemetry tracingTelemetry, TracerContextStorage<Str
}

@Override
public Scope startSpan(String spanName) {
public SpanScope startSpan(String spanName) {
Span span = createSpan(spanName, getCurrentSpan());
setCurrentSpanInContext(span);
addDefaultAttributes(span);
return new ScopeImpl(() -> endSpan(span));
}

@Override
public void addSpanAttribute(String key, String value) {
Span currentSpan = getCurrentSpan();
currentSpan.addAttribute(key, value);
}

@Override
public void addSpanAttribute(String key, long value) {
Span currentSpan = getCurrentSpan();
currentSpan.addAttribute(key, value);
}

@Override
public void addSpanAttribute(String key, double value) {
Span currentSpan = getCurrentSpan();
currentSpan.addAttribute(key, value);
}

@Override
public void addSpanAttribute(String key, boolean value) {
Span currentSpan = getCurrentSpan();
currentSpan.addAttribute(key, value);
}

@Override
public void addSpanEvent(String event) {
Span currentSpan = getCurrentSpan();
currentSpan.addEvent(event);
return new DefaultSpanScope(span, (scopeSpan) -> endSpan(scopeSpan));
}

@Override
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ public interface Span {
*/
void addAttribute(String key, Boolean value);

/**
* Records error in the span
*
* @param exception exception to be recorded
*/
void setError(Exception exception);

/**
* Adds an event in the span
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.telemetry.tracing;

import org.opensearch.telemetry.tracing.noop.NoopSpanScope;

/**
* An auto-closeable that represents scope of the span.
* It provides interface for all the span operations.
*/
public interface SpanScope extends AutoCloseable {
/**
* No-op Scope implementation
*/
SpanScope NO_OP = new NoopSpanScope();

/**
* Adds string attribute to the {@link Span}.
*
* @param key attribute key
* @param value attribute value
*/
void addSpanAttribute(String key, String value);

/**
* Adds long attribute to the {@link Span}.
*
* @param key attribute key
* @param value attribute value
*/
void addSpanAttribute(String key, long value);

/**
* Adds double attribute to the {@link Span}.
*
* @param key attribute key
* @param value attribute value
*/
void addSpanAttribute(String key, double value);

/**
* Adds boolean attribute to the {@link Span}.
*
* @param key attribute key
* @param value attribute value
*/
void addSpanAttribute(String key, boolean value);

/**
* Adds an event to the {@link Span}.
*
* @param event event name
*/
void addSpanEvent(String event);

/**
* Records error in the span
*
* @param exception exception to be recorded
*/
void setError(Exception exception);

/**
* closes the scope
*/
@Override
void close();
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import java.io.Closeable;

/**
* Tracer is the interface used to create a {@link Span} and interact with current active {@link Span}.
* Tracer is the interface used to create a {@link Span}
* It automatically handles the context propagation between threads, tasks, nodes etc.
*
* All methods on the Tracer object are multi-thread safe.
Expand All @@ -24,44 +24,6 @@ public interface Tracer extends Closeable {
* @param spanName span name
* @return scope of the span, must be closed with explicit close or with try-with-resource
*/
Scope startSpan(String spanName);
SpanScope startSpan(String spanName);

/**
* Adds string attribute to the current active {@link Span}.
*
* @param key attribute key
* @param value attribute value
*/
void addSpanAttribute(String key, String value);

/**
* Adds long attribute to the current active {@link Span}.
*
* @param key attribute key
* @param value attribute value
*/
void addSpanAttribute(String key, long value);

/**
* Adds double attribute to the current active {@link Span}.
*
* @param key attribute key
* @param value attribute value
*/
void addSpanAttribute(String key, double value);

/**
* Adds boolean attribute to the current active {@link Span}.
*
* @param key attribute key
* @param value attribute value
*/
void addSpanAttribute(String key, boolean value);

/**
* Adds an event to the current active {@link Span}.
*
* @param event event name
*/
void addSpanEvent(String event);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/

package org.opensearch.telemetry.tracing.noop;

import org.opensearch.telemetry.tracing.SpanScope;

/**
* No-op implementation of SpanScope
*/
public final class NoopSpanScope implements SpanScope {

/**
* No-args constructor
*/
public NoopSpanScope() {}

@Override
public void addSpanAttribute(String key, String value) {

}

@Override
public void addSpanAttribute(String key, long value) {

}

@Override
public void addSpanAttribute(String key, double value) {

}

@Override
public void addSpanAttribute(String key, boolean value) {

}

@Override
public void addSpanEvent(String event) {

}

@Override
public void setError(Exception exception) {

}

@Override
public void close() {

}
}
Loading

0 comments on commit 4bb25b4

Please sign in to comment.