|
| 1 | +package datadog.trace.common.writer; |
| 2 | + |
| 3 | +import datadog.trace.bootstrap.instrumentation.api.WriterConstants; |
| 4 | +import datadog.trace.core.DDSpan; |
| 5 | +import java.io.IOException; |
| 6 | +import java.io.ObjectOutputStream; |
| 7 | +import java.nio.file.Files; |
| 8 | +import java.nio.file.Path; |
| 9 | +import java.nio.file.Paths; |
| 10 | +import java.util.List; |
| 11 | + |
| 12 | +public class BinaryWriter implements Writer { |
| 13 | + private final Path output; |
| 14 | + private ObjectOutputStream outputStream; |
| 15 | + |
| 16 | + /** |
| 17 | + * Creates a {@link BinaryWriter} from {@link MultiWriter} configuration. |
| 18 | + * |
| 19 | + * @param configuration The {@link MultiWriter} configuration. |
| 20 | + * @return The created writer instance. |
| 21 | + */ |
| 22 | + public static Writer fromArguments(String configuration) { |
| 23 | + if (configuration == null) { |
| 24 | + throw new IllegalArgumentException("writer configuration is null"); |
| 25 | + } |
| 26 | + if (configuration.startsWith(WriterConstants.BINARY_WRITER_TYPE + ':')) { |
| 27 | + configuration = configuration.substring(WriterConstants.BINARY_WRITER_TYPE.length() + 1); |
| 28 | + } |
| 29 | + if (configuration.isEmpty()) { |
| 30 | + throw new IllegalArgumentException("no output configuration"); |
| 31 | + } |
| 32 | + return new BinaryWriter(Paths.get(configuration)); |
| 33 | + } |
| 34 | + |
| 35 | + public BinaryWriter(Path output) { |
| 36 | + this.output = output; |
| 37 | + start(); |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public void write(List<DDSpan> trace) { |
| 42 | + if (this.outputStream != null) { |
| 43 | + try { |
| 44 | + for (DDSpan span : trace) { |
| 45 | + this.outputStream.writeObject(span); |
| 46 | + } |
| 47 | + } catch (IOException e) { |
| 48 | + throw new RuntimeException("Failed to write trace", e); |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public void start() { |
| 55 | + checkOutputFolder(); |
| 56 | + try { |
| 57 | + this.outputStream = new ObjectOutputStream(Files.newOutputStream(this.output)); |
| 58 | + } catch (IOException e) { |
| 59 | + throw new RuntimeException(e); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + private void checkOutputFolder() { |
| 64 | + if (!Files.exists(this.output.getParent())) { |
| 65 | + try { |
| 66 | + Files.createFile(this.output.getParent()); |
| 67 | + } catch (IOException e) { |
| 68 | + throw new RuntimeException("Failed to create writer output directory", e); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public boolean flush() { |
| 75 | + if (this.outputStream != null) { |
| 76 | + try { |
| 77 | + this.outputStream.flush(); |
| 78 | + return true; |
| 79 | + } catch (IOException ignored) { |
| 80 | + } |
| 81 | + } |
| 82 | + return false; |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public void close() { |
| 87 | + this.outputStream = null; |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public void incrementDropCounts(int spanCount) { |
| 92 | + // Not supported |
| 93 | + } |
| 94 | +} |
0 commit comments