io.helidon.tracing.NoOpTracer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of helidon-tracing Show documentation
Show all versions of helidon-tracing Show documentation
Support for distributed tracing.
The newest version!
/*
* Copyright (c) 2022, 2024 Oracle and/or its affiliates.
*
* 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.helidon.tracing;
import java.time.Instant;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
class NoOpTracer implements Tracer {
private static final NoOpTracer INSTANCE = new NoOpTracer();
private static final SpanContext SPAN_CONTEXT = new SpanContext();
private static final Builder BUILDER = new Builder();
private static final Span SPAN = new Span();
private static final Scope SCOPE = new Scope();
private static final WritableBaggage EMPTY_BAGGAGE = new WritableBaggage() {
@Override
public Optional get(String key) {
return Optional.empty();
}
@Override
public Set keys() {
return Set.of();
}
@Override
public boolean containsKey(String key) {
return false;
}
@Override
public WritableBaggage set(String key, String value) {
return this;
}
};
private NoOpTracer() {
}
static Tracer instance() {
return INSTANCE;
}
@Override
public boolean enabled() {
return false;
}
@Override
public Builder spanBuilder(String name) {
return BUILDER;
}
@Override
public Optional extract(HeaderProvider headersProvider) {
return Optional.empty();
}
@Override
public void inject(io.helidon.tracing.SpanContext spanContext,
HeaderProvider inboundHeadersProvider,
HeaderConsumer outboundHeadersConsumer) {
}
@Override
public T unwrap(Class tracerClass) {
if (tracerClass.isInstance(this)) {
return tracerClass.cast(this);
}
throw new IllegalArgumentException("Cannot provide an instance of " + tracerClass.getName()
+ ", tracer is: " + getClass().getName());
}
@Override
public Tracer register(SpanListener listener) {
return this;
}
private static class Builder implements Span.Builder {
@Override
public Span build() {
return SPAN;
}
@Override
public Builder parent(io.helidon.tracing.SpanContext spanContext) {
return this;
}
@Override
public Builder kind(Span.Kind kind) {
return this;
}
@Override
public Builder tag(String key, String value) {
return this;
}
@Override
public Builder tag(String key, Boolean value) {
return this;
}
@Override
public Builder tag(String key, Number value) {
return this;
}
@Override
public Span start(Instant instant) {
return SPAN;
}
@Override
public T unwrap(Class type) {
return type.cast(this);
}
}
private static class Span implements io.helidon.tracing.Span {
@Override
public io.helidon.tracing.Span tag(String key, String value) {
return this;
}
@Override
public io.helidon.tracing.Span tag(String key, Boolean value) {
return this;
}
@Override
public io.helidon.tracing.Span tag(String key, Number value) {
return this;
}
@Override
public void status(Status status) {
}
@Override
public SpanContext context() {
return SPAN_CONTEXT;
}
@Override
public void addEvent(String name, Map attributes) {
}
@Override
public void end() {
}
@Override
public void end(Throwable t) {
}
@Override
public Scope activate() {
return SCOPE;
}
@Override
public Span baggage(String key, String value) {
return this;
}
@Override
public Optional baggage(String key) {
return Optional.empty();
}
@Override
public T unwrap(Class spanClass) {
return spanClass.cast(this);
}
@Override
public WritableBaggage baggage() {
return EMPTY_BAGGAGE;
}
}
private static class SpanContext implements io.helidon.tracing.SpanContext {
@Override
public String traceId() {
return "no-op";
}
@Override
public String spanId() {
return "no-op";
}
@Override
public void asParent(io.helidon.tracing.Span.Builder> spanBuilder) {
}
@Override
public Baggage baggage() {
return EMPTY_BAGGAGE;
}
}
private static class Scope implements io.helidon.tracing.Scope {
@Override
public void close() {
}
@Override
public boolean isClosed() {
return true;
}
}
}