Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
gleocadie committed Oct 25, 2023
1 parent 7db4b8a commit b2694db
Show file tree
Hide file tree
Showing 30 changed files with 1,452 additions and 487 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="..\..\..\..\packages\libdatadog.3.0.0\build\native\libdatadog.props" Condition="Exists('..\..\..\..\packages\libdatadog.3.0.0\build\native\libdatadog.props')" />
<ItemGroup Label="ProjectConfigurations">
Expand Down Expand Up @@ -234,13 +234,24 @@
<ClInclude Include="CounterMetric.h" />
<ClInclude Include="DebugInfoStore.h" />
<ClInclude Include="EnabledProfilers.h" />
<ClInclude Include="EncodedProfile.hpp" />
<ClInclude Include="EnumHelpers.h" />
<ClInclude Include="COMHelpers.h" />
<ClInclude Include="ContentionProvider.h" />
<ClInclude Include="error_code.h" />
<ClInclude Include="Exception.h" />
<ClInclude Include="Exporter.h" />
<ClInclude Include="GarbageCollection.h" />
<ClInclude Include="GCBaseRawSample.h" />
<ClInclude Include="GarbageCollectionProvider.h" />
<ClInclude Include="GCThreadsCpuProvider.h" />
<ClInclude Include="libdatadog_details\AgentExporter.hpp" />
<ClInclude Include="libdatadog_details\error_code.hpp" />
<ClInclude Include="libdatadog_details\FileExporter.hpp" />
<ClInclude Include="libdatadog_details\Profile.hpp" />
<ClInclude Include="libdatadog_details\Tags.hpp" />
<ClInclude Include="libdatadog_details\Exporter.hpp" />
<ClInclude Include="libdatadog_helper.hpp" />
<ClInclude Include="MetadataProvider.h" />
<ClInclude Include="IAllocationsRecorder.h" />
<ClInclude Include="IBatchedSamplesProvider.h" />
Expand All @@ -257,6 +268,7 @@
<ClInclude Include="MetricBase.h" />
<ClInclude Include="MetricsRegistry.h" />
<ClInclude Include="NativeThreadsCpuProviderBase.h" />
<ClInclude Include="Profile.h" />
<ClInclude Include="ProxyMetric.h" />
<ClInclude Include="RawGarbageCollectionSample.h" />
<ClInclude Include="RawThreadLifetimeSample.h" />
Expand Down Expand Up @@ -329,6 +341,7 @@
<ClInclude Include="StackSamplerLoopManager.h" />
<ClInclude Include="StackSnapshotResultBuffer.h" />
<ClInclude Include="SumMetric.h" />
<ClInclude Include="Tags.h" />
<ClInclude Include="TagsHelper.h" />
<ClInclude Include="ThreadCpuInfo.h" />
<ClInclude Include="ThreadLifetimeProvider.h" />
Expand Down Expand Up @@ -358,7 +371,10 @@
<ClCompile Include="DebugInfoStore.cpp" />
<ClCompile Include="DogstatsdService.cpp" />
<ClCompile Include="EnabledProfilers.cpp" />
<ClCompile Include="error_code.cpp" />
<ClCompile Include="Exception.cpp" />
<ClCompile Include="ExceptionsProvider.cpp" />
<ClCompile Include="Exporter.cpp" />
<ClCompile Include="FfiHelper.cpp" />
<ClCompile Include="FrameStore.cpp" />
<ClCompile Include="GarbageCollectionProvider.cpp" />
Expand All @@ -369,10 +385,12 @@
<ClCompile Include="MeanMaxMetric.cpp" />
<ClCompile Include="MetricsRegistry.cpp" />
<ClCompile Include="NativeThreadsCpuProviderBase.cpp" />
<ClCompile Include="Profile.cpp" />
<ClCompile Include="ProxyMetric.cpp" />
<ClCompile Include="RawGarbageCollectionSample.cpp" />
<ClCompile Include="RawThreadLifetimeSample.cpp" />
<ClCompile Include="SampleValueTypeProvider.cpp" />
<ClCompile Include="std_extensions.hpp" />
<ClCompile Include="StopTheWorldGCProvider.cpp" />
<ClCompile Include="GenericSampler.cpp" />
<ClCompile Include="HResultConverter.cpp" />
Expand All @@ -395,6 +413,7 @@
<ClCompile Include="StackSamplerLoopManager.cpp" />
<ClCompile Include="StackSnapshotResultBuffer.cpp" />
<ClCompile Include="SumMetric.cpp" />
<ClCompile Include="Tags.cpp" />
<ClCompile Include="TagsHelper.cpp" />
<ClCompile Include="ThreadCpuInfo.cpp" />
<ClCompile Include="ThreadLifetimeProvider.cpp" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@
<Filter Include="Threads">
<UniqueIdentifier>{9343cd1c-a26e-4f7d-a006-85e4612d0813}</UniqueIdentifier>
</Filter>
<Filter Include="libdatadog">
<UniqueIdentifier>{5152df0e-33a6-4ea0-8ece-19a6d38ca88e}</UniqueIdentifier>
</Filter>
<Filter Include="libdatadog\error_handling">
<UniqueIdentifier>{43d49fd0-399f-4216-b7be-8769b8882c01}</UniqueIdentifier>
</Filter>
<Filter Include="libdatadog\details">
<UniqueIdentifier>{3522eff2-3a0d-4312-97a5-e1c6c0fe984b}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="CorProfilerCallback.h">
Expand Down Expand Up @@ -379,6 +388,44 @@
<ClInclude Include="TimelineSampleType.h">
<Filter>CorProfiler-Infrastructure</Filter>
</ClInclude>
<ClInclude Include="MetadataProvider.h" />
<ClInclude Include="Profile.h">
<Filter>libdatadog</Filter>
</ClInclude>
<ClInclude Include="error_code.h">
<Filter>libdatadog\error_handling</Filter>
</ClInclude>
<ClInclude Include="Exporter.h">
<Filter>libdatadog</Filter>
</ClInclude>
<ClInclude Include="libdatadog_details\Exporter.hpp">
<Filter>libdatadog\details</Filter>
</ClInclude>
<ClInclude Include="libdatadog_details\Tags.hpp">
<Filter>libdatadog\details</Filter>
</ClInclude>
<ClInclude Include="Tags.h">
<Filter>libdatadog</Filter>
</ClInclude>
<ClInclude Include="Exception.h">
<Filter>libdatadog</Filter>
</ClInclude>
<ClInclude Include="libdatadog_details\Profile.hpp">
<Filter>libdatadog\details</Filter>
</ClInclude>
<ClInclude Include="libdatadog_details\error_code.hpp">
<Filter>libdatadog\details</Filter>
</ClInclude>
<ClInclude Include="libdatadog_details\FileExporter.hpp">
<Filter>libdatadog\details</Filter>
</ClInclude>
<ClInclude Include="libdatadog_details\AgentExporter.hpp">
<Filter>libdatadog\details</Filter>
</ClInclude>
<ClInclude Include="libdatadog_helper.hpp">
<Filter>libdatadog</Filter>
</ClInclude>
<ClInclude Include="EncodedProfile.hpp" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="OpSysTools.cpp">
Expand Down Expand Up @@ -567,8 +614,26 @@
<ClCompile Include="TimelineSampleType.cpp">
<Filter>CorProfiler-Infrastructure</Filter>
</ClCompile>
<ClCompile Include="Profile.cpp">
<Filter>libdatadog</Filter>
</ClCompile>
<ClCompile Include="error_code.cpp">
<Filter>libdatadog\error_handling</Filter>
</ClCompile>
<ClCompile Include="Exporter.cpp">
<Filter>libdatadog</Filter>
</ClCompile>
<ClCompile Include="Tags.cpp">
<Filter>libdatadog</Filter>
</ClCompile>
<ClCompile Include="Exception.cpp">
<Filter>libdatadog</Filter>
</ClCompile>
<ClCompile Include="std_extensions.hpp">
<Filter>libdatadog</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
</Project>
</Project>
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 profiler/src/ProfilerEngine/Datadog.Profiler.Native/Exception.cpp
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 profiler/src/ProfilerEngine/Datadog.Profiler.Native/Exception.h
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 profiler/src/ProfilerEngine/Datadog.Profiler.Native/Exporter.cpp
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
Loading

0 comments on commit b2694db

Please sign in to comment.