Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add InstrumentationLibrary to Tracer #693

Merged
merged 13 commits into from
Apr 26, 2021
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright 2021, OpenTelemetry Authors
//
// 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.

#pragma once

#include "opentelemetry/nostd/string_view.h"
#include "opentelemetry/nostd/unique_ptr.h"
#include "opentelemetry/version.h"

OPENTELEMETRY_BEGIN_NAMESPACE

namespace instrumentationlibrary
{

class InstrumentationLibrary
{
public:
InstrumentationLibrary(const InstrumentationLibrary &) = default;

/**
* Returns a newly created InstrumentationLibrary with the specified library name and version.
* @param name name of the instrumentation library.
* @param version version of the instrumentation library.
* @returns the newly created InstrumentationLibrary.
*/
static nostd::unique_ptr<InstrumentationLibrary> create(nostd::string_view name,
nostd::string_view version)
{
return nostd::unique_ptr<InstrumentationLibrary>(
new InstrumentationLibrary{std::string{name}, std::string{version}});
}

const std::string &GetName() const { return name_; }
const std::string &GetVersion() const { return version_; }

private:
InstrumentationLibrary(nostd::string_view name, nostd::string_view version)
: name_(name), version_(version)
{}

private:
std::string name_;
std::string version_;
};

} // namespace instrumentationlibrary

