|
| 1 | +package datadog.trace.bootstrap.instrumentation.api; |
| 2 | + |
| 3 | +import datadog.trace.api.TagMap; |
| 4 | + |
| 5 | +/** |
| 6 | + * A baked-once, frozen descriptor of a span's constant initial state — the per-decorator constants |
| 7 | + * (instrumentation name, span type, {@code span.kind}, component, …) that {@code |
| 8 | + * BaseDecorator.afterStart} otherwise stamps one entry at a time, per span. |
| 9 | + * |
| 10 | + * <p>Composed through {@link #builder()}: authors set identity and constant tags via typed methods |
| 11 | + * and never touch {@link TagMap} directly. {@link Builder#extends_(SpanPrototype)} inherits a base |
| 12 | + * prototype (e.g. a SpanType base like {@code HttpServer}) so an integration adds only what's |
| 13 | + * specific to it. Rides the existing {@code TagMap} API, so it's independent of any deeper TagMap |
| 14 | + * rework — the internal seed can get faster without changing this surface. |
| 15 | + * |
| 16 | + * <p>v1 carries identity + constant tags. Derivation / canonicalization / lifecycle hooks are |
| 17 | + * deliberately out — grown when the work that needs each arrives, not pre-slotted. |
| 18 | + */ |
| 19 | +public final class SpanPrototype { |
| 20 | + /** The empty prototype — for spans created without a decorator-provided prototype. */ |
| 21 | + public static final SpanPrototype NONE = builder().build(); |
| 22 | + |
| 23 | + public static Builder builder() { |
| 24 | + return new Builder(); |
| 25 | + } |
| 26 | + |
| 27 | + private final String instrumentationName; |
| 28 | + private final CharSequence operationName; |
| 29 | + private final CharSequence spanType; |
| 30 | + private final TagMap tags; // frozen |
| 31 | + |
| 32 | + private SpanPrototype(final Builder builder) { |
| 33 | + this.instrumentationName = builder.instrumentationName; |
| 34 | + this.operationName = builder.operationName; |
| 35 | + this.spanType = builder.spanType; |
| 36 | + this.tags = builder.tags.immutableCopy(); |
| 37 | + } |
| 38 | + |
| 39 | + public String instrumentationName() { |
| 40 | + return instrumentationName; |
| 41 | + } |
| 42 | + |
| 43 | + public CharSequence operationName() { |
| 44 | + return operationName; |
| 45 | + } |
| 46 | + |
| 47 | + public CharSequence spanType() { |
| 48 | + return spanType; |
| 49 | + } |
| 50 | + |
| 51 | + /** The frozen constant tags — the internal seed applied at span construction. */ |
| 52 | + public TagMap tags() { |
| 53 | + return tags; |
| 54 | + } |
| 55 | + |
| 56 | + public static final class Builder { |
| 57 | + private String instrumentationName; |
| 58 | + private CharSequence operationName; |
| 59 | + private CharSequence spanType; |
| 60 | + // Internal accumulator — never exposed; authors compose via the typed methods below. |
| 61 | + private final TagMap tags = TagMap.create(); |
| 62 | + |
| 63 | + private Builder() {} |
| 64 | + |
| 65 | + /** |
| 66 | + * Inherit a base prototype's identity and constant tags (e.g. a SpanType base). Subsequent |
| 67 | + * identity / {@code init*} calls on this builder override the inherited values. |
| 68 | + */ |
| 69 | + public Builder extends_(final SpanPrototype base) { |
| 70 | + if (base != null) { |
| 71 | + if (base.instrumentationName != null) { |
| 72 | + this.instrumentationName = base.instrumentationName; |
| 73 | + } |
| 74 | + if (base.operationName != null) { |
| 75 | + this.operationName = base.operationName; |
| 76 | + } |
| 77 | + if (base.spanType != null) { |
| 78 | + this.spanType = base.spanType; |
| 79 | + } |
| 80 | + this.tags.putAll(base.tags); |
| 81 | + } |
| 82 | + return this; |
| 83 | + } |
| 84 | + |
| 85 | + public Builder instrumentationName(final String[] instrumentationNames) { |
| 86 | + return (instrumentationNames == null || instrumentationNames.length == 0) |
| 87 | + ? this |
| 88 | + : instrumentationName(instrumentationNames[0]); |
| 89 | + } |
| 90 | + |
| 91 | + public Builder instrumentationName(final String instrumentationName) { |
| 92 | + this.instrumentationName = instrumentationName; |
| 93 | + return this; |
| 94 | + } |
| 95 | + |
| 96 | + public Builder operationName(final CharSequence operationName) { |
| 97 | + this.operationName = operationName; |
| 98 | + return this; |
| 99 | + } |
| 100 | + |
| 101 | + public Builder spanType(final CharSequence spanType) { |
| 102 | + this.spanType = spanType; |
| 103 | + return this; |
| 104 | + } |
| 105 | + |
| 106 | + /** Sets {@code span.kind}. */ |
| 107 | + public Builder initKind(final CharSequence kind) { |
| 108 | + return initTag(Tags.SPAN_KIND, kind); |
| 109 | + } |
| 110 | + |
| 111 | + /** Sets {@code component}. */ |
| 112 | + public Builder initComponent(final CharSequence component) { |
| 113 | + return initTag(Tags.COMPONENT, component); |
| 114 | + } |
| 115 | + |
| 116 | + public Builder initTag(final String key, final CharSequence value) { |
| 117 | + if (value != null) { |
| 118 | + this.tags.set(key, value); |
| 119 | + } |
| 120 | + return this; |
| 121 | + } |
| 122 | + |
| 123 | + public Builder initTag(final String key, final Object value) { |
| 124 | + if (value != null) { |
| 125 | + this.tags.set(key, value); |
| 126 | + } |
| 127 | + return this; |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Advanced/internal: reuse an already-built entry — a decorator's cached constant or a metric |
| 132 | + * entry — rather than re-creating it. Authors should prefer the typed {@code init*} methods. |
| 133 | + */ |
| 134 | + public Builder initTag(final TagMap.EntryReader entry) { |
| 135 | + if (entry != null) { |
| 136 | + this.tags.set(entry); |
| 137 | + } |
| 138 | + return this; |
| 139 | + } |
| 140 | + |
| 141 | + public SpanPrototype build() { |
| 142 | + return new SpanPrototype(this); |
| 143 | + } |
| 144 | + } |
| 145 | +} |
0 commit comments