forked from opensearch-project/OpenSearch
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
91 additions
and
1 deletion.
There are no files selected for viewing
90 changes: 90 additions & 0 deletions
90
plugins/telemetry-otel/src/main/java/org/opensearch/telemetry/tracing/FileSpanExporter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* 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 io.opentelemetry.sdk.common.CompletableResultCode; | ||
import io.opentelemetry.sdk.common.InstrumentationScopeInfo; | ||
import io.opentelemetry.sdk.trace.data.SpanData; | ||
import io.opentelemetry.sdk.trace.export.SpanExporter; | ||
import java.util.Collection; | ||
import java.util.concurrent.atomic.AtomicBoolean; | ||
import org.apache.logging.log4j.LogManager; | ||
import org.apache.logging.log4j.Logger; | ||
|
||
/** | ||
* Writes the span to a log file. This is mostly for testing where you can trace one request and all your spans | ||
* will be written to a file. Tracing log file would be available in the logs directory. | ||
*/ | ||
public class FileSpanExporter implements SpanExporter { | ||
/** | ||
* jfkdfj | ||
*/ | ||
public static final String TRACING_LOG_PREFIX = "tracing_log"; | ||
private static final Logger TRACING_LOGGER = LogManager.getLogger(TRACING_LOG_PREFIX); | ||
private final Logger DEFAULT_LOGGER = LogManager.getLogger(FileSpanExporter.class); | ||
private final String FIELD_SEPARATOR = "\t"; | ||
|
||
private final AtomicBoolean isShutdown = new AtomicBoolean(); | ||
|
||
/** | ||
* fjkdjf | ||
*/ | ||
public FileSpanExporter() { | ||
} | ||
|
||
@Override | ||
public CompletableResultCode export(Collection<SpanData> spans) { | ||
if (isShutdown.get()) { | ||
return CompletableResultCode.ofFailure(); | ||
} | ||
StringBuilder sb = new StringBuilder(128); | ||
for (SpanData span : spans) { | ||
sb.setLength(0); | ||
InstrumentationScopeInfo instrumentationScopeInfo = span.getInstrumentationScopeInfo(); | ||
sb.append("'") | ||
.append(span.getName()) | ||
.append("'") | ||
.append(FIELD_SEPARATOR) | ||
.append(span.getTraceId()) | ||
.append(FIELD_SEPARATOR) | ||
.append(span.getSpanId()) | ||
.append(FIELD_SEPARATOR) | ||
.append(span.getParentSpanId()) | ||
.append(FIELD_SEPARATOR) | ||
.append(span.getKind()) | ||
.append(FIELD_SEPARATOR) | ||
.append(span.getStartEpochNanos()) | ||
.append(FIELD_SEPARATOR) | ||
.append(span.getEndEpochNanos()) | ||
.append(FIELD_SEPARATOR) | ||
.append("[tracer:") | ||
.append(instrumentationScopeInfo.getName()) | ||
.append(":") | ||
.append(instrumentationScopeInfo.getVersion() == null ? "" : instrumentationScopeInfo.getVersion()) | ||
.append("]") | ||
.append(FIELD_SEPARATOR) | ||
.append(span.getAttributes()); | ||
// TRACING_LOGGER.info(sb.toString()); | ||
} | ||
return CompletableResultCode.ofSuccess(); | ||
} | ||
|
||
@Override | ||
public CompletableResultCode flush() { | ||
return CompletableResultCode.ofSuccess(); | ||
} | ||
|
||
@Override | ||
public CompletableResultCode shutdown() { | ||
if (!isShutdown.compareAndSet(false, true)) { | ||
DEFAULT_LOGGER.info("Duplicate shutdown() calls."); | ||
} | ||
return CompletableResultCode.ofSuccess(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters