All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
io.trino.spooling.filesystem.TracingSpoolingManager Maven / Gradle / Ivy
/*
* 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.trino.spooling.filesystem;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.StatusCode;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.semconv.ExceptionAttributes;
import io.trino.spi.protocol.SpooledLocation;
import io.trino.spi.protocol.SpooledLocation.DirectLocation;
import io.trino.spi.protocol.SpooledSegmentHandle;
import io.trino.spi.protocol.SpoolingContext;
import io.trino.spi.protocol.SpoolingManager;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Optional;
import static io.opentelemetry.api.common.AttributeKey.stringKey;
import static java.util.Objects.requireNonNull;
public class TracingSpoolingManager
implements SpoolingManager
{
public static final AttributeKey SEGMENT_ID = stringKey("trino.segment.id");
public static final AttributeKey SEGMENT_QUERY_ID = stringKey("trino.segment.query_id");
public static final AttributeKey SEGMENT_ENCODING = stringKey("trino.segment.encoding");
private final Tracer tracer;
private final SpoolingManager delegate;
TracingSpoolingManager(Tracer tracer, SpoolingManager delegate)
{
this.tracer = requireNonNull(tracer, "tracer is null");
this.delegate = requireNonNull(delegate, "delegate is null");
}
@Override
public SpooledSegmentHandle create(SpoolingContext context)
{
Span span = tracer.spanBuilder("SpoolingManager.create")
.setAttribute(SEGMENT_QUERY_ID, context.queryId().toString())
.setAttribute(SEGMENT_ENCODING, context.encoding())
.startSpan();
return withTracing(span, () -> delegate.create(context));
}
@Override
public OutputStream createOutputStream(SpooledSegmentHandle handle)
throws IOException
{
Span span = tracer.spanBuilder("SpoolingManager.createOutputStream")
.setAttribute(SEGMENT_QUERY_ID, handle.queryId().toString())
.setAttribute(SEGMENT_ID, handle.identifier())
.setAttribute(SEGMENT_ENCODING, handle.encoding())
.startSpan();
return withTracing(span, () -> delegate.createOutputStream(handle));
}
@Override
public InputStream openInputStream(SpooledSegmentHandle handle)
throws IOException
{
Span span = tracer.spanBuilder("SpoolingManager.openInputStream")
.setAttribute(SEGMENT_QUERY_ID, handle.queryId().toString())
.setAttribute(SEGMENT_ID, handle.identifier())
.setAttribute(SEGMENT_ENCODING, handle.encoding())
.startSpan();
return withTracing(span, () -> delegate.openInputStream(handle));
}
@Override
public void acknowledge(SpooledSegmentHandle handle)
throws IOException
{
Span span = tracer.spanBuilder("SpoolingManager.acknowledge")
.setAttribute(SEGMENT_QUERY_ID, handle.queryId().toString())
.setAttribute(SEGMENT_ID, handle.identifier())
.setAttribute(SEGMENT_ENCODING, handle.encoding())
.startSpan();
withTracing(span, () -> delegate.acknowledge(handle));
}
@Override
public Optional directLocation(SpooledSegmentHandle handle)
throws IOException
{
Span span = tracer.spanBuilder("SpoolingManager.directLocation")
.setAttribute(SEGMENT_QUERY_ID, handle.queryId().toString())
.setAttribute(SEGMENT_ID, handle.identifier())
.setAttribute(SEGMENT_ENCODING, handle.encoding())
.startSpan();
return withTracing(span, () -> delegate.directLocation(handle));
}
// Methods below do not need to be traced as they are not doing any I/O
@Override
public SpooledLocation location(SpooledSegmentHandle handle)
{
return delegate.location(handle);
}
@Override
public SpooledSegmentHandle handle(SpooledLocation location)
{
return delegate.handle(location);
}
public static void withTracing(Span span, CheckedRunnable runnable)
throws E
{
withTracing(span, () -> {
runnable.run();
return null;
});
}
public static T withTracing(Span span, CheckedSupplier supplier)
throws E
{
try (var _ = span.makeCurrent()) {
return supplier.get();
}
catch (Throwable t) {
span.setStatus(StatusCode.ERROR, t.getMessage());
span.recordException(t, Attributes.of(ExceptionAttributes.EXCEPTION_ESCAPED, true));
throw t;
}
finally {
span.end();
}
}
public interface CheckedRunnable
{
void run()
throws E;
}
public interface CheckedSupplier
{
T get()
throws E;
}
}