001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.juneau.rest.server.tracing.otel;
018
019import static org.apache.juneau.commons.utils.Shorts.*;
020
021import java.util.*;
022
023import org.apache.juneau.http.tracing.TraceContextCarrier;
024
025import io.opentelemetry.context.propagation.*;
026
027/**
028 * {@link TextMapGetter} implementation that reads W3C trace-context fields from a neutral
029 * {@link TraceContextCarrier} — for example an MCP request's {@code params._meta} object
030 * — instead of HTTP request headers.
031 *
032 * <p>
033 * {@link OtelTracerHook} chains an extraction pass through this getter after an ordinary HTTP-header
034 * extraction (via {@link RestRequestTextMapGetter}) so an explicit carrier value wins while an
035 * absent/invalid one falls back to whatever the HTTP-header extraction already produced. This relies
036 * entirely on the standard {@link TextMapPropagator} contract &mdash; extraction that finds nothing
037 * usable for a given concern returns the supplied {@link io.opentelemetry.context.Context} unmodified
038 * &mdash; so no bespoke precedence-merging logic lives here or in {@link OtelTracerHook}.
039 *
040 * @since 10.0.0
041 */
042@SuppressWarnings({
043   "java:S6548" // Singleton pattern is intentional; INSTANCE is a stateless, thread-safe TextMapGetter implementation.
044})
045public final class TraceContextCarrierTextMapGetter implements TextMapGetter<TraceContextCarrier> {
046
047   /** Process-wide singleton instance. */
048   public static final TraceContextCarrierTextMapGetter INSTANCE = new TraceContextCarrierTextMapGetter();
049
050   private TraceContextCarrierTextMapGetter() {}
051
052   @Override /* TextMapGetter */
053   public Iterable<String> keys(TraceContextCarrier carrier) {
054      if (carrier == null)
055         return List.of();
056      var keys = carrier.keys();
057      return keys == null ? List.of() : keys;
058   }
059
060   @Override /* TextMapGetter */
061   public String get(TraceContextCarrier carrier, String key) {
062      if (carrier == null || key == null)
063         return null;
064      var value = carrier.get(key);
065      return ie(value) ? null : value;
066   }
067}