// Copyright 2021-2023 Bloomberg Finance L.P.
// SPDX-License-Identifier: Apache-2.0
//
// 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.

// bmqpi_dtspan.h                                                     -*-C++-*-
#ifndef INCLUDED_BMQPI_DTSPAN
#define INCLUDED_BMQPI_DTSPAN

//@PURPOSE: Provide an interface representing a span of a distributed trace.
//
//@CLASSES:
//  bmqpi::DTSpan: Interface for a span of a distributed trace.
//  bmqpi::DTSpan::Baggage: A set of key-values used to describe a 'DTSpan'.
//
//@DESCRIPTION:
// 'bmqpi::DTSpan' is a pure interface representing a node within a distributed
// trace graph (which forms a DAG with edges as invocations).
//
// 'bmqpi::DTSpan::Baggage' represents a set of key-values used to describe
// metadata belonging to a 'DTSpan'. The phrase "baggage" is borrowed from the
// OpenTelemetry standard's terminology.

// BMQ
#include <bmqscm_version.h>

// BDE
#include <bsl_unordered_map.h>
#include <bsl_string.h>
#include <bsl_string_view.h>
#include <bslma_allocator.h>

namespace BloombergLP {
namespace bmqpi {

                                // ============
                                // class DTSpan
                                // ============

class DTSpan {
    // A pure interface for representing a span of a distributed trace.

  public:
    // PUBLIC CREATORS
    virtual ~DTSpan();
        // Destructor

    // PUBLIC ACCESSORS
    virtual bsl::string_view operation() const = 0;
        // Returns the name of the operation that this 'DTSpan' represents.

                               // =============
                               // class Baggage
                               // =============


    // PUBLIC INNER CLASSES
    class Baggage BSLS_KEYWORD_FINAL {
        // A set of key-values used to describe a 'DTSpan'.

      private:
        // PRIVATE TYPEDEFS
        typedef bsl::unordered_map<bsl::string, bsl::string> MapType;

        // PRIVATE DATA
        MapType d_data;

      public:
        // PUBLIC TYPES
        typedef MapType::const_iterator const_iterator;

        // PUBLIC CREATORS
        Baggage(bslma::Allocator *allocator = 0);

        // PUBLIC ACCESSORS
        const_iterator begin() const;
            // Returns a const-iterator used to iterate over key-values.

        const_iterator end() const;
            // Returns a const-iterator representing the end of key-values.

        bool has(const bsl::string& key) const;
            // Returns whether this object holds a value associated with the
            // specified 'key'.

        bsl::string_view get(const bsl::string&      key,
                             const bsl::string_view& dflt = "") const;
            // Returns the value currently associated with 'key', or 'dflt' if
            // no associated value is currently held.
            //
            // Beware that if 'key' is not found and the returned 'string_view'
            // outlives 'dflt', then the 'string_view' will reference a
            // deallocated address.

        // PUBLIC MANIPULATORS
        void put(const bsl::string_view& key, const bsl::string_view& value);
            // Stores the specified 'value' associated with the specified
            // 'key'. Note that if such a 'key' was already stored, then its
            // associated value will be replaced by the supplied one.

        bool erase(const bsl::string& key);
            // Erases any value currently associated with 'key', and returns
            // whether any such value was previously held.
    };
};

}  // close package namespace
}  // close enterprise namespace

#endif