-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
30 changed files
with
1,452 additions
and
487 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
32 changes: 32 additions & 0 deletions
32
profiler/src/ProfilerEngine/Datadog.Profiler.Native/EncodedProfile.hpp
This file contains 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,32 @@ | ||
|
||
|
||
extern "C" | ||
{ | ||
#include "datadog/common.h" | ||
#include "datadog/profiling.h" | ||
} | ||
|
||
struct EncodedProfile | ||
{ | ||
struct EncodedProfileDeleter | ||
{ | ||
void operator()(ddog_prof_EncodedProfile* o) | ||
{ | ||
ddog_prof_EncodedProfile_drop(o); | ||
} | ||
}; | ||
|
||
using encoded_profile_ptr = std::unique_ptr<ddog_prof_EncodedProfile, EncodedProfileDeleter>; | ||
|
||
EncodedProfile(ddog_prof_EncodedProfile* p) : | ||
_profile(p) | ||
{ | ||
} | ||
|
||
operator ddog_prof_EncodedProfile*() | ||
{ | ||
return _profile.get(); | ||
} | ||
|
||
encoded_profile_ptr _profile; | ||
}; |
12 changes: 12 additions & 0 deletions
12
profiler/src/ProfilerEngine/Datadog.Profiler.Native/Exception.cpp
This file contains 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,12 @@ | ||
#include "Exception.h" | ||
|
||
namespace libdatadog { | ||
Exception::Exception(std::string message) : | ||
_message(std::move(message)) | ||
{ | ||
} | ||
char const* Exception::what() const noexcept | ||
{ | ||
return _message.c_str(); | ||
} | ||
} // namespace libdatadog |
15 changes: 15 additions & 0 deletions
15
profiler/src/ProfilerEngine/Datadog.Profiler.Native/Exception.h
This file contains 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,15 @@ | ||
#pragma once | ||
#include "error_code.h" | ||
|
||
namespace libdatadog { | ||
class Exception : public std::exception | ||
{ | ||
public: | ||
Exception(std::string message); | ||
|
||
char const* what() const noexcept override; | ||
|
||
private: | ||
std::string _message; | ||
}; | ||
} // namespace libdatadog |
156 changes: 156 additions & 0 deletions
156
profiler/src/ProfilerEngine/Datadog.Profiler.Native/Exporter.cpp
This file contains 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,156 @@ | ||
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License. | ||
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2022 Datadog, Inc. | ||
|
||
#include "Exporter.h" | ||
|
||
#include "Exception.h" | ||
#include "FfiHelper.h" | ||
#include "Profile.h" | ||
#include "Tags.h" | ||
#include "libdatadog_details/AgentExporter.hpp" | ||
#include "libdatadog_details/Exporter.hpp" | ||
#include "libdatadog_details/FileExporter.hpp" | ||
#include "libdatadog_details/Profile.hpp" | ||
#include "libdatadog_details/Tags.hpp" | ||
#include "libdatadog_details/error_code.hpp" | ||
#include "libdatadog_helper.hpp" | ||
#include "EncodedProfile.hpp" | ||
#include "std_extensions.hpp" | ||
|
||
#include <cassert> | ||
|
||
namespace libdatadog { | ||
|
||
Exporter::Exporter(std::unique_ptr<detail::AgentExporter> ddExporter, std::unique_ptr<detail::FileExporter> fileExporter) : | ||
_exporterImpl{std::move(ddExporter)}, | ||
_fileExporter{std::move(fileExporter)} | ||
{ | ||
} | ||
|
||
Exporter::~Exporter() = default; | ||
|
||
Exporter::ExporterBuilder& Exporter::ExporterBuilder::WithAgent(std::string url) | ||
{ | ||
assert(!_agentless); | ||
_withAgent = true; | ||
_url = std::move(url); | ||
return *this; | ||
} | ||
|
||
Exporter::ExporterBuilder& Exporter::ExporterBuilder::WithoutAgent(std::string site, std::string apiKey) | ||
{ | ||
assert(_withAgent == false); | ||
_agentless = true; | ||
|
||
_site = std::move(site); | ||
_apiKey = std::move(apiKey); | ||
return *this; | ||
} | ||
|
||
Exporter::ExporterBuilder& Exporter::ExporterBuilder::WithTags(Tags tags) | ||
{ | ||
_tags = std::move(tags); | ||
return *this; | ||
} | ||
|
||
struct Exporter::ExporterBuilder::AgentEndpoint | ||
{ | ||
ddog_Endpoint inner; | ||
}; | ||
|
||
std::unique_ptr<libdatadog::detail::AgentExporter> Exporter::ExporterBuilder::CreateDatadogAgentExporter() | ||
{ | ||
auto endpoint = CreateEndpoint(); | ||
|
||
auto result = ddog_prof_Exporter_new( | ||
FfiHelper::StringToCharSlice(_libraryName), | ||
FfiHelper::StringToCharSlice(_libraryVersion), | ||
FfiHelper::StringToCharSlice(_languageFamily), | ||
static_cast<ddog_Vec_Tag*>(*_tags._impl), | ||
endpoint.inner); | ||
|
||
if (result.tag == DDOG_PROF_EXPORTER_NEW_RESULT_ERR) | ||
{ | ||
std::unique_ptr<ddog_Error> error(&result.err); | ||
throw Exception(std::to_string(error.get())); | ||
} | ||
|
||
// the AgentExporter instance is acquiring the ownership of the ok ptr | ||
return std::make_unique<detail::AgentExporter>(result.ok); | ||
} | ||
|
||
Exporter::ExporterBuilder::ExporterBuilder() = default; | ||
Exporter::ExporterBuilder::~ExporterBuilder() = default; | ||
|
||
Exporter::ExporterBuilder& Exporter::ExporterBuilder::WithFileExporter(fs::path outputDirectory) | ||
{ | ||
_outputDirectory = std::move(outputDirectory); | ||
return *this; | ||
} | ||
|
||
Exporter::ExporterBuilder& Exporter::ExporterBuilder::SetLibraryName(std::string libraryName) | ||
{ | ||
_libraryName = std::move(libraryName); | ||
return *this; | ||
} | ||
|
||
Exporter::ExporterBuilder& Exporter::ExporterBuilder::SetLibraryVersion(std::string libraryVersion) | ||
{ | ||
_libraryVersion = std::move(libraryVersion); | ||
return *this; | ||
} | ||
|
||
Exporter::ExporterBuilder& Exporter::ExporterBuilder::SetLanguageFamily(std::string family) | ||
{ | ||
_languageFamily = std::move(family); | ||
return *this; | ||
} | ||
|
||
Exporter::ExporterBuilder::AgentEndpoint Exporter::ExporterBuilder::CreateEndpoint() | ||
{ | ||
if (_agentless) | ||
{ | ||
assert(!_site.empty()); | ||
assert(!_apiKey.empty()); | ||
return {ddog_Endpoint_agentless(FfiHelper::StringToCharSlice(_site), FfiHelper::StringToCharSlice(_apiKey))}; | ||
} | ||
|
||
assert(!_url.empty()); | ||
return {ddog_Endpoint_agent(FfiHelper::StringToCharSlice(_url))}; | ||
} | ||
|
||
std::unique_ptr<Exporter> Exporter::ExporterBuilder::Build() | ||
{ | ||
auto datadogAgentExporter = CreateDatadogAgentExporter(); | ||
|
||
std::unique_ptr<detail::FileExporter> fileExporter = nullptr; | ||
if (!_outputDirectory.empty()) | ||
{ | ||
fileExporter = std::make_unique<detail::FileExporter>(_outputDirectory); | ||
} | ||
|
||
return std::unique_ptr<Exporter>(new Exporter(std::move(datadogAgentExporter), std::move(fileExporter))); | ||
} | ||
|
||
libdatadog::error_code Exporter::Send(Profile* profile, Tags tags, std::vector<std::pair<std::string, std::string>> files, std::string metadata) | ||
{ | ||
auto s = ddog_prof_Profile_serialize(*(profile->_impl), nullptr, nullptr); | ||
|
||
if (s.tag == DDOG_PROF_PROFILE_SERIALIZE_RESULT_ERR) | ||
{ | ||
return detail::make_error(s.err); | ||
} | ||
|
||
auto ep = EncodedProfile(&s.ok); | ||
|
||
if (_fileExporter != nullptr) | ||
{ | ||
// link error code ? or log ?? | ||
_fileExporter->WriteToDisk(ep, profile->GetApplicationName()); | ||
} | ||
|
||
assert(_exporterImpl != nullptr); | ||
return _exporterImpl->Send(ep, std::move(tags), std::move(files), std::move(metadata)); | ||
} | ||
|
||
} // namespace libdatadog |
Oops, something went wrong.