|
| 1 | +package datadog.trace.instrumentation.spark; |
| 2 | + |
| 3 | +import datadog.trace.api.DDSpanId; |
| 4 | +import datadog.trace.api.DDTraceId; |
| 5 | +import datadog.trace.api.sampling.PrioritySampling; |
| 6 | +import datadog.trace.bootstrap.instrumentation.api.AgentSpan; |
| 7 | +import datadog.trace.bootstrap.instrumentation.api.AgentTraceCollector; |
| 8 | +import datadog.trace.bootstrap.instrumentation.api.AgentTracer; |
| 9 | +import datadog.trace.bootstrap.instrumentation.api.PathwayContext; |
| 10 | +import java.nio.ByteBuffer; |
| 11 | +import java.nio.charset.StandardCharsets; |
| 12 | +import java.security.MessageDigest; |
| 13 | +import java.security.NoSuchAlgorithmException; |
| 14 | +import java.util.Collections; |
| 15 | +import java.util.Map; |
| 16 | +import java.util.Optional; |
| 17 | +import java.util.regex.Pattern; |
| 18 | +import org.apache.spark.SparkConf; |
| 19 | +import org.slf4j.Logger; |
| 20 | +import org.slf4j.LoggerFactory; |
| 21 | + |
| 22 | +public class OpenlineageParentContext implements AgentSpan.Context { |
| 23 | + private static final Logger log = LoggerFactory.getLogger(OpenlineageParentContext.class); |
| 24 | + private static final Pattern UUID = |
| 25 | + Pattern.compile( |
| 26 | + "^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$"); |
| 27 | + |
| 28 | + private final DDTraceId traceId; |
| 29 | + private final long spanId; |
| 30 | + private final long childRootSpanId; |
| 31 | + |
| 32 | + private final String parentJobNamespace; |
| 33 | + private final String parentJobName; |
| 34 | + private final String parentRunId; |
| 35 | + |
| 36 | + public static final String OPENLINEAGE_PARENT_JOB_NAMESPACE = |
| 37 | + "spark.openlineage.parentJobNamespace"; |
| 38 | + public static final String OPENLINEAGE_PARENT_JOB_NAME = "spark.openlineage.parentJobName"; |
| 39 | + public static final String OPENLINEAGE_PARENT_RUN_ID = "spark.openlineage.parentRunId"; |
| 40 | + |
| 41 | + public static Optional<OpenlineageParentContext> from(SparkConf sparkConf) { |
| 42 | + if (!sparkConf.contains(OPENLINEAGE_PARENT_JOB_NAMESPACE) |
| 43 | + || !sparkConf.contains(OPENLINEAGE_PARENT_JOB_NAME) |
| 44 | + || !sparkConf.contains(OPENLINEAGE_PARENT_RUN_ID)) { |
| 45 | + return Optional.empty(); |
| 46 | + } |
| 47 | + |
| 48 | + String parentJobNamespace = sparkConf.get(OPENLINEAGE_PARENT_JOB_NAMESPACE); |
| 49 | + String parentJobName = sparkConf.get(OPENLINEAGE_PARENT_JOB_NAME); |
| 50 | + String parentRunId = sparkConf.get(OPENLINEAGE_PARENT_RUN_ID); |
| 51 | + |
| 52 | + if (!UUID.matcher(parentRunId).matches()) { |
| 53 | + return Optional.empty(); |
| 54 | + } |
| 55 | + |
| 56 | + return Optional.of( |
| 57 | + new OpenlineageParentContext(parentJobNamespace, parentJobName, parentRunId)); |
| 58 | + } |
| 59 | + |
| 60 | + OpenlineageParentContext(String parentJobNamespace, String parentJobName, String parentRunId) { |
| 61 | + log.debug( |
| 62 | + "Creating OpenlineageParentContext with parentJobNamespace: {}, parentJobName: {}, parentRunId: {}", |
| 63 | + parentJobNamespace, |
| 64 | + parentJobName, |
| 65 | + parentRunId); |
| 66 | + |
| 67 | + this.parentJobNamespace = parentJobNamespace; |
| 68 | + this.parentJobName = parentJobName; |
| 69 | + this.parentRunId = parentRunId; |
| 70 | + |
| 71 | + MessageDigest digest = null; |
| 72 | + try { |
| 73 | + digest = MessageDigest.getInstance("SHA-256"); |
| 74 | + } catch (NoSuchAlgorithmException e) { |
| 75 | + log.debug("Unable to find SHA-256 algorithm", e); |
| 76 | + } |
| 77 | + |
| 78 | + if (digest != null && parentJobNamespace != null && parentRunId != null) { |
| 79 | + traceId = computeTraceId(digest, parentJobNamespace, parentJobName, parentRunId); |
| 80 | + spanId = DDSpanId.ZERO; |
| 81 | + |
| 82 | + childRootSpanId = |
| 83 | + computeChildRootSpanId(digest, parentJobNamespace, parentJobName, parentRunId); |
| 84 | + } else { |
| 85 | + traceId = DDTraceId.ZERO; |
| 86 | + spanId = DDSpanId.ZERO; |
| 87 | + |
| 88 | + childRootSpanId = DDSpanId.ZERO; |
| 89 | + } |
| 90 | + |
| 91 | + log.debug("Created OpenlineageParentContext with traceId: {}, spanId: {}", traceId, spanId); |
| 92 | + } |
| 93 | + |
| 94 | + private long computeChildRootSpanId( |
| 95 | + MessageDigest digest, String parentJobNamespace, String parentJobName, String parentRunId) { |
| 96 | + byte[] inputBytes = |
| 97 | + (parentJobNamespace + parentJobName + parentRunId).getBytes(StandardCharsets.UTF_8); |
| 98 | + byte[] hash = digest.digest(inputBytes); |
| 99 | + |
| 100 | + return ByteBuffer.wrap(hash).getLong(); |
| 101 | + } |
| 102 | + |
| 103 | + private DDTraceId computeTraceId( |
| 104 | + MessageDigest digest, String parentJobNamespace, String parentJobName, String parentRunId) { |
| 105 | + byte[] inputBytes = |
| 106 | + (parentJobNamespace + parentJobName + parentRunId).getBytes(StandardCharsets.UTF_8); |
| 107 | + byte[] hash = digest.digest(inputBytes); |
| 108 | + |
| 109 | + return DDTraceId.from(ByteBuffer.wrap(hash).getLong()); |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public DDTraceId getTraceId() { |
| 114 | + return traceId; |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public long getSpanId() { |
| 119 | + return spanId; |
| 120 | + } |
| 121 | + |
| 122 | + public long getChildRootSpanId() { |
| 123 | + return childRootSpanId; |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public AgentTraceCollector getTraceCollector() { |
| 128 | + return AgentTracer.NoopAgentTraceCollector.INSTANCE; |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public int getSamplingPriority() { |
| 133 | + return PrioritySampling.USER_KEEP; |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + public Iterable<Map.Entry<String, String>> baggageItems() { |
| 138 | + return Collections.<String, String>emptyMap().entrySet(); |
| 139 | + } |
| 140 | + |
| 141 | + @Override |
| 142 | + public PathwayContext getPathwayContext() { |
| 143 | + return null; |
| 144 | + } |
| 145 | + |
| 146 | + public String getParentJobNamespace() { |
| 147 | + return parentJobNamespace; |
| 148 | + } |
| 149 | + |
| 150 | + public String getParentJobName() { |
| 151 | + return parentJobName; |
| 152 | + } |
| 153 | + |
| 154 | + public String getParentRunId() { |
| 155 | + return parentRunId; |
| 156 | + } |
| 157 | +} |
0 commit comments