Implemented default LoggingExporter for trace api

This commit is contained in:
Sebastian Sdorra
2020-10-26 16:51:57 +01:00
parent e3a6111056
commit 09d85f6dbb
5 changed files with 171 additions and 17 deletions

View File

@@ -0,0 +1,78 @@
/*
* MIT License
*
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package sonia.scm.trace;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sonia.scm.plugin.Extension;
import javax.inject.Inject;
import java.util.Map;
import java.util.function.Consumer;
/**
* An {@link Exporter} which logs every collected span.
*
* @since 2.9.0
*/
@Extension
public final class LoggingExporter implements Exporter {
private static final Logger LOG = LoggerFactory.getLogger(LoggingExporter.class);
private final Consumer<String> logger;
@Inject
LoggingExporter() {
this(LOG::info);
}
LoggingExporter(Consumer<String> logger) {
this.logger = logger;
}
@Override
public void export(SpanContext span) {
logger.accept(format(span));
}
private String format(SpanContext span) {
StringBuilder message = new StringBuilder("received ");
if (span.isFailed()) {
message.append("failed ");
}
message.append(span.getKind()).append(" span, which took ");
message.append(span.duration().toMillis()).append("ms");
Map<String, String> labels = span.getLabels();
if (!labels.isEmpty()) {
message.append(":");
for (Map.Entry<String, String> e : labels.entrySet()) {
message.append("\n - ").append(e.getKey()).append(": ").append(e.getValue());
}
}
return message.toString();
}
}