-
Notifications
You must be signed in to change notification settings - Fork 158
Updated OpenTelemetry tests to use their own SpanExporter rather than depend on the InMemorySpanExporter.
#4042
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
Merged
LarryOsterman
merged 10 commits into
Azure:main
from
LarryOsterman:larryo/currentopentelemetry
Oct 24, 2022
Merged
Changes from 9 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
3269985
Removed version override for opentelemetry
LarryOsterman c4af018
Disable deprecation warnings for open telemety tests to guard against…
LarryOsterman 2aad39e
Added explanation for 4996 error; removed InMemoryExporter from docum…
LarryOsterman 9653c60
Checkpoint adding manual telemetry exporter
LarryOsterman 88f1f2a
Switch from using OpenTelemetry's InMemoryExporter (which is an inter…
LarryOsterman 9f8370b
removed unused variables because gcc doesn't like them
LarryOsterman 5e17e95
Fixed doxygen comments
LarryOsterman cafdfc5
Removed a bunch of unnecessary headers
LarryOsterman ca29e60
Merge branch 'Azure:main' into larryo/currentopentelemetry
LarryOsterman fca17ec
Update sdk/core/azure-core-tracing-opentelemetry/test/ut/test_exporte…
LarryOsterman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
224 changes: 224 additions & 0 deletions
224
sdk/core/azure-core-tracing-opentelemetry/test/ut/test_exporter.hpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,224 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <memory> | ||
| #include <opentelemetry/sdk/trace/exporter.h> | ||
|
|
||
| class RecordedSpan : public opentelemetry::sdk::trace::Recordable { | ||
| struct Event | ||
| { | ||
| std::string Name; | ||
| std::chrono::system_clock::time_point Timestamp; | ||
| opentelemetry::sdk::common::AttributeMap Attributes; | ||
| }; | ||
| opentelemetry::trace::SpanId m_parentSpan; | ||
| opentelemetry::trace::SpanId m_spanId; | ||
| opentelemetry::sdk::common::AttributeMap m_attributes; | ||
| std::vector<Event> m_events; | ||
| opentelemetry::trace::StatusCode m_statusCode{}; | ||
| std::string m_statusDescription; | ||
| std::string m_name; | ||
| opentelemetry::trace::SpanKind m_spanKind{}; | ||
| std::chrono::system_clock::time_point m_startTime; | ||
| std::chrono::nanoseconds m_duration{}; | ||
| std::unique_ptr<opentelemetry::sdk::instrumentationscope::InstrumentationScope> m_scope; | ||
| std::unique_ptr<opentelemetry::sdk::resource::Resource> m_resource; | ||
|
|
||
| public: | ||
| ~RecordedSpan() = default; | ||
|
|
||
| /** | ||
| * Set the span context and parent span id | ||
| * @param span_context the span context to set | ||
| * @param parent_span_id the parent span id to set | ||
| */ | ||
| void SetIdentity( | ||
| const opentelemetry::trace::SpanContext& span_context, | ||
| opentelemetry::trace::SpanId parent_span_id) noexcept override | ||
| { | ||
| m_parentSpan = parent_span_id; | ||
| m_spanId = span_context.span_id(); | ||
| }; | ||
|
|
||
| /** | ||
| * Set an attribute of a span. | ||
| * @param key the name of the attribute | ||
| * @param value the attribute value | ||
| */ | ||
| void SetAttribute( | ||
| opentelemetry::nostd::string_view key, | ||
| const opentelemetry::common::AttributeValue& value) noexcept override | ||
| { | ||
| m_attributes.SetAttribute(key, value); | ||
| }; | ||
|
|
||
| /** | ||
| * Add an event to a span. | ||
| * @param name the name of the event | ||
| * @param timestamp the timestamp of the event | ||
| * @param attributes the attributes associated with the event | ||
| */ | ||
| void AddEvent( | ||
| opentelemetry::nostd::string_view name, | ||
| opentelemetry::common::SystemTimestamp timestamp, | ||
| const opentelemetry::common::KeyValueIterable& attributes) noexcept override | ||
| { | ||
| Event event; | ||
| event.Name = std::string(name); | ||
| event.Timestamp = timestamp; | ||
|
|
||
| attributes.ForEachKeyValue( | ||
| [&event]( | ||
| opentelemetry::nostd::string_view name, opentelemetry::common::AttributeValue value) { | ||
| event.Attributes.SetAttribute(name, value); | ||
| return true; | ||
| }); | ||
| m_events.push_back(event); | ||
| }; | ||
|
|
||
| /** | ||
| * Add a link to a span. | ||
| */ | ||
| void AddLink( | ||
| const opentelemetry::trace::SpanContext&, | ||
| const opentelemetry::common::KeyValueIterable&) noexcept override{ | ||
| // TODO, when we use this, we need to test this. | ||
| // NO-OP since this exporter silences link data. | ||
| }; | ||
|
|
||
| /** | ||
| * Set the status of the span. | ||
| * @param code the status code | ||
| * @param description a description of the status | ||
| */ | ||
| void SetStatus( | ||
| opentelemetry::trace::StatusCode code, | ||
| opentelemetry::nostd::string_view description) noexcept override | ||
| { | ||
| m_statusCode = code; | ||
| m_statusDescription = std::string(description); | ||
| }; | ||
|
|
||
| /** | ||
| * Set the name of the span. | ||
| * @param name the name to set | ||
| */ | ||
| void SetName(opentelemetry::nostd::string_view name) noexcept override | ||
| { | ||
| m_name = std::string(name); | ||
| }; | ||
|
|
||
| /** | ||
| * Set the spankind of the span. | ||
| * @param span_kind the spankind to set | ||
| */ | ||
| void SetSpanKind(opentelemetry::trace::SpanKind span_kind) noexcept override | ||
| { | ||
| m_spanKind = span_kind; | ||
| }; | ||
|
|
||
| /** | ||
| * Set Resource of the span | ||
| * @param resource the resource to set | ||
| */ | ||
| void SetResource(const opentelemetry::sdk::resource::Resource& resource) noexcept override | ||
| { | ||
| m_resource = std::make_unique<opentelemetry::sdk::resource::Resource>(resource); | ||
| }; | ||
|
|
||
| /** | ||
| * Set the start time of the span. | ||
| * @param start_time the start time to set | ||
| */ | ||
| void SetStartTime(opentelemetry::common::SystemTimestamp start_time) noexcept override | ||
| { | ||
| m_startTime = start_time; | ||
| }; | ||
|
|
||
| /** | ||
| * Set the duration of the span. | ||
| * @param duration the duration to set | ||
| */ | ||
| void SetDuration(std::chrono::nanoseconds duration) noexcept override { m_duration = duration; } | ||
|
|
||
| /** | ||
| * Set the instrumentation scope of the span. | ||
| * @param instrumentation_scope the instrumentation scope to set | ||
| */ | ||
| void SetInstrumentationScope(const opentelemetry::sdk::instrumentationscope::InstrumentationScope& | ||
| instrumentation_scope) noexcept override | ||
| { | ||
| m_scope = std::make_unique<opentelemetry::sdk::instrumentationscope::InstrumentationScope>( | ||
| instrumentation_scope); | ||
| }; | ||
|
|
||
| std::string GetName() { return m_name; } | ||
| opentelemetry::trace::StatusCode GetStatus() { return m_statusCode; } | ||
| opentelemetry::trace::SpanId GetParentSpanId() { return m_parentSpan; } | ||
| opentelemetry::trace::SpanKind GetSpanKind() { return m_spanKind; } | ||
| opentelemetry::trace::SpanId GetSpanId() { return m_spanId; } | ||
| opentelemetry::sdk::common::AttributeMap const& GetAttributes() { return m_attributes; } | ||
| opentelemetry::sdk::instrumentationscope::InstrumentationScope& GetInstrumentationScope() | ||
| { | ||
| return *m_scope; | ||
| } | ||
| }; | ||
|
|
||
| class TestExporter final : public opentelemetry::sdk::trace::SpanExporter { | ||
|
|
||
| public: | ||
| class TestData { | ||
| std::vector<std::unique_ptr<RecordedSpan>> m_spans; | ||
|
|
||
| public: | ||
| // Returns a copy of the recorded spans and clears the set of recorded spans. | ||
| std::vector<std::unique_ptr<RecordedSpan>> const ExtractSpans() { return std::move(m_spans); } | ||
| void AddSpan(std::unique_ptr<RecordedSpan>&& span) { m_spans.push_back(std::move(span)); } | ||
| }; | ||
| std::shared_ptr<TestData> const& GetTestData() { return m_testData; } | ||
|
|
||
| TestExporter() : m_testData{std::make_shared<TestData>()} {} | ||
| virtual ~TestExporter() = default; | ||
|
|
||
| /** | ||
| * Create a span recordable. This object will be used to record span data and | ||
| * will subsequently be passed to SpanExporter::Export. Vendors can implement | ||
| * custom recordables or use the default SpanData recordable provided by the | ||
| * SDK. | ||
| * @return a newly initialized Recordable object | ||
| * | ||
| * Note: This method must be callable from multiple threads. | ||
| */ | ||
| std::unique_ptr<opentelemetry::sdk::trace::Recordable> MakeRecordable() noexcept override | ||
| { | ||
| return std::unique_ptr<opentelemetry::sdk::trace::Recordable>(new (std::nothrow) RecordedSpan); | ||
| } | ||
|
|
||
| /** | ||
| * Exports a batch of span recordables. This method must not be called | ||
| * concurrently for the same exporter instance. | ||
| * @param spans a span of unique pointers to span recordables | ||
| */ | ||
| opentelemetry::sdk::common::ExportResult Export( | ||
| const opentelemetry::nostd::span<std::unique_ptr<opentelemetry::sdk::trace::Recordable>>& | ||
| spans) noexcept override | ||
| { | ||
| for (auto& recordable : spans) | ||
| { | ||
| auto span = std::unique_ptr<RecordedSpan>(static_cast<RecordedSpan*>(recordable.release())); | ||
| m_testData->AddSpan(std::move(span)); | ||
| } | ||
| return opentelemetry::sdk::common::ExportResult::kSuccess; | ||
| } | ||
|
|
||
| /** | ||
| * Shut down the exporter. | ||
| * @return return the status of the operation. | ||
| */ | ||
| bool Shutdown(std::chrono::microseconds) noexcept override { return true; } | ||
|
|
||
| private: | ||
| std::shared_ptr<TestData> m_testData; | ||
| }; | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.