static void updateThreadCorrelationStorage()

in universal-profiling-integration/src/main/java/co/elastic/otel/ProfilerSharedMemoryWriter.java [93:122]


  static void updateThreadCorrelationStorage(Span newSpan) {
    ByteBuffer tls = UniversalProfilingCorrelation.getCurrentThreadStorage(true, TLS_STORAGE_SIZE);
    // tls might be null if unsupported or something went wrong on initialization
    if (tls != null) {
      // the valid flag is used to signal the host-agent that it is reading incomplete data
      tls.put(TLS_VALID_OFFSET, (byte) 0);
      memoryStoreStoreBarrier();
      tls.putChar(TLS_MINOR_VERSION_OFFSET, (char) 1);

      SpanContext spanCtx = newSpan.getSpanContext();
      if (spanCtx.isValid() && !spanCtx.isRemote()) {
        ReadableSpan localRoot = LocalRootSpan.getFor(newSpan);
        if (localRoot != null) {
          String localRootSpanId = localRoot.getSpanContext().getSpanId();
          tls.put(TLS_TRACE_PRESENT_OFFSET, (byte) 1);
          tls.put(TLS_TRACE_FLAGS_OFFSET, spanCtx.getTraceFlags().asByte());
          HexUtils.writeHexAsBinary(spanCtx.getTraceId(), 0, tls, TLS_TRACE_ID_OFFSET, 16);
          HexUtils.writeHexAsBinary(spanCtx.getSpanId(), 0, tls, TLS_SPAN_ID_OFFSET, 8);
          HexUtils.writeHexAsBinary(localRootSpanId, 0, tls, TLS_LOCAL_ROOT_SPAN_ID_OFFSET, 8);
        } else {
          tls.put(TLS_TRACE_PRESENT_OFFSET, (byte) 0);
          log.log(Level.WARNING, "Cannot propagate trace with unknown local root: {0}", newSpan);
        }
      } else {
        tls.put(TLS_TRACE_PRESENT_OFFSET, (byte) 0);
      }
      memoryStoreStoreBarrier();
      tls.put(TLS_VALID_OFFSET, (byte) 1);
    }
  }