OPENTELEMETRY_END_NAMESPACE
26 changes: 26 additions & 0 deletions api/include/opentelemetry/trace/tracer.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "opentelemetry/instrumentationlibrary/instrumentation_library.h"
#include "opentelemetry/nostd/shared_ptr.h"
#include "opentelemetry/nostd/string_view.h"
#include "opentelemetry/nostd/unique_ptr.h"
Expand All @@ -14,6 +15,9 @@
OPENTELEMETRY_BEGIN_NAMESPACE
namespace trace
{

using InstrumentationLibrary = opentelemetry::instrumentationlibrary::InstrumentationLibrary;

/**
* Handles span creation and in-process context propagation.
*
Expand Down Expand Up @@ -162,6 +166,25 @@ class Tracer
}
}

/**
* Get the instrumentation library associated with the current tracer.
* @return the instrumentation library for current tracer.
*/
InstrumentationLibrary *GetInstrumentationLibrary() const noexcept
ThomsonTan marked this conversation as resolved.
Show resolved Hide resolved
{
return instrumentation_library_.get();
}

/**
* Set instrumentation library.
* @param instrumentation library to set on the current tracer.
*/
void SetInstrumentationLibrary(
lalitb marked this conversation as resolved.
Show resolved Hide resolved
nostd::unique_ptr<InstrumentationLibrary> &&instrumentation_library) noexcept
{
instrumentation_library_ = std::move(instrumentation_library);
}

/**
* Force any buffered spans to flush.
* @param timeout to complete the flush
Expand All @@ -187,6 +210,9 @@ class Tracer
}

virtual void CloseWithMicroseconds(uint64_t timeout) noexcept = 0;

private:
std::unique_ptr<InstrumentationLibrary> instrumentation_library_;
};
} // namespace trace
OPENTELEMETRY_END_NAMESPACE
1 change: 1 addition & 0 deletions api/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ add_subdirectory(metrics)
add_subdirectory(logs)
add_subdirectory(common)
add_subdirectory(baggage)
add_subdirectory(instrumentationlibrary)
12 changes: 12 additions & 0 deletions api/test/instrumentationlibrary/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
load("//bazel:otel_cc_benchmark.bzl", "otel_cc_benchmark")

cc_test(
name = "instrumentationlibrary_test",
srcs = [
"instrumentationlibrary_test.cc",
],
deps = [
"//api",
"@com_google_googletest//:gtest_main",
],
)
12 changes: 12 additions & 0 deletions api/test/instrumentationlibrary/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
include(GoogleTest)

foreach(testname instrumentationlibrary_test)
add_executable(${testname} "${testname}.cc")
target_link_libraries(
${testname} ${GTEST_BOTH_LIBRARIES} ${CORE_RUNTIME_LIBS}
${CMAKE_THREAD_LIBS_INIT} opentelemetry_api)
gtest_add_tests(
TARGET ${testname}
TEST_PREFIX baggage.
TEST_LIST ${testname})
endforeach()
35 changes: 35 additions & 0 deletions api/test/instrumentationlibrary/instrumentationlibrary_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2021, OpenTelemetry Authors
//
// 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.

#include "opentelemetry/nostd/string_view.h"

#include <gtest/gtest.h>
#include <string>
#include <vector>

#include "opentelemetry/instrumentationlibrary/instrumentation_library.h"

using namespace opentelemetry;
using namespace opentelemetry::instrumentationlibrary;

TEST(InstrumentationLibrary, CreateInstrumentationLibrary)
{

std::string library_name = "opentelemetry-cpp";
std::string library_version = "0.1.0";
auto instrumentation_library = InstrumentationLibrary::create(library_name, library_version);

EXPECT_EQ(instrumentation_library->GetName(), library_name);
EXPECT_EQ(instrumentation_library->GetVersion(), library_version);
}
4 changes: 2 additions & 2 deletions sdk/include/opentelemetry/sdk/trace/tracer.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ class Tracer final : public trace_api::Tracer, public std::enable_shared_from_th
void CloseWithMicroseconds(uint64_t timeout) noexcept override;

/** Returns the currently active span processor. */
SpanProcessor &GetActiveProcessor() noexcept { return context_->GetActiveProcessor(); }
SpanProcessor &GetActiveProcessor() const noexcept { return context_->GetActiveProcessor(); }

/** Returns the configured Id generator */
IdGenerator &GetIdGenerator() noexcept { return context_->GetIdGenerator(); }
IdGenerator &GetIdGenerator() const noexcept { return context_->GetIdGenerator(); }

// Note: Test only
Sampler &GetSampler() { return context_->GetSampler(); }
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/trace/tracer_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ TracerContext::TracerContext(std::unique_ptr<SpanProcessor> processor,

Sampler &TracerContext::GetSampler() const noexcept
{
return *sampler_.get();
return *sampler_;
}

const opentelemetry::sdk::resource::Resource &TracerContext::GetResource() const noexcept
Expand Down
3 changes: 3 additions & 0 deletions sdk/src/trace/tracer_provider.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ opentelemetry::nostd::shared_ptr<opentelemetry::trace::Tracer> TracerProvider::G
nostd::string_view library_name,
nostd::string_view library_version) noexcept
{
tracer_->SetInstrumentationLibrary(
opentelemetry::instrumentationlibrary::InstrumentationLibrary::create(library_name,
lalitb marked this conversation as resolved.
Show resolved Hide resolved
library_version));
return opentelemetry::nostd::shared_ptr<opentelemetry::trace::Tracer>(tracer_);
}

Expand Down
13 changes: 12 additions & 1 deletion sdk/test/trace/tracer_provider_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ TEST(TracerProvider, GetTracer)
std::unique_ptr<IdGenerator>(new RandomIdGenerator)));
auto sdkTracer2 = dynamic_cast<Tracer *>(tp2.GetTracer("test").get());
ASSERT_EQ("AlwaysOffSampler", sdkTracer2->GetSampler().GetDescription());

// TODO: return different tracer for different name.
// auto instrumentation_library1 = t1->GetInstrumentationLibrary();
// ASSERT_NE(instrumentation_library1, nullptr);
// ASSERT_EQ(instrumentation_library1->GetName(), "test");
// ASSERT_EQ(instrumentation_library1->GetVersion(), "");

auto instrumentation_library3 = t3->GetInstrumentationLibrary();
ASSERT_NE(instrumentation_library3, nullptr);
ASSERT_EQ(instrumentation_library3->GetName(), "different");
ASSERT_EQ(instrumentation_library3->GetVersion(), "1.0.0");
}

TEST(TracerProvider, Shutdown)
Expand All @@ -55,4 +66,4 @@ TEST(TracerProvider, ForceFlush)
TracerProvider tp1(std::move(processor1));

EXPECT_TRUE(tp1.ForceFlush());
}
}