Skip to content
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

Move span operation to Scope from Tracer #8411

Merged
merged 6 commits into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Improve summary error message for invalid setting updates ([#4792](https://github.com/opensearch-project/OpenSearch/pull/4792))
- Pass localNode info to all plugins on node start ([#7919](https://github.com/opensearch-project/OpenSearch/pull/7919))
- Improved performance of parsing floating point numbers ([#7909](https://github.com/opensearch-project/OpenSearch/pull/7909))
- 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);
suranjay marked this conversation as resolved.
Show resolved Hide resolved
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