How to use the @opentelemetry/types.TraceFlags.UNSAMPLED function in @opentelemetry/types

To help you get started, we’ve selected a few @opentelemetry/types examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Azure / azure-sdk-for-js / sdk / core / core-tracing / lib / utils / traceParentHeader.ts View on Github external
export function getTraceParentHeader(spanContext: SpanContext): string | undefined {
  const missingFields: string[] = [];
  if (!spanContext.traceId) {
    missingFields.push("traceId");
  }
  if (!spanContext.spanId) {
    missingFields.push("spanId");
  }

  if (missingFields.length) {
    return;
  }

  const flags = spanContext.traceFlags || TraceFlags.UNSAMPLED;
  const hexFlags = flags.toString(16);
  const traceFlags = hexFlags.length === 1 ? `0${hexFlags}` : hexFlags;

  // https://www.w3.org/TR/trace-context/#traceparent-header-field-values
  return `${VERSION}-${spanContext.traceId}-${spanContext.spanId}-${traceFlags}`;
}
github open-telemetry / opentelemetry-js / packages / opentelemetry-tracing / src / BasicTracer.ts View on Github external
// make sampling decision
    const samplingDecision = this._sampler.shouldSample(parentContext);
    const spanId = randomSpanId();
    let traceId;
    let traceState;
    if (!parentContext || !isValid(parentContext)) {
      // New root span.
      traceId = randomTraceId();
    } else {
      // New child span.
      traceId = parentContext.traceId;
      traceState = parentContext.traceState;
    }
    const traceFlags = samplingDecision
      ? TraceFlags.SAMPLED
      : TraceFlags.UNSAMPLED;
    const spanContext = { traceId, spanId, traceFlags, traceState };
    const recordEvents = options.isRecording || false;
    if (!recordEvents && !samplingDecision) {
      this.logger.debug('Sampling is off, starting no recording span');
      return new NoRecordingSpan(spanContext);
    }

    const span = new Span(
      this,
      name,
      spanContext,
      options.kind || types.SpanKind.INTERNAL,
      parentContext ? parentContext.spanId : undefined,
      options.links || [],
      options.startTime
    );
github open-telemetry / opentelemetry-js / packages / opentelemetry-core / src / context / propagation / BinaryTraceContext.ts View on Github external
*/
    const traceId = spanContext.traceId;
    const spanId = spanContext.spanId;
    const buf = new Uint8Array(FORMAT_LENGTH);
    let j = TRACE_ID_OFFSET;
    for (let i = TRACE_ID_OFFSET; i < SPAN_ID_FIELD_ID_OFFSET; i++) {
      // tslint:disable-next-line:ban Needed to parse hexadecimal.
      buf[j++] = parseInt(traceId.substr((i - TRACE_ID_OFFSET) * 2, 2), 16);
    }
    buf[j++] = SPAN_ID_FIELD_ID;
    for (let i = SPAN_ID_OFFSET; i < TRACE_OPTION_FIELD_ID_OFFSET; i++) {
      // tslint:disable-next-line:ban Needed to parse hexadecimal.
      buf[j++] = parseInt(spanId.substr((i - SPAN_ID_OFFSET) * 2, 2), 16);
    }
    buf[j++] = TRACE_OPTION_FIELD_ID;
    buf[j++] = Number(spanContext.traceFlags) || TraceFlags.UNSAMPLED;
    return buf;
  }
github open-telemetry / opentelemetry-js / packages / opentelemetry-core / src / context / propagation / B3Format.ts View on Github external
if (!traceIdHeader || !spanIdHeader) return null;
    const traceId = Array.isArray(traceIdHeader)
      ? traceIdHeader[0]
      : traceIdHeader;
    const spanId = Array.isArray(spanIdHeader) ? spanIdHeader[0] : spanIdHeader;
    const options = Array.isArray(sampledHeader)
      ? sampledHeader[0]
      : sampledHeader;

    if (isValidTraceId(traceId) && isValidSpanId(spanId)) {
      return {
        traceId,
        spanId,
        isRemote: true,
        traceFlags: isNaN(Number(options))
          ? TraceFlags.UNSAMPLED
          : Number(options),
      };
    }
    return null;
  }
}
github open-telemetry / opentelemetry-js / packages / opentelemetry-core / src / trace / spancontext-utils.ts View on Github external
*
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { SpanContext, TraceFlags } from '@opentelemetry/types';

export const INVALID_SPANID = '0';
export const INVALID_TRACEID = '0';
export const INVALID_SPAN_CONTEXT: SpanContext = {
  traceId: INVALID_TRACEID,
  spanId: INVALID_SPANID,
  traceFlags: TraceFlags.UNSAMPLED,
};

/**
 * Returns true if this {@link SpanContext} is valid.
 * @return true if this {@link SpanContext} is valid.
 */
export function isValid(spanContext: SpanContext): boolean {
  return (
    spanContext.traceId !== INVALID_TRACEID &&
    spanContext.spanId !== INVALID_SPANID
  );
}
github open-telemetry / opentelemetry-js / packages / opentelemetry-core / src / context / propagation / HttpTraceContext.ts View on Github external
inject(
    spanContext: SpanContext,
    format: string,
    carrier: { [key: string]: unknown }
  ) {
    const traceParent = `${VERSION}-${spanContext.traceId}-${
      spanContext.spanId
    }-0${Number(spanContext.traceFlags || TraceFlags.UNSAMPLED).toString(16)}`;

    carrier[TRACE_PARENT_HEADER] = traceParent;
    if (spanContext.traceState) {
      carrier[TRACE_STATE_HEADER] = spanContext.traceState.serialize();
    }
  }