Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions include/api/Metrics.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#pragma once

#include <array>
#include <optional>
#include <unordered_map>
#include <tuple>
#include <mutex>
Expand Down Expand Up @@ -88,6 +89,16 @@ class Metrics
IdType lookup(const std::string_view name) const;
IntType *lookup(IdType id, std::string_view *name = nullptr) const;

std::optional<IntType *>
lookupPtr(const std::string_view name) const
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A const string_view strikes me as odd, but I see it is done elsewhere in the file.

{
IdType id = lookup(name);
if (id != NOT_FOUND) {
return lookup(id);
}
return std::nullopt;
}

// A bit of a convenience, since we use the ptr to the atomic frequently in the core
IntType *
newMetricPtr(const std::string_view name)
Expand Down
13 changes: 13 additions & 0 deletions src/api/test_Metrics.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,17 @@ TEST_CASE("Metrics", "[libtsapi][Metrics]")

REQUIRE(m[0].load() == 42);
}

SECTION("lookup")
{
auto nm = m.lookupPtr("notametric");
REQUIRE(!nm);

auto mid = m.newMetric("ametric");
auto fm = m.lookupPtr("ametric");
REQUIRE(fm.has_value());
REQUIRE(fm.value());
REQUIRE(fm.value() == m.lookup(mid));
REQUIRE(m.lookup("ametric") == mid);
}
}