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

com.couchbase.client.core.cnc.CbTracing Maven / Gradle / Ivy

There is a newer version: 2.7.0
Show newest version
/*
 * Copyright 2021 Couchbase, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * 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.
 */

package com.couchbase.client.core.cnc;

import com.couchbase.client.core.CoreContext;
import com.couchbase.client.core.annotation.Stability;
import com.couchbase.client.core.cnc.tracing.NoopRequestSpan;
import com.couchbase.client.core.cnc.tracing.NoopRequestTracer;
import com.couchbase.client.core.cnc.tracing.ThresholdLoggingTracer;
import com.couchbase.client.core.cnc.tracing.ThresholdRequestSpan;
import com.couchbase.client.core.service.ServiceType;

import java.util.EnumMap;
import java.util.Map;

import static java.util.Collections.unmodifiableMap;

@Stability.Internal
public class CbTracing {
  private CbTracing() {
    throw new AssertionError("not instantiable");
  }

  /**
   * Returns true if the tracer is an internal one (noop or threshold) so that particular
   * optimizations can be applied.
   *
   * @param tracer the tracer to check.
   * @return true if internal, false otherwise.
   */
  public static boolean isInternalTracer(final RequestTracer tracer) {
    return tracer instanceof NoopRequestTracer || tracer instanceof ThresholdLoggingTracer;
  }

  /**
   * Returns true if the span is an internal one (noop or threshold) so that particular
   * optimizations can be applied.
   *
   * @param span the span to check.
   * @return true if internal, false otherwise.
   */
  public static boolean isInternalSpan(final RequestSpan span) {
    return span instanceof NoopRequestSpan || span instanceof ThresholdRequestSpan;
  }

  /**
   * Returns a new span with the `db.system` attribute set to `couchbase`.
   * @param parent (nullable)
   */
  public static RequestSpan newSpan(CoreContext coreContext, String spanName, RequestSpan parent) {
    return newSpan(coreContext.environment().requestTracer(), spanName, parent);
  }

  /**
   * Returns a new span with the `db.system` attribute set to `couchbase`.
   *
   * @param parent (nullable)
   */
  public static RequestSpan newSpan(RequestTracer tracer, String spanName, RequestSpan parent) {
    RequestSpan span = tracer.requestSpan(spanName, parent);
    span.attribute(TracingIdentifiers.ATTR_SYSTEM, TracingIdentifiers.ATTR_SYSTEM_COUCHBASE);
    return span;
  }

  /**
   * @param span (nullable)
   * @param attributes (nullable)
   */
  public static void setAttributes(RequestSpan span, Map attributes) {
    if (span == null || attributes == null) {
      return;
    }
    attributes.forEach((k, v) -> {
      if (v instanceof String) {
        span.attribute(k, (String) v);
      } else if (v instanceof Integer || v instanceof Long) {
        span.attribute(k, ((Number) v).longValue());
      } else if (v instanceof Boolean) {
        span.attribute(k, (Boolean) v);
      } else {
        span.attribute(k, String.valueOf(v));
      }
    });
  }

  private static final Map serviceTypeToTracingId;

  static {
    Map map = new EnumMap<>(ServiceType.class);
    map.put(ServiceType.ANALYTICS, TracingIdentifiers.SERVICE_ANALYTICS);
    map.put(ServiceType.EVENTING, TracingIdentifiers.SERVICE_EVENTING);
    map.put(ServiceType.KV, TracingIdentifiers.SERVICE_KV);
    map.put(ServiceType.MANAGER, TracingIdentifiers.SERVICE_MGMT);
    map.put(ServiceType.QUERY, TracingIdentifiers.SERVICE_QUERY);
    map.put(ServiceType.SEARCH, TracingIdentifiers.SERVICE_SEARCH);
    map.put(ServiceType.VIEWS, TracingIdentifiers.SERVICE_VIEWS);

    serviceTypeToTracingId = unmodifiableMap(map);
  }

  public static String getTracingId(ServiceType serviceType) {
    return serviceTypeToTracingId.getOrDefault(serviceType, serviceType.ident());
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy