All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.rocketmq.shaded.io.opentelemetry.sdk.logs.data.LogDataBuilder Maven / Gradle / Ivy

There is a newer version: 5.0.7
Show newest version
/*
 * Copyright The OpenTelemetry Authors
 * SPDX-License-Identifier: Apache-2.0
 */

package org.apache.rocketmq.shaded.io.opentelemetry.sdk.logs.data;

import org.apache.rocketmq.shaded.io.opentelemetry.api.common.Attributes;
import org.apache.rocketmq.shaded.io.opentelemetry.api.trace.Span;
import org.apache.rocketmq.shaded.io.opentelemetry.api.trace.SpanContext;
import org.apache.rocketmq.shaded.io.opentelemetry.context.Context;
import org.apache.rocketmq.shaded.io.opentelemetry.sdk.common.Clock;
import org.apache.rocketmq.shaded.io.opentelemetry.sdk.common.InstrumentationScopeInfo;
import org.apache.rocketmq.shaded.io.opentelemetry.sdk.resources.Resource;
import java.time.Instant;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nullable;

/** Builder for {@link LogData}. */
public final class LogDataBuilder {

  private final Resource resource;
  private final InstrumentationScopeInfo instrumentationScopeInfo;

  private long epochNanos;
  private SpanContext spanContext = SpanContext.getInvalid();
  private Severity severity = Severity.UNDEFINED_SEVERITY_NUMBER;
  @Nullable private String severityText;
  private Body body = Body.empty();
  private final Clock clock;
  private Attributes attributes = Attributes.empty();

  private LogDataBuilder(
      Resource resource, InstrumentationScopeInfo instrumentationScopeInfo, Clock clock) {
    this.resource = resource;
    this.instrumentationScopeInfo = instrumentationScopeInfo;
    this.clock = clock;
  }

  /** Returns a new {@link LogDataBuilder} with the default clock. */
  public static LogDataBuilder create(
      Resource resource, InstrumentationScopeInfo instrumentationScopeInfo) {
    return create(resource, instrumentationScopeInfo, Clock.getDefault());
  }

  /** Returns a new {@link LogDataBuilder}. */
  public static LogDataBuilder create(
      Resource resource, InstrumentationScopeInfo instrumentationScopeInfo, Clock clock) {
    return new LogDataBuilder(resource, instrumentationScopeInfo, clock);
  }

  /** Set the epoch timestamp using the timestamp and unit. */
  public LogDataBuilder setEpoch(long timestamp, TimeUnit unit) {
    this.epochNanos = unit.toNanos(timestamp);
    return this;
  }

  /** Set the epoch timestamp using the instant. */
  public LogDataBuilder setEpoch(Instant instant) {
    this.epochNanos = TimeUnit.SECONDS.toNanos(instant.getEpochSecond()) + instant.getNano();
    return this;
  }

  /** Sets the context. */
  public LogDataBuilder setContext(Context context) {
    return setSpanContext(Span.fromContext(context).getSpanContext());
  }

  /** Sets the span context. */
  public LogDataBuilder setSpanContext(SpanContext spanContext) {
    this.spanContext = spanContext == null ? SpanContext.getInvalid() : spanContext;
    return this;
  }

  /** Set the severity. */
  public LogDataBuilder setSeverity(Severity severity) {
    this.severity = severity;
    return this;
  }

  /** Set the severity text. */
  public LogDataBuilder setSeverityText(String severityText) {
    this.severityText = severityText;
    return this;
  }

  /** Set the body string. */
  public LogDataBuilder setBody(String body) {
    this.body = Body.string(body);
    return this;
  }

  /** Set the attributes. */
  public LogDataBuilder setAttributes(Attributes attributes) {
    this.attributes = attributes;
    return this;
  }

  /** Build a {@link LogData} instance from the configured properties. */
  public LogData build() {
    if (epochNanos == 0) {
      epochNanos = clock.now();
    }
    return LogDataImpl.create(
        resource,
        instrumentationScopeInfo,
        epochNanos,
        spanContext,
        severity,
        severityText,
        body,
        attributes);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy