From 1f6090dc013e9331857c49531ba17750fae81ae3 Mon Sep 17 00:00:00 2001 From: duncanpo Date: Thu, 17 Aug 2023 14:46:53 -0400 Subject: [PATCH 01/11] Move common code to common folder --- CMakeLists.txt | 15 ++++-- .../+common}/mustBeScalarString.m | 0 .../common/ProcessedAttributes.h | 21 ++++++++ .../opentelemetry-matlab/common/attribute.h | 22 ++++++++ api/{trace => common}/src/attribute.cpp | 50 +++++++++---------- api/trace/+opentelemetry/+trace/Span.m | 4 +- api/trace/+opentelemetry/+trace/Tracer.m | 2 +- .../+opentelemetry/+trace/TracerProvider.m | 2 +- .../trace/TracerProviderProxy.h | 2 - .../opentelemetry-matlab/trace/attribute.h | 28 ----------- api/trace/src/SpanProxy.cpp | 21 +++----- api/trace/src/TracerProxy.cpp | 17 +++---- .../sdk/common/resource.h | 16 ++++++ sdk/common/src/resource.cpp | 34 +++++++++++++ sdk/trace/src/TracerProviderProxy.cpp | 22 ++------ test/commonSetupOnce.m | 2 +- test/ttrace.m | 18 +++---- 17 files changed, 158 insertions(+), 118 deletions(-) rename api/{utils/+opentelemetry/+utils => common/+opentelemetry/+common}/mustBeScalarString.m (100%) create mode 100644 api/common/include/opentelemetry-matlab/common/ProcessedAttributes.h create mode 100644 api/common/include/opentelemetry-matlab/common/attribute.h rename api/{trace => common}/src/attribute.cpp (57%) delete mode 100644 api/trace/include/opentelemetry-matlab/trace/attribute.h create mode 100644 sdk/common/include/opentelemetry-matlab/sdk/common/resource.h create mode 100644 sdk/common/src/resource.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index da4e649..9cd97df 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -73,16 +73,20 @@ endif() set(TRACE_API_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/api/trace/include) set(CONTEXT_API_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/api/context/include) set(BAGGAGE_API_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/api/baggage/include) +set(COMMON_API_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/api/common/include) set(TRACE_SDK_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/sdk/trace/include) +set(COMMON_SDK_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/sdk/common/include) set(OTLP_EXPORTER_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/exporters/otlp/include) -set(OPENTELEMETRY_PROXY_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR} ${TRACE_API_INCLUDE_DIR} ${CONTEXT_API_INCLUDE_DIR} ${BAGGAGE_API_INCLUDE_DIR} ${TRACE_SDK_INCLUDE_DIR} ${OTLP_EXPORTER_INCLUDE_DIR} ${OPENTELEMETRY_CPP_INCLUDE_DIRS}) +set(OPENTELEMETRY_PROXY_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR} ${TRACE_API_INCLUDE_DIR} ${CONTEXT_API_INCLUDE_DIR} ${BAGGAGE_API_INCLUDE_DIR} ${COMMON_API_INCLUDE_DIR} ${TRACE_SDK_INCLUDE_DIR} ${COMMON_SDK_INCLUDE_DIR} ${OTLP_EXPORTER_INCLUDE_DIR} ${OPENTELEMETRY_CPP_INCLUDE_DIRS}) set(OPENTELEMETRY_PROXY_FACTORY_CLASS_NAME OtelMatlabProxyFactory) set(OPENTELEMETRY_PROXY_FACTORY_SOURCES_DIR ${CMAKE_CURRENT_SOURCE_DIR}) set(TRACE_API_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/api/trace/src) set(CONTEXT_API_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/api/context/src) set(BAGGAGE_API_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/api/baggage/src) +set(COMMON_API_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/api/common/src) set(TRACE_SDK_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/sdk/trace/src) +set(COMMON_SDK_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/sdk/common/src) set(OTLP_EXPORTER_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/exporters/otlp/src) set(OPENTELEMETRY_PROXY_SOURCES ${OPENTELEMETRY_PROXY_FACTORY_SOURCES_DIR}/${OPENTELEMETRY_PROXY_FACTORY_CLASS_NAME}.cpp @@ -90,7 +94,7 @@ set(OPENTELEMETRY_PROXY_SOURCES ${TRACE_API_SOURCE_DIR}/TracerProxy.cpp ${TRACE_API_SOURCE_DIR}/SpanProxy.cpp ${TRACE_API_SOURCE_DIR}/SpanContextProxy.cpp - ${TRACE_API_SOURCE_DIR}/attribute.cpp + ${COMMON_API_SOURCE_DIR}/attribute.cpp ${CONTEXT_API_SOURCE_DIR}/TextMapPropagatorProxy.cpp ${CONTEXT_API_SOURCE_DIR}/CompositePropagatorProxy.cpp ${CONTEXT_API_SOURCE_DIR}/TextMapCarrierProxy.cpp @@ -99,7 +103,8 @@ set(OPENTELEMETRY_PROXY_SOURCES ${TRACE_SDK_SOURCE_DIR}/TracerProviderProxy.cpp ${TRACE_SDK_SOURCE_DIR}/SimpleSpanProcessorProxy.cpp ${TRACE_SDK_SOURCE_DIR}/BatchSpanProcessorProxy.cpp - ${TRACE_SDK_SOURCE_DIR}/ParentBasedSamplerProxy.cpp) + ${TRACE_SDK_SOURCE_DIR}/ParentBasedSamplerProxy.cpp + ${COMMON_SDK_SOURCE_DIR}/resource.cpp) if(WITH_OTLP_HTTP) set(OPENTELEMETRY_PROXY_SOURCES ${OPENTELEMETRY_PROXY_SOURCES} ${OTLP_EXPORTER_SOURCE_DIR}/OtlpHttpSpanExporterProxy.cpp) @@ -215,7 +220,7 @@ libmexclass_client_install( set(TRACE_API_MATLAB_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/api/trace/+opentelemetry) set(CONTEXT_API_MATLAB_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/api/context/+opentelemetry) set(BAGGAGE_API_MATLAB_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/api/baggage/+opentelemetry) -set(UTILS_API_MATLAB_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/api/utils/+opentelemetry) +set(COMMON_API_MATLAB_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/api/common/+opentelemetry) set(TRACE_SDK_MATLAB_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/sdk/trace/+opentelemetry) set(DEFAULT_EXPORTER_MATLAB_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/exporters/otlp/+opentelemetry/+exporters/+otlp/defaultSpanExporter.m) set(OTLP_HTTP_EXPORTER_MATLAB_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/exporters/otlp/+opentelemetry/+exporters/+otlp/OtlpHttpSpanExporter.m) @@ -226,7 +231,7 @@ set(OTLP_EXPORTERS_DIR +opentelemetry/+exporters/+otlp) install(DIRECTORY ${TRACE_API_MATLAB_SOURCES} DESTINATION .) install(DIRECTORY ${CONTEXT_API_MATLAB_SOURCES} DESTINATION .) install(DIRECTORY ${BAGGAGE_API_MATLAB_SOURCES} DESTINATION .) -install(DIRECTORY ${UTILS_API_MATLAB_SOURCES} DESTINATION .) +install(DIRECTORY ${COMMON_API_MATLAB_SOURCES} DESTINATION .) install(DIRECTORY ${TRACE_SDK_MATLAB_SOURCES} DESTINATION .) install(FILES ${DEFAULT_EXPORTER_MATLAB_SOURCES} DESTINATION ${OTLP_EXPORTERS_DIR}) if(WITH_OTLP_HTTP) diff --git a/api/utils/+opentelemetry/+utils/mustBeScalarString.m b/api/common/+opentelemetry/+common/mustBeScalarString.m similarity index 100% rename from api/utils/+opentelemetry/+utils/mustBeScalarString.m rename to api/common/+opentelemetry/+common/mustBeScalarString.m diff --git a/api/common/include/opentelemetry-matlab/common/ProcessedAttributes.h b/api/common/include/opentelemetry-matlab/common/ProcessedAttributes.h new file mode 100644 index 0000000..9b2c02a --- /dev/null +++ b/api/common/include/opentelemetry-matlab/common/ProcessedAttributes.h @@ -0,0 +1,21 @@ +// Copyright 2023 The MathWorks, Inc. + +#pragma once + +#include "opentelemetry/common/attribute_value.h" +#include "opentelemetry/nostd/string_view.h" + +#include + +namespace common = opentelemetry::common; +namespace nostd = opentelemetry::nostd; + +namespace libmexclass::opentelemetry { + +struct ProcessedAttributes { + std::list > Attributes; + std::list > DimensionsBuffer; // list of vector, to hold the dimensions of array attributes + std::list StringBuffer; // list of strings as a buffer to hold the string attributes + std::list > StringViewBuffer; // list of vector of strings views, used for string array attributes only +}; +} // namespace libmexclass::opentelemetry diff --git a/api/common/include/opentelemetry-matlab/common/attribute.h b/api/common/include/opentelemetry-matlab/common/attribute.h new file mode 100644 index 0000000..6236667 --- /dev/null +++ b/api/common/include/opentelemetry-matlab/common/attribute.h @@ -0,0 +1,22 @@ +// Copyright 2023 The MathWorks, Inc. + +#pragma once +#include "opentelemetry-matlab/common/ProcessedAttributes.h" + +#include "opentelemetry/common/attribute_value.h" +#include "opentelemetry/nostd/string_view.h" + +#include "MatlabDataArray.hpp" + +#include + +namespace common = opentelemetry::common; +namespace nostd = opentelemetry::nostd; + +namespace libmexclass::opentelemetry { + +void processAttribute(const std::string& attrname, // input, attribute name + const matlab::data::Array& attrvalue, // input, unprocessed attribute value + ProcessedAttributes& attrs); // output, processed attributes struct + +} // namespace libmexclass::opentelemetry diff --git a/api/trace/src/attribute.cpp b/api/common/src/attribute.cpp similarity index 57% rename from api/trace/src/attribute.cpp rename to api/common/src/attribute.cpp index c15ea39..f21970f 100644 --- a/api/trace/src/attribute.cpp +++ b/api/common/src/attribute.cpp @@ -1,7 +1,7 @@ // Copyright 2023 The MathWorks, Inc. -#include "opentelemetry-matlab/trace/attribute.h" +#include "opentelemetry-matlab/common/attribute.h" #include "opentelemetry/nostd/span.h" @@ -9,10 +9,7 @@ namespace libmexclass::opentelemetry { void processAttribute(const std::string& attrname, // input, attribute name const matlab::data::Array& attrvalue, // input, unprocessed attribute value - std::list >& attrs, // output, processed attribute name-value pair - std::list& string_buffer, // buffer to store processed string attribute values - std::list >& stringview_buffer, // buffer used only for string array attributes - std::list >& dimensions_buffer) // buffer for array dimensions of nonscalar attribute values + ProcessedAttributes& attrs) // output, processed attribute struct { std::vector attrvalue_dims_buffer; // dimensions of array attribute, cast to double @@ -24,45 +21,44 @@ void processAttribute(const std::string& attrname, // input, attribute name if (nelements == 1) { // scalar case if (valtype == matlab::data::ArrayType::DOUBLE) { matlab::data::TypedArray attrvalue_mda = attrvalue; - attrs.push_back(std::pair(attrname, attrvalue_mda[0])); + attrs.Attributes.push_back(std::pair(attrname, attrvalue_mda[0])); } else if (valtype == matlab::data::ArrayType::INT32) { matlab::data::TypedArray attrvalue_mda = attrvalue; - attrs.push_back(std::pair(attrname, attrvalue_mda[0])); + attrs.Attributes.push_back(std::pair(attrname, attrvalue_mda[0])); } else if (valtype == matlab::data::ArrayType::UINT32) { matlab::data::TypedArray attrvalue_mda = attrvalue; - attrs.push_back(std::pair(attrname, attrvalue_mda[0])); + attrs.Attributes.push_back(std::pair(attrname, attrvalue_mda[0])); } else if (valtype == matlab::data::ArrayType::INT64) { matlab::data::TypedArray attrvalue_mda = attrvalue; - attrs.push_back(std::pair(attrname, attrvalue_mda[0])); + attrs.Attributes.push_back(std::pair(attrname, attrvalue_mda[0])); } else if (valtype == matlab::data::ArrayType::LOGICAL) { matlab::data::TypedArray attrvalue_mda = attrvalue; - attrs.push_back(std::pair(attrname, attrvalue_mda[0])); + attrs.Attributes.push_back(std::pair(attrname, attrvalue_mda[0])); } else { // string matlab::data::StringArray attrvalue_mda = attrvalue; - //string_buffer.push_back(static_cast(attrvalue_mda[0])); - string_buffer.push_back(static_cast(*(attrvalue_mda.begin()))); - attrs.push_back(std::pair(attrname, string_buffer.back())); + attrs.StringBuffer.push_back(static_cast(*(attrvalue_mda.begin()))); + attrs.Attributes.push_back(std::pair(attrname, attrs.StringBuffer.back())); } } else { // array case if (valtype == matlab::data::ArrayType::DOUBLE) { matlab::data::TypedArray attrvalue_mda = attrvalue; - attrs.push_back(std::pair(attrname, + attrs.Attributes.push_back(std::pair(attrname, nostd::span{&(*attrvalue_mda.cbegin()), &(*attrvalue_mda.cend())})); } else if (valtype == matlab::data::ArrayType::INT32) { matlab::data::TypedArray attrvalue_mda = attrvalue; - attrs.push_back(std::pair(attrname, + attrs.Attributes.push_back(std::pair(attrname, nostd::span{&(*attrvalue_mda.cbegin()), &(*attrvalue_mda.cend())})); } else if (valtype == matlab::data::ArrayType::UINT32) { matlab::data::TypedArray attrvalue_mda = attrvalue; - attrs.push_back(std::pair(attrname, + attrs.Attributes.push_back(std::pair(attrname, nostd::span{&(*attrvalue_mda.cbegin()), &(*attrvalue_mda.cend())})); } else if (valtype == matlab::data::ArrayType::INT64) { matlab::data::TypedArray attrvalue_mda = attrvalue; - attrs.push_back(std::pair(attrname, + attrs.Attributes.push_back(std::pair(attrname, nostd::span{&(*attrvalue_mda.cbegin()), &(*attrvalue_mda.cend())})); } else if (valtype == matlab::data::ArrayType::LOGICAL) { matlab::data::TypedArray attrvalue_mda = attrvalue; - attrs.push_back(std::pair(attrname, + attrs.Attributes.push_back(std::pair(attrname, nostd::span{&(*attrvalue_mda.cbegin()), &(*attrvalue_mda.cend())})); } else { // string matlab::data::StringArray attrvalue_mda = attrvalue; @@ -70,12 +66,12 @@ void processAttribute(const std::string& attrname, // input, attribute name strarray_attr.reserve(nelements); for (auto itr = attrvalue_mda.begin(); itr < attrvalue_mda.end(); ++itr) { - string_buffer.push_back(static_cast(*itr)); - strarray_attr.push_back(string_buffer.back()); + attrs.StringBuffer.push_back(static_cast(*itr)); + strarray_attr.push_back(attrs.StringBuffer.back()); } - stringview_buffer.push_back(strarray_attr); - attrs.push_back(std::pair(attrname, - nostd::span{&(*stringview_buffer.back().cbegin()), &(*stringview_buffer.back().cend())})); + attrs.StringViewBuffer.push_back(strarray_attr); + attrs.Attributes.push_back(std::pair(attrname, + nostd::span{&(*attrs.StringViewBuffer.back().cbegin()), &(*attrs.StringViewBuffer.back().cend())})); } // Add a size attribute to preserve the shape std::string sizeattr{attrname + ".size"}; @@ -83,10 +79,10 @@ void processAttribute(const std::string& attrname, // input, attribute name for (copyfrom = attrdims.begin(); copyfrom != attrdims.end(); ++copyfrom) { attrvalue_dims_buffer.push_back(static_cast(*copyfrom)); } - dimensions_buffer.push_back(attrvalue_dims_buffer); - attrs.push_back(std::pair(sizeattr, - nostd::span{&(*dimensions_buffer.back().cbegin()), - &(*dimensions_buffer.back().cend())})); + attrs.DimensionsBuffer.push_back(attrvalue_dims_buffer); + attrs.Attributes.push_back(std::pair(sizeattr, + nostd::span{&(*attrs.DimensionsBuffer.back().cbegin()), + &(*attrs.DimensionsBuffer.back().cend())})); } } } // namespace diff --git a/api/trace/+opentelemetry/+trace/Span.m b/api/trace/+opentelemetry/+trace/Span.m index 090d795..5698920 100644 --- a/api/trace/+opentelemetry/+trace/Span.m +++ b/api/trace/+opentelemetry/+trace/Span.m @@ -114,7 +114,7 @@ function addEvent(obj, eventname, varargin) eventtime = posixtime(datetime("now")); end - eventname = opentelemetry.utils.mustBeScalarString(eventname); + eventname = opentelemetry.common.mustBeScalarString(eventname); attrs = obj.processAttributes(varargin); obj.Proxy.addEvent(eventname, eventtime, attrs{:}); end @@ -132,7 +132,7 @@ function setStatus(obj, status, description) % new status is not valid, ignore return end - description = opentelemetry.utils.mustBeScalarString(description); + description = opentelemetry.common.mustBeScalarString(description); obj.Proxy.setStatus(status, description); end diff --git a/api/trace/+opentelemetry/+trace/Tracer.m b/api/trace/+opentelemetry/+trace/Tracer.m index db4a48a..772958f 100644 --- a/api/trace/+opentelemetry/+trace/Tracer.m +++ b/api/trace/+opentelemetry/+trace/Tracer.m @@ -121,7 +121,7 @@ end end - spname = opentelemetry.utils.mustBeScalarString(spname); + spname = opentelemetry.common.mustBeScalarString(spname); id = obj.Proxy.startSpan(spname, contextid, spankind, starttime, ... attributekeys, attributevalues, links{:}); spanproxy = libmexclass.proxy.Proxy("Name", ... diff --git a/api/trace/+opentelemetry/+trace/TracerProvider.m b/api/trace/+opentelemetry/+trace/TracerProvider.m index a7f03a7..6cccc5a 100644 --- a/api/trace/+opentelemetry/+trace/TracerProvider.m +++ b/api/trace/+opentelemetry/+trace/TracerProvider.m @@ -41,7 +41,7 @@ end % name, version, schema accepts any types that can convert to a % string - import opentelemetry.utils.mustBeScalarString + import opentelemetry.common.mustBeScalarString trname = mustBeScalarString(trname); trversion = mustBeScalarString(trversion); trschema = mustBeScalarString(trschema); diff --git a/api/trace/include/opentelemetry-matlab/trace/TracerProviderProxy.h b/api/trace/include/opentelemetry-matlab/trace/TracerProviderProxy.h index cb18287..e6d0524 100644 --- a/api/trace/include/opentelemetry-matlab/trace/TracerProviderProxy.h +++ b/api/trace/include/opentelemetry-matlab/trace/TracerProviderProxy.h @@ -14,8 +14,6 @@ #include "opentelemetry/trace/provider.h" #include "opentelemetry/trace/noop.h" -#define OTEL_MATLAB_VERSION "0.1.0" - namespace trace_api = opentelemetry::trace; namespace trace_sdk = opentelemetry::sdk::trace; namespace trace_exporter = opentelemetry::exporter::otlp; diff --git a/api/trace/include/opentelemetry-matlab/trace/attribute.h b/api/trace/include/opentelemetry-matlab/trace/attribute.h deleted file mode 100644 index b04fd23..0000000 --- a/api/trace/include/opentelemetry-matlab/trace/attribute.h +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright 2023 The MathWorks, Inc. - -#pragma once - -#include "opentelemetry/common/attribute_value.h" -#include "opentelemetry/nostd/string_view.h" - -#include "MatlabDataArray.hpp" - -#include - -namespace common = opentelemetry::common; -namespace nostd = opentelemetry::nostd; - -namespace libmexclass::opentelemetry { - -void processAttribute(const std::string& attrname, // input, attribute name - const matlab::data::Array& attrvalue, // input, unprocessed attribute value - std::list >& attrs, // output, processed attribute name-value pair - std::list& string_buffer, // buffer to store processed string attribute values - // Buffers are necessary to persist these processed attribute - // values beyond the scope of this function - // Lists are used to ensure no memory reallocation as it grows, - // which happens with vectors - std::list >& stringview_buffer, // buffer used only for string array attributes - std::list >& dimensions_buffer); // buffer for array dimensions of nonscalar attribute values - -} // namespace libmexclass::opentelemetry diff --git a/api/trace/src/SpanProxy.cpp b/api/trace/src/SpanProxy.cpp index 6678ecc..d09a047 100644 --- a/api/trace/src/SpanProxy.cpp +++ b/api/trace/src/SpanProxy.cpp @@ -3,7 +3,7 @@ #include "opentelemetry-matlab/trace/SpanProxy.h" #include "opentelemetry-matlab/trace/ScopeProxy.h" #include "opentelemetry-matlab/trace/SpanContextProxy.h" -#include "opentelemetry-matlab/trace/attribute.h" +#include "opentelemetry-matlab/common/attribute.h" #include "opentelemetry-matlab/context/ContextProxy.h" #include "libmexclass/proxy/ProxyManager.h" @@ -73,14 +73,10 @@ void SpanProxy::setAttribute(libmexclass::proxy::method::Context& context) { std::string attrname = static_cast(attrname_mda[0]); matlab::data::Array attrvalue = context.inputs[1]; - std::list > spanattrs; - std::list stringattrs; // list of strings as a buffer to hold the string attributes - std::list > stringviews; // list of vector of strings views, used for string array attributes only - std::list > attrdims_double; // list of vectors, to hold the dimensions of array attributes - - processAttribute(attrname, attrvalue, spanattrs, stringattrs, stringviews, attrdims_double); + ProcessedAttributes attrs; + processAttribute(attrname, attrvalue, attrs); - for (auto itr = spanattrs.cbegin(); itr!=spanattrs.cend(); ++itr) { + for (auto itr = attrs.Attributes.cbegin(); itr!=attrs.Attributes.cend(); ++itr) { CppSpan->SetAttribute(itr->first, itr->second); } } @@ -93,21 +89,18 @@ void SpanProxy::addEvent(libmexclass::proxy::method::Context& context) { common::SystemTimestamp eventtime{std::chrono::duration{eventtime_mda[0]}}; size_t nin = context.inputs.getNumberOfElements(); // attributes - std::list > eventattrs; - std::list > attrdims_double; // list of vector, to hold the dimensions of array attributes - std::list stringattrs; // list of strings as a buffer to hold the string attributes - std::list > stringviews; // list of vector of strings views, used for string array attributes only + ProcessedAttributes eventattrs; for (size_t i = 2, count = 0; i < nin; i += 2, ++count) { matlab::data::StringArray attrname_mda = context.inputs[i]; std::string attrname = static_cast(attrname_mda[0]); matlab::data::Array attrvalue = context.inputs[i+1]; - processAttribute(attrname, attrvalue, eventattrs, stringattrs, stringviews, attrdims_double); + processAttribute(attrname, attrvalue, eventattrs); } if (nin < 3) { CppSpan->AddEvent(eventname, eventtime); } else { - CppSpan->AddEvent(eventname, eventtime, eventattrs); + CppSpan->AddEvent(eventname, eventtime, eventattrs.Attributes); } } diff --git a/api/trace/src/TracerProxy.cpp b/api/trace/src/TracerProxy.cpp index 8d8ca65..e6260a0 100644 --- a/api/trace/src/TracerProxy.cpp +++ b/api/trace/src/TracerProxy.cpp @@ -3,7 +3,7 @@ #include "opentelemetry-matlab/trace/TracerProxy.h" #include "opentelemetry-matlab/trace/SpanProxy.h" #include "opentelemetry-matlab/trace/SpanContextProxy.h" -#include "opentelemetry-matlab/trace/attribute.h" +#include "opentelemetry-matlab/common/attribute.h" #include "opentelemetry-matlab/context/ContextProxy.h" #include "libmexclass/proxy/ProxyManager.h" @@ -62,19 +62,17 @@ void TracerProxy::startSpan(libmexclass::proxy::method::Context& context) { } // attributes - std::list > attrs; - std::list > attrdims_double; // list of vectors, to hold the dimensions of array attributes - std::list stringattrs; // list of strings as a buffer to hold the string attributes - std::list > stringviews; // list of vector of strings views, used for string array attributes only + ProcessedAttributes attrs; for (size_t i = 0; i < nattrs; ++i) { std::string attrname = static_cast(attrnames_mda[i]); matlab::data::Array attrvalue = attrvalues_mda[i]; - processAttribute(attrname, attrvalue, attrs, stringattrs, stringviews, attrdims_double); + processAttribute(attrname, attrvalue, attrs); } // links std::list > > > links; + ProcessedAttributes linkattrs; for (size_t i = nfixedinputs; i < ninputs; i+=3) { // link target matlab::data::TypedArray linktargetid_mda = context.inputs[i]; @@ -83,7 +81,6 @@ void TracerProxy::startSpan(libmexclass::proxy::method::Context& context) { libmexclass::proxy::ProxyManager::getProxy(linktargetid)); // link attributes - std::list > linkattrs; matlab::data::StringArray linkattrnames_mda = context.inputs[i+1]; size_t nlinkattrs = linkattrnames_mda.getNumberOfElements(); matlab::data::Array linkattrvalues_mda = context.inputs[i+2]; @@ -91,12 +88,12 @@ void TracerProxy::startSpan(libmexclass::proxy::method::Context& context) { std::string linkattrname = static_cast(linkattrnames_mda[ii]); matlab::data::Array linkattrvalue = linkattrvalues_mda[ii]; - processAttribute(linkattrname, linkattrvalue, linkattrs, stringattrs, stringviews, attrdims_double); + processAttribute(linkattrname, linkattrvalue, linkattrs); } - links.push_back(std::pair(linktarget->getInstance(), linkattrs)); + links.push_back(std::pair(linktarget->getInstance(), linkattrs.Attributes)); } - auto sp = CppTracer->StartSpan(name, attrs, links, options); + auto sp = CppTracer->StartSpan(name, attrs.Attributes, links, options); // instantiate a SpanProxy instance SpanProxy* newproxy = new SpanProxy(sp); diff --git a/sdk/common/include/opentelemetry-matlab/sdk/common/resource.h b/sdk/common/include/opentelemetry-matlab/sdk/common/resource.h new file mode 100644 index 0000000..41a04a5 --- /dev/null +++ b/sdk/common/include/opentelemetry-matlab/sdk/common/resource.h @@ -0,0 +1,16 @@ +// Copyright 2023 The MathWorks, Inc. + +#pragma once + +#include "opentelemetry/sdk/resource/resource.h" + +#include "MatlabDataArray.hpp" + +namespace resource = opentelemetry::sdk::resource; + +namespace libmexclass::opentelemetry::sdk { + +resource::Resource createResource(const matlab::data::StringArray& resourcenames_mda, + const matlab::data::CellArray& resourcevalues_mda); + +} // namespace libmexclass::opentelemetry diff --git a/sdk/common/src/resource.cpp b/sdk/common/src/resource.cpp new file mode 100644 index 0000000..4ed3d6b --- /dev/null +++ b/sdk/common/src/resource.cpp @@ -0,0 +1,34 @@ +// Copyright 2023 The MathWorks, Inc. + +#include + +#include "opentelemetry-matlab/sdk/common/Resource.h" +#include "opentelemetry-matlab/common/attribute.h" + +#include "opentelemetry/common/attribute_value.h" +#include "opentelemetry/nostd/string_view.h" + +#define OTEL_MATLAB_VERSION "1.2.0" + +namespace common = opentelemetry::common; +namespace nostd = opentelemetry::nostd; + +namespace libmexclass::opentelemetry::sdk { + +resource::Resource createResource(const matlab::data::StringArray& resourcenames_mda, + const matlab::data::CellArray& resourcevalues_mda) { + size_t nresourceattrs = resourcenames_mda.getNumberOfElements(); + ProcessedAttributes resourceattrs; + for (size_t i = 0; i < nresourceattrs; ++i) { + std::string resourcename = static_cast(resourcenames_mda[i]); + matlab::data::Array resourcevalue = resourcevalues_mda[i]; + + processAttribute(resourcename, resourcevalue, resourceattrs); + } + resourceattrs.Attributes.push_back(std::pair("telemetry.sdk.language", "MATLAB")); + resourceattrs.Attributes.push_back(std::pair("telemetry.sdk.version", OTEL_MATLAB_VERSION)); + auto resource_custom = resource::Resource::Create(common::KeyValueIterableView{resourceattrs.Attributes}); + return std::move(resource_custom); +} + +} // namespace libmexclass::opentelemetry diff --git a/sdk/trace/src/TracerProviderProxy.cpp b/sdk/trace/src/TracerProviderProxy.cpp index f9b8d76..022ca8b 100644 --- a/sdk/trace/src/TracerProviderProxy.cpp +++ b/sdk/trace/src/TracerProviderProxy.cpp @@ -3,7 +3,8 @@ #include "opentelemetry-matlab/sdk/trace/TracerProviderProxy.h" #include "opentelemetry-matlab/sdk/trace/SpanProcessorProxy.h" #include "opentelemetry-matlab/sdk/trace/SamplerProxy.h" -#include "opentelemetry-matlab/trace/attribute.h" +#include "opentelemetry-matlab/sdk/common/resource.h" +#include "opentelemetry-matlab/common/attribute.h" #include "libmexclass/proxy/ProxyManager.h" @@ -50,25 +51,10 @@ libmexclass::proxy::MakeResult TracerProviderProxy::make(const libmexclass::prox auto sampler = std::static_pointer_cast( libmexclass::proxy::ProxyManager::getProxy(samplerid))->getInstance(); - // resource - std::list > resourceattrs; - std::list > resourcedims_double; // list of vector, to hold the dimensions of array attributes - std::list string_resource_attrs; // list of strings as a buffer to hold the string attributes - std::list > stringview_resource_attrs; // list of vector of stringviews, used for string array attributes only - for (size_t i = 0; i < nresourceattrs; ++i) { - std::string resourcename = static_cast(resourcenames_mda[i]); - matlab::data::Array resourcevalue = resourcevalues_mda[i]; - - processAttribute(resourcename, resourcevalue, resourceattrs, string_resource_attrs, stringview_resource_attrs, resourcedims_double); - } - auto resource_default = resource::Resource::Create({ {"telemetry.sdk.language", "MATLAB"}, - {"telemetry.sdk.version", OTEL_MATLAB_VERSION} }); - auto resource_custom = resource::Resource::Create(common::KeyValueIterableView{resourceattrs}); - // the order matters, default resource must come after custom. Otherwise the default resource will be overwritten. - auto resource_merged = resource_custom.Merge(resource_default); + auto resource_custom = createResource(resourcenames_mda, resourcevalues_mda); out = std::make_shared(nostd::shared_ptr( - std::move(trace_sdk::TracerProviderFactory::Create(std::move(processor), resource_merged, + std::move(trace_sdk::TracerProviderFactory::Create(std::move(processor), resource_custom, std::move(sampler))))); } return out; diff --git a/test/commonSetupOnce.m b/test/commonSetupOnce.m index 7a38918..1885696 100644 --- a/test/commonSetupOnce.m +++ b/test/commonSetupOnce.m @@ -23,7 +23,7 @@ function commonSetupOnce(testCase) windows_killroot = string(getenv("WINDOWS_KILL_INSTALL")); assert(~isempty(windows_killroot), "WINDOWS_KILL_INSTALL environment must be defined.") testCase.Sigint = @(id)fullfile(windows_killroot,"windows-kill") + " -SIGINT " + id; - testCase.Sigterm = @(id)"taskkill /pid " + id; + testCase.Sigterm = @(id)"taskkill /F /pid " + id; elseif isunix && ~ismac testCase.ListPid = @(name)"ps -C " + name; testCase.ReadPidList = @readtable; diff --git a/test/ttrace.m b/test/ttrace.m index 9dfaaca..115fe3d 100644 --- a/test/ttrace.m +++ b/test/ttrace.m @@ -86,7 +86,7 @@ function testBasic(testCase) versionidx = find(resourcekeys == "telemetry.sdk.version"); verifyNotEmpty(testCase, versionidx); - verifyEqual(testCase, results.resourceSpans.resource.attributes(versionidx).value.stringValue, '0.1.0'); + verifyEqual(testCase, results.resourceSpans.resource.attributes(versionidx).value.stringValue, '1.2.0'); nameidx = find(resourcekeys == "telemetry.sdk.name"); verifyNotEmpty(testCase, nameidx); @@ -550,26 +550,26 @@ function testLinks(testCase) % two links, with attributes % first link verifyLength(testCase, results{2}.resourceSpans.scopeSpans.spans.links, 2); % 2 links - verifyEqual(testCase, string(results{2}.resourceSpans.scopeSpans.spans.links{1}.traceId), ctxt1.TraceId); - verifyEqual(testCase, string(results{2}.resourceSpans.scopeSpans.spans.links{1}.spanId), ctxt1.SpanId); + verifyEqual(testCase, string(results{2}.resourceSpans.scopeSpans.spans.links(1).traceId), ctxt1.TraceId); + verifyEqual(testCase, string(results{2}.resourceSpans.scopeSpans.spans.links(1).spanId), ctxt1.SpanId); - linkattrkeys = string({results{2}.resourceSpans.scopeSpans.spans.links{1}.attributes.key}); + linkattrkeys = string({results{2}.resourceSpans.scopeSpans.spans.links(1).attributes.key}); stringscidx = find(linkattrkeys == "StringScalar"); verifyNotEmpty(testCase, stringscidx); - verifyEqual(testCase, string(results{2}.resourceSpans.scopeSpans.spans.links{1}.attributes(stringscidx).value.stringValue), ... + verifyEqual(testCase, string(results{2}.resourceSpans.scopeSpans.spans.links(1).attributes(stringscidx).value.stringValue), ... l2attributes{2}); doublearidx = find(linkattrkeys == "DoubleArray"); verifyNotEmpty(testCase, doublearidx); - verifyEqual(testCase, [results{2}.resourceSpans.scopeSpans.spans.links{1}.attributes(doublearidx).value.arrayValue.values.doubleValue], ... + verifyEqual(testCase, [results{2}.resourceSpans.scopeSpans.spans.links(1).attributes(doublearidx).value.arrayValue.values.doubleValue], ... reshape(l2attributes{4},1,[])); doubleszidx = find(linkattrkeys == "DoubleArray.size"); verifyNotEmpty(testCase, doubleszidx); - verifyEqual(testCase, [results{2}.resourceSpans.scopeSpans.spans.links{1}.attributes(doubleszidx).value.arrayValue.values.doubleValue], ... + verifyEqual(testCase, [results{2}.resourceSpans.scopeSpans.spans.links(1).attributes(doubleszidx).value.arrayValue.values.doubleValue], ... size(l2attributes{4})); % second link - verifyEqual(testCase, string(results{2}.resourceSpans.scopeSpans.spans.links{2}.traceId), ctxt3.TraceId); - verifyEqual(testCase, string(results{2}.resourceSpans.scopeSpans.spans.links{2}.spanId), ctxt3.SpanId); + verifyEqual(testCase, string(results{2}.resourceSpans.scopeSpans.spans.links(2).traceId), ctxt3.TraceId); + verifyEqual(testCase, string(results{2}.resourceSpans.scopeSpans.spans.links(2).spanId), ctxt3.SpanId); end function testInvalidSpanInputs(testCase) From 81b0a1a21e9d1987c7fdbf72a029211b8f3c255f Mon Sep 17 00:00:00 2001 From: duncanpo Date: Thu, 17 Aug 2023 15:09:10 -0400 Subject: [PATCH 02/11] fixing a fallout from previous change --- sdk/common/src/resource.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/common/src/resource.cpp b/sdk/common/src/resource.cpp index 4ed3d6b..7e74227 100644 --- a/sdk/common/src/resource.cpp +++ b/sdk/common/src/resource.cpp @@ -2,7 +2,7 @@ #include -#include "opentelemetry-matlab/sdk/common/Resource.h" +#include "opentelemetry-matlab/sdk/common/resource.h" #include "opentelemetry-matlab/common/attribute.h" #include "opentelemetry/common/attribute_value.h" From 461f05cd645ab566f3247e153769f7f966813318 Mon Sep 17 00:00:00 2001 From: duncanpo Date: Fri, 25 Aug 2023 12:45:16 -0400 Subject: [PATCH 03/11] Enable building OpenTelemetry-cpp as an external project --- CMakeLists.txt | 64 +++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 55 insertions(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 9cd97df..fc58e7c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,12 @@ -cmake_minimum_required(VERSION 3.0.0) +cmake_minimum_required(VERSION 3.7.0) cmake_policy(SET CMP0074 NEW) +# Autodetect vcpkg toolchain from VCPKG_ROOT environment variable +if(DEFINED ENV{VCPKG_ROOT} AND NOT DEFINED CMAKE_TOOLCHAIN_FILE) + string(REPLACE "\\" "/" CMAKE_TOOLCHAIN_FILE "$ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake") +endif() + set(CLIENT_PROJECT_NAME otel-matlab) project(${CLIENT_PROJECT_NAME} VERSION 0.1.0) @@ -39,6 +44,29 @@ FetchContent_MakeAvailable( ${LIBMEXCLASS_FETCH_CONTENT_NAME} ) +# ########################### +# OpenTelemetry-cpp +# ########################### +include(ExternalProject) +set(OTEL_CPP_PROJECT_NAME opentelemetry-cpp) +set(OTEL_CPP_GIT_REPOSITORY "https://github.com/open-telemetry/opentelemetry-cpp.git") +set(OTEL_CPP_GIT_TAG "11d5d9e") + +if(DEFINED OTEL_CPP_PREFIX) + string(REPLACE "\\" "/" OTEL_CPP_PREFIX ${OTEL_CPP_PREFIX}) +else() + set(OTEL_CPP_PREFIX ${CMAKE_BINARY_DIR}/otel-cpp) +endif() +ExternalProject_Add( + ${OTEL_CPP_PROJECT_NAME} + GIT_REPOSITORY ${OTEL_CPP_GIT_REPOSITORY} + GIT_TAG ${OTEL_CPP_GIT_TAG} + PREFIX ${OTEL_CPP_PREFIX} + CMAKE_ARGS -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DWITH_OTLP_HTTP=${WITH_OTLP_HTTP} -DWITH_OTLP_GRPC=${WITH_OTLP_GRPC} -DBUILD_TESTING=OFF -DWITH_BENCHMARK=OFF -DOPENTELEMETRY_INSTALL=ON -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE} + INSTALL_DIR ${OTEL_CPP_PREFIX} + INSTALL_COMMAND ${CMAKE_COMMAND} --install . --prefix ${OTEL_CPP_PREFIX} +) + # ########################### # OpenTelemetry Proxy Library # ########################### @@ -46,8 +74,6 @@ FetchContent_MakeAvailable( set(OPENTELEMETRY_PROXY_LIBRARY_NAME "OtelMatlabProxy") -# Specify location for find_package to locate opentelemetry-cpp-config.cmake -find_package(opentelemetry-cpp CONFIG REQUIRED) find_package(Protobuf REQUIRED) find_package(nlohmann_json REQUIRED) if(WIN32) @@ -77,7 +103,7 @@ set(COMMON_API_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/api/common/include) set(TRACE_SDK_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/sdk/trace/include) set(COMMON_SDK_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/sdk/common/include) set(OTLP_EXPORTER_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/exporters/otlp/include) -set(OPENTELEMETRY_PROXY_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR} ${TRACE_API_INCLUDE_DIR} ${CONTEXT_API_INCLUDE_DIR} ${BAGGAGE_API_INCLUDE_DIR} ${COMMON_API_INCLUDE_DIR} ${TRACE_SDK_INCLUDE_DIR} ${COMMON_SDK_INCLUDE_DIR} ${OTLP_EXPORTER_INCLUDE_DIR} ${OPENTELEMETRY_CPP_INCLUDE_DIRS}) +set(OPENTELEMETRY_PROXY_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR} ${TRACE_API_INCLUDE_DIR} ${CONTEXT_API_INCLUDE_DIR} ${BAGGAGE_API_INCLUDE_DIR} ${COMMON_API_INCLUDE_DIR} ${TRACE_SDK_INCLUDE_DIR} ${COMMON_SDK_INCLUDE_DIR} ${OTLP_EXPORTER_INCLUDE_DIR} ${OTEL_CPP_PREFIX}/include) set(OPENTELEMETRY_PROXY_FACTORY_CLASS_NAME OtelMatlabProxyFactory) set(OPENTELEMETRY_PROXY_FACTORY_SOURCES_DIR ${CMAKE_CURRENT_SOURCE_DIR}) @@ -140,13 +166,33 @@ endif() target_compile_options(${OPENTELEMETRY_PROXY_LIBRARY_NAME} PRIVATE ${OTLP_MACROS}) # link against OpenTelemetry-cpp libraries and their dependencies -target_link_libraries(${OPENTELEMETRY_PROXY_LIBRARY_NAME} PRIVATE ${OPENTELEMETRY_CPP_LIBRARIES}) +target_link_libraries(${OPENTELEMETRY_PROXY_LIBRARY_NAME} PRIVATE ${OTEL_CPP_PREFIX}/lib/opentelemetry_common${CMAKE_LINK_LIBRARY_SUFFIX} + ${OTEL_CPP_PREFIX}/lib/opentelemetry_otlp_recordable${CMAKE_LINK_LIBRARY_SUFFIX} + ${OTEL_CPP_PREFIX}/lib/opentelemetry_proto${CMAKE_LINK_LIBRARY_SUFFIX} + ${OTEL_CPP_PREFIX}/lib/opentelemetry_resources${CMAKE_LINK_LIBRARY_SUFFIX} + ${OTEL_CPP_PREFIX}/lib/opentelemetry_trace${CMAKE_LINK_LIBRARY_SUFFIX} + ${OTEL_CPP_PREFIX}/lib/opentelemetry_version${CMAKE_LINK_LIBRARY_SUFFIX} + ${OTEL_CPP_PREFIX}/lib/opentelemetry_logs${CMAKE_LINK_LIBRARY_SUFFIX} + ${Protobuf_LIBRARIES}) +if(WITH_OTLP_HTTP) + target_link_libraries(${OPENTELEMETRY_PROXY_LIBRARY_NAME} PRIVATE ${OTEL_CPP_PREFIX}/lib/opentelemetry_exporter_otlp_http${CMAKE_LINK_LIBRARY_SUFFIX} + ${OTEL_CPP_PREFIX}/lib/opentelemetry_exporter_otlp_http_client${CMAKE_LINK_LIBRARY_SUFFIX} + ${OTEL_CPP_PREFIX}/lib/opentelemetry_http_client_curl${CMAKE_LINK_LIBRARY_SUFFIX} + ${CURL_LIBRARIES}) +endif() +if(WITH_OTLP_GRPC) + target_link_libraries(${OPENTELEMETRY_PROXY_LIBRARY_NAME} PRIVATE ${OTEL_CPP_PREFIX}/lib/opentelemetry_exporter_otlp_grpc${CMAKE_LINK_LIBRARY_SUFFIX} + ${OTEL_CPP_PREFIX}/lib/opentelemetry_exporter_otlp_grpc_client${CMAKE_LINK_LIBRARY_SUFFIX} + ${OTEL_CPP_PREFIX}/lib/opentelemetry_proto_grpc${CMAKE_LINK_LIBRARY_SUFFIX} + gRPC::grpc++ + absl::synchronization) +endif() # On Linux, when linking with certain static libraries, need to force include entire archive to avoid the linker mistakenly leaving out symbols if(UNIX AND NOT APPLE AND NOT CYGWIN) set(OPENTELEMETRY_PROXY_LINK_OPTIONS -Wl,--whole-archive - "${OPENTELEMETRY_CPP_LIBRARY_DIRS}/libopentelemetry_trace.a" - "${OPENTELEMETRY_CPP_LIBRARY_DIRS}/libopentelemetry_common.a" + "${OTEL_CPP_PREFIX}/lib/libopentelemetry_trace.a" + "${OTEL_CPP_PREFIX}/lib/libopentelemetry_common.a" ${ABSL_LIBRARIES} ${UPB_LIBRARIES} -Wl,--no-whole-archive) target_link_options(${OPENTELEMETRY_PROXY_LIBRARY_NAME} PRIVATE ${OPENTELEMETRY_PROXY_LINK_OPTIONS}) @@ -184,10 +230,10 @@ if(WIN32) ${RE2_RUNTIME}) endif() elseif(UNIX AND NOT APPLE AND NOT CYGWIN) - FILE(GLOB OTEL_CPP_RUNTIME ${OPENTELEMETRY_CPP_LIBRARY_DIRS}/*.so) + FILE(GLOB OTEL_CPP_RUNTIME ${OTEL_CPP_PREFIX}/lib/*.so) set(OPENTELEMETRY_PROXY_RUNTIME_LIBRARIES ${OTEL_CPP_RUNTIME}) elseif(APPLE) - FILE(GLOB OTEL_CPP_RUNTIME ${OPENTELEMETRY_CPP_LIBRARY_DIRS}/*.dylib) + FILE(GLOB OTEL_CPP_RUNTIME ${OTEL_CPP_PREFIX}/lib/*.dylib) set(OPENTELEMETRY_PROXY_RUNTIME_LIBRARIES ${OTEL_CPP_RUNTIME}) endif() From f2d6a5c93581c828c7767f035a539de911574936 Mon Sep 17 00:00:00 2001 From: Duncan Po Date: Fri, 25 Aug 2023 15:30:20 -0400 Subject: [PATCH 04/11] Enable building otel-cpp as external project on Linux --- CMakeLists.txt | 42 ++++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index fc58e7c..0e2c87d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -57,12 +57,19 @@ if(DEFINED OTEL_CPP_PREFIX) else() set(OTEL_CPP_PREFIX ${CMAKE_BINARY_DIR}/otel-cpp) endif() + +if(WITH_OTLP_GRPC) + set(OTEL_CPP_CXX_STANDARD 14) # Abseil requires at least Cxx14 +else() + set(OTEL_CPP_CXX_STANDARD 11) +endif() + ExternalProject_Add( ${OTEL_CPP_PROJECT_NAME} GIT_REPOSITORY ${OTEL_CPP_GIT_REPOSITORY} GIT_TAG ${OTEL_CPP_GIT_TAG} PREFIX ${OTEL_CPP_PREFIX} - CMAKE_ARGS -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DWITH_OTLP_HTTP=${WITH_OTLP_HTTP} -DWITH_OTLP_GRPC=${WITH_OTLP_GRPC} -DBUILD_TESTING=OFF -DWITH_BENCHMARK=OFF -DOPENTELEMETRY_INSTALL=ON -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE} + CMAKE_ARGS -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DWITH_OTLP_HTTP=${WITH_OTLP_HTTP} -DWITH_OTLP_GRPC=${WITH_OTLP_GRPC} -DBUILD_TESTING=OFF -DWITH_BENCHMARK=OFF -DOPENTELEMETRY_INSTALL=ON -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE} -DCMAKE_CXX_STANDARD=${OTEL_CPP_CXX_STANDARD} INSTALL_DIR ${OTEL_CPP_PREFIX} INSTALL_COMMAND ${CMAKE_COMMAND} --install . --prefix ${OTEL_CPP_PREFIX} ) @@ -163,27 +170,33 @@ if(WITH_OTLP_GRPC) endif() endif() +if(WIN32) + set(OTEL_PROTO_LIBRARY_SUFFIX ${CMAKE_STATIC_LIBRARY_SUFFIX}) +else() + set(OTEL_PROTO_LIBRARY_SUFFIX ${CMAKE_SHARED_LIBRARY_SUFFIX}) +endif() + target_compile_options(${OPENTELEMETRY_PROXY_LIBRARY_NAME} PRIVATE ${OTLP_MACROS}) # link against OpenTelemetry-cpp libraries and their dependencies -target_link_libraries(${OPENTELEMETRY_PROXY_LIBRARY_NAME} PRIVATE ${OTEL_CPP_PREFIX}/lib/opentelemetry_common${CMAKE_LINK_LIBRARY_SUFFIX} - ${OTEL_CPP_PREFIX}/lib/opentelemetry_otlp_recordable${CMAKE_LINK_LIBRARY_SUFFIX} - ${OTEL_CPP_PREFIX}/lib/opentelemetry_proto${CMAKE_LINK_LIBRARY_SUFFIX} - ${OTEL_CPP_PREFIX}/lib/opentelemetry_resources${CMAKE_LINK_LIBRARY_SUFFIX} - ${OTEL_CPP_PREFIX}/lib/opentelemetry_trace${CMAKE_LINK_LIBRARY_SUFFIX} - ${OTEL_CPP_PREFIX}/lib/opentelemetry_version${CMAKE_LINK_LIBRARY_SUFFIX} - ${OTEL_CPP_PREFIX}/lib/opentelemetry_logs${CMAKE_LINK_LIBRARY_SUFFIX} +target_link_libraries(${OPENTELEMETRY_PROXY_LIBRARY_NAME} PRIVATE ${OTEL_CPP_PREFIX}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}opentelemetry_common${CMAKE_STATIC_LIBRARY_SUFFIX} + ${OTEL_CPP_PREFIX}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}opentelemetry_otlp_recordable${CMAKE_STATIC_LIBRARY_SUFFIX} + ${OTEL_CPP_PREFIX}/lib/${CMAKE_SHARED_LIBRARY_PREFIX}opentelemetry_proto${OTEL_PROTO_LIBRARY_SUFFIX} + ${OTEL_CPP_PREFIX}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}opentelemetry_resources${CMAKE_STATIC_LIBRARY_SUFFIX} + ${OTEL_CPP_PREFIX}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}opentelemetry_trace${CMAKE_STATIC_LIBRARY_SUFFIX} + ${OTEL_CPP_PREFIX}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}opentelemetry_version${CMAKE_STATIC_LIBRARY_SUFFIX} + ${OTEL_CPP_PREFIX}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}opentelemetry_logs${CMAKE_STATIC_LIBRARY_SUFFIX} ${Protobuf_LIBRARIES}) if(WITH_OTLP_HTTP) - target_link_libraries(${OPENTELEMETRY_PROXY_LIBRARY_NAME} PRIVATE ${OTEL_CPP_PREFIX}/lib/opentelemetry_exporter_otlp_http${CMAKE_LINK_LIBRARY_SUFFIX} - ${OTEL_CPP_PREFIX}/lib/opentelemetry_exporter_otlp_http_client${CMAKE_LINK_LIBRARY_SUFFIX} - ${OTEL_CPP_PREFIX}/lib/opentelemetry_http_client_curl${CMAKE_LINK_LIBRARY_SUFFIX} + target_link_libraries(${OPENTELEMETRY_PROXY_LIBRARY_NAME} PRIVATE ${OTEL_CPP_PREFIX}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}opentelemetry_exporter_otlp_http${CMAKE_STATIC_LIBRARY_SUFFIX} + ${OTEL_CPP_PREFIX}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}opentelemetry_exporter_otlp_http_client${CMAKE_STATIC_LIBRARY_SUFFIX} + ${OTEL_CPP_PREFIX}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}opentelemetry_http_client_curl${CMAKE_STATIC_LIBRARY_SUFFIX} ${CURL_LIBRARIES}) endif() if(WITH_OTLP_GRPC) - target_link_libraries(${OPENTELEMETRY_PROXY_LIBRARY_NAME} PRIVATE ${OTEL_CPP_PREFIX}/lib/opentelemetry_exporter_otlp_grpc${CMAKE_LINK_LIBRARY_SUFFIX} - ${OTEL_CPP_PREFIX}/lib/opentelemetry_exporter_otlp_grpc_client${CMAKE_LINK_LIBRARY_SUFFIX} - ${OTEL_CPP_PREFIX}/lib/opentelemetry_proto_grpc${CMAKE_LINK_LIBRARY_SUFFIX} + target_link_libraries(${OPENTELEMETRY_PROXY_LIBRARY_NAME} PRIVATE ${OTEL_CPP_PREFIX}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}opentelemetry_exporter_otlp_grpc${CMAKE_STATIC_LIBRARY_SUFFIX} + ${OTEL_CPP_PREFIX}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}opentelemetry_exporter_otlp_grpc_client${CMAKE_STATIC_LIBRARY_SUFFIX} + ${OTEL_CPP_PREFIX}/lib/${CMAKE_SHARED_LIBRARY_PREFIX}opentelemetry_proto_grpc${OTEL_PROTO_LIBRARY_SUFFIX} gRPC::grpc++ absl::synchronization) endif() @@ -193,6 +206,7 @@ if(UNIX AND NOT APPLE AND NOT CYGWIN) set(OPENTELEMETRY_PROXY_LINK_OPTIONS -Wl,--whole-archive "${OTEL_CPP_PREFIX}/lib/libopentelemetry_trace.a" "${OTEL_CPP_PREFIX}/lib/libopentelemetry_common.a" + "${OTEL_CPP_PREFIX}/lib/libopentelemetry_otlp_recordable.a" ${ABSL_LIBRARIES} ${UPB_LIBRARIES} -Wl,--no-whole-archive) target_link_options(${OPENTELEMETRY_PROXY_LIBRARY_NAME} PRIVATE ${OPENTELEMETRY_PROXY_LINK_OPTIONS}) From cfede70a8886edbe0c52ffa02114f45e9b42ecb1 Mon Sep 17 00:00:00 2001 From: Duncan Po Date: Mon, 28 Aug 2023 17:11:13 -0400 Subject: [PATCH 05/11] update build for Mac OS --- CMakeLists.txt | 15 ++++++++++----- otel-cpp.patch | 13 +++++++++++++ 2 files changed, 23 insertions(+), 5 deletions(-) create mode 100644 otel-cpp.patch diff --git a/CMakeLists.txt b/CMakeLists.txt index 0e2c87d..c83b7d1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -64,11 +64,14 @@ else() set(OTEL_CPP_CXX_STANDARD 11) endif() +set(patch_command git apply ${CMAKE_SOURCE_DIR}/otel-cpp.patch) + ExternalProject_Add( ${OTEL_CPP_PROJECT_NAME} GIT_REPOSITORY ${OTEL_CPP_GIT_REPOSITORY} GIT_TAG ${OTEL_CPP_GIT_TAG} PREFIX ${OTEL_CPP_PREFIX} + PATCH_COMMAND ${patch_command} CMAKE_ARGS -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DWITH_OTLP_HTTP=${WITH_OTLP_HTTP} -DWITH_OTLP_GRPC=${WITH_OTLP_GRPC} -DBUILD_TESTING=OFF -DWITH_BENCHMARK=OFF -DOPENTELEMETRY_INSTALL=ON -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE} -DCMAKE_CXX_STANDARD=${OTEL_CPP_CXX_STANDARD} INSTALL_DIR ${OTEL_CPP_PREFIX} INSTALL_COMMAND ${CMAKE_COMMAND} --install . --prefix ${OTEL_CPP_PREFIX} @@ -243,11 +246,13 @@ if(WIN32) ${OPENSSL_RUNTIME} ${RE2_RUNTIME}) endif() -elseif(UNIX AND NOT APPLE AND NOT CYGWIN) - FILE(GLOB OTEL_CPP_RUNTIME ${OTEL_CPP_PREFIX}/lib/*.so) - set(OPENTELEMETRY_PROXY_RUNTIME_LIBRARIES ${OTEL_CPP_RUNTIME}) -elseif(APPLE) - FILE(GLOB OTEL_CPP_RUNTIME ${OTEL_CPP_PREFIX}/lib/*.dylib) +elseif(UNIX AND NOT CYGWIN) + if(WITH_OTLP_GRPC) + set(OTEL_CPP_RUNTIME ${OTEL_CPP_PREFIX}/lib/libopentelemetry_proto${CMAKE_SHARED_LIBRARY_SUFFIX} + ${OTEL_CPP_PREFIX}/lib/libopentelemetry_proto_grpc${CMAKE_SHARED_LIBRARY_SUFFIX}) + else() + set(OTEL_CPP_RUNTIME ${OTEL_CPP_PREFIX}/lib/libopentelemetry_proto${CMAKE_SHARED_LIBRARY_SUFFIX}) + endif() set(OPENTELEMETRY_PROXY_RUNTIME_LIBRARIES ${OTEL_CPP_RUNTIME}) endif() diff --git a/otel-cpp.patch b/otel-cpp.patch new file mode 100644 index 0000000..75c01cd --- /dev/null +++ b/otel-cpp.patch @@ -0,0 +1,13 @@ +diff --git a/cmake/opentelemetry-proto.cmake b/cmake/opentelemetry-proto.cmake +index 22082356..e25a89a2 100644 +--- a/cmake/opentelemetry-proto.cmake ++++ b/cmake/opentelemetry-proto.cmake +@@ -276,7 +276,7 @@ if(WITH_OTLP_GRPC) + PUBLIC opentelemetry_proto) + + get_target_property(grpc_lib_type gRPC::grpc++ TYPE) +- if (grpc_lib_type STREQUAL "SHARED_LIBRARY") ++ if (grpc_lib_type STREQUAL "SHARED_LIBRARY" OR APPLE) + target_link_libraries(opentelemetry_proto_grpc + PUBLIC gRPC::grpc++) + endif() From 8d77e9eb8573072241737eca8054988f192ee224 Mon Sep 17 00:00:00 2001 From: duncanpo Date: Tue, 29 Aug 2023 08:52:48 -0400 Subject: [PATCH 06/11] Update readme and github workflow to use simplified build instructions --- .github/workflows/build.yml | 15 ++------------- README.md | 15 ++++----------- 2 files changed, 6 insertions(+), 24 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 310c7d5..b25bc2c 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -9,18 +9,13 @@ jobs: OPENTELEMETRY_CPP_INSTALL: "${{ github.workspace }}/otel_cpp_install" OPENTELEMETRY_MATLAB_INSTALL: "${{ github.workspace }}/otel_matlab_install" OPENTELEMETRY_COLLECTOR_INSTALL: "${{ github.workspace }}/otelcol" + VCPKG_ROOT: "${{ github.workspace }}/vcpkg" SYSTEM_LIBSTDCPP_PATH: "/usr/lib/x86_64-linux-gnu/libstdc++.so.6" steps: - name: Download OpenTelemetry-Matlab source uses: actions/checkout@v3 with: path: opentelemetry-matlab - - name: Download OpenTelemetry-cpp source - uses: actions/checkout@v3 - with: - repository: open-telemetry/opentelemetry-cpp - path: opentelemetry-cpp - ref: v1.9.0 - name: Download vcpkg uses: actions/checkout@v3 with: @@ -38,16 +33,10 @@ jobs: mkdir otelcol && cd otelcol wget https://github.com/open-telemetry/opentelemetry-collector-releases/releases/download/v0.75.0/otelcol_0.75.0_linux_amd64.tar.gz tar -xzf otelcol_0.75.0_linux_amd64.tar.gz - - name: Build OpenTelemetry-cpp - run: | - cd opentelemetry-cpp - cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DCMAKE_CXX_STANDARD=20 -DWITH_OTLP=ON -DWITH_OTLP_HTTP=ON -DWITH_OTLP_GRPC=OFF -DBUILD_TESTING=OFF -DWITH_BENCHMARK=OFF -DOPENTELEMETRY_INSTALL=ON -DCMAKE_TOOLCHAIN_FILE=../vcpkg/scripts/buildsystems/vcpkg.cmake - cmake --build build --config Release --target all - cmake --install build --prefix ${{ env.OPENTELEMETRY_CPP_INSTALL }} - name: Build OpenTelemetry-Matlab run: | cd opentelemetry-matlab - cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=${{ env.OPENTELEMETRY_MATLAB_INSTALL }} -DCMAKE_TOOLCHAIN_FILE=../vcpkg/scripts/buildsystems/vcpkg.cmake -DCMAKE_PREFIX_PATH=${{ env.OPENTELEMETRY_CPP_INSTALL }}/lib/cmake/opentelemetry-cpp + cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=${{ env.OPENTELEMETRY_MATLAB_INSTALL }} cmake --build build --config Release --target install - name: Run tests env: diff --git a/README.md b/README.md index fe28573..63426f0 100644 --- a/README.md +++ b/README.md @@ -27,23 +27,16 @@ Before proceeding, ensure that the below products are installed: - protobuf - zlib -2. Download, build and install [OpenTelemetry C++](https://github.com/open-telemetry/opentelemetry-cpp) -``` -cd -cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DCMAKE_CXX_STANDARD=20 -DWITH_OTLP_HTTP=ON -DBUILD_TESTING=OFF -DWITH_BENCHMARK=OFF -DOPENTELEMETRY_INSTALL=ON -DCMAKE_TOOLCHAIN_FILE=\scripts\buildsystems\vcpkg.cmake -cmake --build build --config Release --target ALL_BUILD -cmake --install build --prefix -``` -3. Download OpenTelemetry MATLAB +2. Set VCPKG_ROOT environment variable to point to installed location of vcpkg. -4. Build and install OpenTelemetry MATLAB +3. Download, Build and install OpenTelemetry MATLAB ``` cd -cmake -S . -B build -DCMAKE_INSTALL_PREFIX= -DCMAKE_TOOLCHAIN_FILE=\scripts\buildsystems\vcpkg.cmake -DCMAKE_PREFIX_PATH= +cmake -S . -B build -DCMAKE_INSTALL_PREFIX= cmake --build build --config Release --target install ``` -5. Download [OpenTelemetry Collector](https://github.com/open-telemetry/opentelemetry-collector-releases/releases). You can just obtain a pre-built binary for your platform. +4. Download [OpenTelemetry Collector](https://github.com/open-telemetry/opentelemetry-collector-releases/releases). You can just obtain a pre-built binary for your platform. ## Getting Started 1. Start OpenTelemetry Collector From 8e1fe70852da6acd271c831f91ec8b7a00a93424 Mon Sep 17 00:00:00 2001 From: duncanpo Date: Tue, 5 Sep 2023 22:36:08 -0400 Subject: [PATCH 07/11] stop detecting updates in otel-cpp external project, which can cause patch failure --- CMakeLists.txt | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index c83b7d1..e4a1d39 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,6 +20,9 @@ option(WITH_OTLP_GRPC "Whether to include the OTLP gRPC exporter" OFF) if(NOT WITH_OTLP_HTTP AND NOT WITH_OTLP_GRPC) message(FATAL_ERROR "At least one of WITH_OTLP_HTTP and WITH_OTLP_GRPC must be ON") endif() +if(APPLE) + option(SKIP_OTEL_CPP_PATCH "Whether to skip patching OpenTelemetry-cpp" OFF) +endif() # ###################################### # libmexclass FetchContent Configuration @@ -64,13 +67,18 @@ else() set(OTEL_CPP_CXX_STANDARD 11) endif() -set(patch_command git apply ${CMAKE_SOURCE_DIR}/otel-cpp.patch) +if(NOT APPLE OR SKIP_OTEL_CPP_PATCH) + set(patch_command "") +else() + set(patch_command git apply ${CMAKE_SOURCE_DIR}/otel-cpp.patch) +endif() ExternalProject_Add( ${OTEL_CPP_PROJECT_NAME} GIT_REPOSITORY ${OTEL_CPP_GIT_REPOSITORY} GIT_TAG ${OTEL_CPP_GIT_TAG} PREFIX ${OTEL_CPP_PREFIX} + UPDATE_DISCONNECTED 1 PATCH_COMMAND ${patch_command} CMAKE_ARGS -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DWITH_OTLP_HTTP=${WITH_OTLP_HTTP} -DWITH_OTLP_GRPC=${WITH_OTLP_GRPC} -DBUILD_TESTING=OFF -DWITH_BENCHMARK=OFF -DOPENTELEMETRY_INSTALL=ON -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE} -DCMAKE_CXX_STANDARD=${OTEL_CPP_CXX_STANDARD} INSTALL_DIR ${OTEL_CPP_PREFIX} @@ -173,13 +181,20 @@ if(WITH_OTLP_GRPC) endif() endif() +# On Windows, suppress a compiler warning about deprecation of result_of +if(WIN32) + set(CUSTOM_CXX_FLAGS -D_SILENCE_CXX17_RESULT_OF_DEPRECATION_WARNING) +else() + set(CUSTOM_CXX_FLAGS "") +endif() + if(WIN32) set(OTEL_PROTO_LIBRARY_SUFFIX ${CMAKE_STATIC_LIBRARY_SUFFIX}) else() set(OTEL_PROTO_LIBRARY_SUFFIX ${CMAKE_SHARED_LIBRARY_SUFFIX}) endif() -target_compile_options(${OPENTELEMETRY_PROXY_LIBRARY_NAME} PRIVATE ${OTLP_MACROS}) +target_compile_options(${OPENTELEMETRY_PROXY_LIBRARY_NAME} PRIVATE ${OTLP_MACROS} ${CUSTOM_CXX_FLAGS}) # link against OpenTelemetry-cpp libraries and their dependencies target_link_libraries(${OPENTELEMETRY_PROXY_LIBRARY_NAME} PRIVATE ${OTEL_CPP_PREFIX}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}opentelemetry_common${CMAKE_STATIC_LIBRARY_SUFFIX} From 55ddd9a0433b70efec8c9e3a612c6453fd65402e Mon Sep 17 00:00:00 2001 From: duncanpo Date: Mon, 11 Sep 2023 22:30:49 -0400 Subject: [PATCH 08/11] Use fetchcontent to obtain vcpkg --- .github/workflows/build.yml | 11 --- CMakeLists.txt | 140 ++++++++++++++++++++++++++---------- README.md | 12 +--- vcpkg.json | 12 ++++ 4 files changed, 115 insertions(+), 60 deletions(-) create mode 100644 vcpkg.json diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index b25bc2c..f7e5ee2 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -9,25 +9,14 @@ jobs: OPENTELEMETRY_CPP_INSTALL: "${{ github.workspace }}/otel_cpp_install" OPENTELEMETRY_MATLAB_INSTALL: "${{ github.workspace }}/otel_matlab_install" OPENTELEMETRY_COLLECTOR_INSTALL: "${{ github.workspace }}/otelcol" - VCPKG_ROOT: "${{ github.workspace }}/vcpkg" SYSTEM_LIBSTDCPP_PATH: "/usr/lib/x86_64-linux-gnu/libstdc++.so.6" steps: - name: Download OpenTelemetry-Matlab source uses: actions/checkout@v3 with: path: opentelemetry-matlab - - name: Download vcpkg - uses: actions/checkout@v3 - with: - repository: microsoft/vcpkg - path: vcpkg - name: Install MATLAB uses: matlab-actions/setup-matlab@v1 - - name: Install vcpkg packages - run: | - cd vcpkg - ./bootstrap-vcpkg.sh - ./vcpkg install curl nlohmann-json protobuf zlib - name: Download OpenTelemetry Collector binary run: | mkdir otelcol && cd otelcol diff --git a/CMakeLists.txt b/CMakeLists.txt index e4a1d39..6896b93 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,14 +2,52 @@ cmake_minimum_required(VERSION 3.7.0) cmake_policy(SET CMP0074 NEW) -# Autodetect vcpkg toolchain from VCPKG_ROOT environment variable -if(DEFINED ENV{VCPKG_ROOT} AND NOT DEFINED CMAKE_TOOLCHAIN_FILE) - string(REPLACE "\\" "/" CMAKE_TOOLCHAIN_FILE "$ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake") -endif() -set(CLIENT_PROJECT_NAME otel-matlab) +# ########################### +# vcpkg +# ########################### -project(${CLIENT_PROJECT_NAME} VERSION 0.1.0) +include(FetchContent) + +# check if VCPKG_ROOT is defined, which should point to an existing installation +if(DEFINED ENV{VCPKG_ROOT}) + # Autodetect vcpkg toolchain + if(NOT DEFINED CMAKE_TOOLCHAIN_FILE) + string(REPLACE "\\" "/" CMAKE_TOOLCHAIN_FILE "$ENV{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake") + endif() +else() + # VCPKG_ROOT not defined, get it using FetchContent + + if(DEFINED VCPKG_PREFIX}) + # download location specified + string(REPLACE "\\" "/" VCPKG_PREFIX "${VCPKG_PREFIX}") + else() + set(VCPKG_PREFIX ${CMAKE_BINARY_DIR}/vcpkg) + endif() + + set(VCPKG_FETCH_CONTENT_NAME vcpkg) + set(VCPKG_GIT_REPOSITORY "https://github.com/microsoft/vcpkg.git") + set(VCPKG_GIT_TAG "9edb1b8") + FetchContent_Declare( + ${VCPKG_FETCH_CONTENT_NAME} + GIT_REPOSITORY ${VCPKG_GIT_REPOSITORY} + GIT_TAG ${VCPKG_GIT_TAG} + PREFIX ${VCPKG_PREFIX} + UPDATE_DISCONNECTED 1 + ) + + FetchContent_MakeAvailable( + ${VCPKG_FETCH_CONTENT_NAME} + ) + + FetchContent_GetProperties(${VCPKG_FETCH_CONTENT_NAME}) + string(REPLACE "\\" "/" CMAKE_TOOLCHAIN_FILE "${vcpkg_SOURCE_DIR}/scripts/buildsystems/vcpkg.cmake") + +endif() + +if(NOT DEFINED VCPKG_INSTALLED_DIR) + set(DVCPKG_INSTALLED_DIR ${CMAKE_BINARY_DIR}/vcpkg_installed) +endif() # ###################################### # Options @@ -24,8 +62,25 @@ if(APPLE) option(SKIP_OTEL_CPP_PATCH "Whether to skip patching OpenTelemetry-cpp" OFF) endif() +# set vcpkg features depending on specified options +set(VCPKG_MANIFEST_FEATURES "") # start with empty +if(WITH_OTLP_HTTP) + set(VCPKG_MANIFEST_FEATURES ${VCPKG_MANFIEST_FEATURES} "otlp-http") +endif() +if(WITH_OTLP_GRPC) + set(VCPKG_MANIFEST_FEATURES ${VCPKG_MANIFEST_FEATURES} "otlp-grpc") +endif() + # ###################################### -# libmexclass FetchContent Configuration +# Project Declaration +# ###################################### + +set(CLIENT_PROJECT_NAME otel-matlab) + +project(${CLIENT_PROJECT_NAME} VERSION 0.1.0) + +# ###################################### +# libmexclass # ###################################### set(LIBMEXCLASS_FETCH_CONTENT_NAME libmexclass) @@ -36,7 +91,6 @@ set(LIBMEXCLASS_FETCH_CONTENT_GIT_TAG "77f3d72") set(LIBMEXCLASS_FETCH_CONTENT_SOURCE_SUBDIR "libmexclass/cpp") -include(FetchContent) FetchContent_Declare( ${LIBMEXCLASS_FETCH_CONTENT_NAME} GIT_REPOSITORY ${LIBMEXCLASS_FETCH_CONTENT_GIT_REPOSITORY} @@ -47,44 +101,52 @@ FetchContent_MakeAvailable( ${LIBMEXCLASS_FETCH_CONTENT_NAME} ) + # ########################### # OpenTelemetry-cpp # ########################### -include(ExternalProject) -set(OTEL_CPP_PROJECT_NAME opentelemetry-cpp) -set(OTEL_CPP_GIT_REPOSITORY "https://github.com/open-telemetry/opentelemetry-cpp.git") -set(OTEL_CPP_GIT_TAG "11d5d9e") -if(DEFINED OTEL_CPP_PREFIX) - string(REPLACE "\\" "/" OTEL_CPP_PREFIX ${OTEL_CPP_PREFIX}) +if(DEFINED OTEL_CPP_INSTALLED_DIR) + # OTEL_CPP_INSTALLED_DIR should point to an installed location of OpenTelemetry-cpp + string(REPLACE "\\" "/" OTEL_CPP_PREFIX ${OTEL_CPP_INSTALLED_DIR}) else() - set(OTEL_CPP_PREFIX ${CMAKE_BINARY_DIR}/otel-cpp) + # No installed location supplied. Fetch it as an external project + include(ExternalProject) + set(OTEL_CPP_PROJECT_NAME opentelemetry-cpp) + set(OTEL_CPP_GIT_REPOSITORY "https://github.com/open-telemetry/opentelemetry-cpp.git") + set(OTEL_CPP_GIT_TAG "11d5d9e") + + if(DEFINED OTEL_CPP_PREFIX) + string(REPLACE "\\" "/" OTEL_CPP_PREFIX ${OTEL_CPP_PREFIX}) + else() + set(OTEL_CPP_PREFIX ${CMAKE_BINARY_DIR}/otel-cpp) + endif() + + if(WITH_OTLP_GRPC) + set(OTEL_CPP_CXX_STANDARD 14) # Abseil requires at least Cxx14 + else() + set(OTEL_CPP_CXX_STANDARD 11) + endif() + + if(NOT APPLE OR SKIP_OTEL_CPP_PATCH) + set(patch_command "") + else() + set(patch_command git apply ${CMAKE_SOURCE_DIR}/otel-cpp.patch) + endif() + + ExternalProject_Add( + ${OTEL_CPP_PROJECT_NAME} + GIT_REPOSITORY ${OTEL_CPP_GIT_REPOSITORY} + GIT_TAG ${OTEL_CPP_GIT_TAG} + PREFIX ${OTEL_CPP_PREFIX} + UPDATE_DISCONNECTED 1 + PATCH_COMMAND ${patch_command} + CMAKE_ARGS -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DWITH_OTLP_HTTP=${WITH_OTLP_HTTP} -DWITH_OTLP_GRPC=${WITH_OTLP_GRPC} -DBUILD_TESTING=OFF -DWITH_BENCHMARK=OFF -DOPENTELEMETRY_INSTALL=ON -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE} -DCMAKE_CXX_STANDARD=${OTEL_CPP_CXX_STANDARD} -DVCPKG_INSTALLED_DIR=${VCPKG_INSTALLED_DIR} + INSTALL_DIR ${OTEL_CPP_PREFIX} + INSTALL_COMMAND ${CMAKE_COMMAND} --install . --prefix ${OTEL_CPP_PREFIX} + ) endif() -if(WITH_OTLP_GRPC) - set(OTEL_CPP_CXX_STANDARD 14) # Abseil requires at least Cxx14 -else() - set(OTEL_CPP_CXX_STANDARD 11) -endif() - -if(NOT APPLE OR SKIP_OTEL_CPP_PATCH) - set(patch_command "") -else() - set(patch_command git apply ${CMAKE_SOURCE_DIR}/otel-cpp.patch) -endif() - -ExternalProject_Add( - ${OTEL_CPP_PROJECT_NAME} - GIT_REPOSITORY ${OTEL_CPP_GIT_REPOSITORY} - GIT_TAG ${OTEL_CPP_GIT_TAG} - PREFIX ${OTEL_CPP_PREFIX} - UPDATE_DISCONNECTED 1 - PATCH_COMMAND ${patch_command} - CMAKE_ARGS -DCMAKE_POSITION_INDEPENDENT_CODE=ON -DWITH_OTLP_HTTP=${WITH_OTLP_HTTP} -DWITH_OTLP_GRPC=${WITH_OTLP_GRPC} -DBUILD_TESTING=OFF -DWITH_BENCHMARK=OFF -DOPENTELEMETRY_INSTALL=ON -DCMAKE_TOOLCHAIN_FILE=${CMAKE_TOOLCHAIN_FILE} -DCMAKE_CXX_STANDARD=${OTEL_CPP_CXX_STANDARD} - INSTALL_DIR ${OTEL_CPP_PREFIX} - INSTALL_COMMAND ${CMAKE_COMMAND} --install . --prefix ${OTEL_CPP_PREFIX} -) - # ########################### # OpenTelemetry Proxy Library # ########################### diff --git a/README.md b/README.md index 63426f0..c61d746 100644 --- a/README.md +++ b/README.md @@ -21,22 +21,14 @@ Installation instructions Before proceeding, ensure that the below products are installed: * [MATLAB](https://www.mathworks.com/products/matlab.html) -1. Download [vcpkg](https://vcpkg.io). Install the following packages: -- curl -- nlohmann-json -- protobuf -- zlib - -2. Set VCPKG_ROOT environment variable to point to installed location of vcpkg. - -3. Download, Build and install OpenTelemetry MATLAB +1. Download, Build and install OpenTelemetry MATLAB ``` cd cmake -S . -B build -DCMAKE_INSTALL_PREFIX= cmake --build build --config Release --target install ``` -4. Download [OpenTelemetry Collector](https://github.com/open-telemetry/opentelemetry-collector-releases/releases). You can just obtain a pre-built binary for your platform. +2. Download [OpenTelemetry Collector](https://github.com/open-telemetry/opentelemetry-collector-releases/releases). You can just obtain a pre-built binary for your platform. ## Getting Started 1. Start OpenTelemetry Collector diff --git a/vcpkg.json b/vcpkg.json new file mode 100644 index 0000000..f7b0ee6 --- /dev/null +++ b/vcpkg.json @@ -0,0 +1,12 @@ +{ + "name": "opentelemetry-matlab", + "$schema": "https://raw.githubusercontent.com/microsoft/vcpkg-tool/main/docs/vcpkg.schema.json", + "dependencies": [ + "protobuf", + "zlib" + ], + "features": { + "otlp-http": {"description": "Otlp HTTP Exporter", "dependencies": ["curl", "nlohmann-json"]}, + "otlp-grpc": {"description": "Otlp gRPC Exporter", "dependencies": ["grpc", "abseil", "c-ares", "re2", "openssl", "upb"]} + } +} From 2516f6d3f14950f5b6232830099fe101e17ebd93 Mon Sep 17 00:00:00 2001 From: Duncan Po Date: Tue, 12 Sep 2023 16:44:37 -0400 Subject: [PATCH 09/11] add custom triplet to vcpkg on Mac --- CMakeLists.txt | 10 ++++++++++ vcpkg.json | 5 +++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6896b93..53307e7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,6 +25,16 @@ else() set(VCPKG_PREFIX ${CMAKE_BINARY_DIR}/vcpkg) endif() + # On Mac, there is a conflict between libcurl and the version in MATLAB, so + # use libcurl as a shared library and load the MATLAB version at runtime + if(APPLE) + # run uname -m to determine whether arm64 or x86_64 + exec_program(uname ARGS -m OUTPUT_VARIABLE MAC_HOST_SYSTEM) + set(VCPKG_OTEL_TRIPLET ${MAC_HOST_SYSTEM}-osx-otel-matlab) + set(VCPKG_OVERLAY_TRIPLETS ${CMAKE_SOURCE_DIR}/cmake/vcpkg_triplets) + set(VCPKG_TARGET_TRIPLET ${VCPKG_OTEL_TRIPLET}) + endif() + set(VCPKG_FETCH_CONTENT_NAME vcpkg) set(VCPKG_GIT_REPOSITORY "https://github.com/microsoft/vcpkg.git") set(VCPKG_GIT_TAG "9edb1b8") diff --git a/vcpkg.json b/vcpkg.json index f7b0ee6..6717cef 100644 --- a/vcpkg.json +++ b/vcpkg.json @@ -3,10 +3,11 @@ "$schema": "https://raw.githubusercontent.com/microsoft/vcpkg-tool/main/docs/vcpkg.schema.json", "dependencies": [ "protobuf", - "zlib" + "zlib", + "nlohmann-json" ], "features": { - "otlp-http": {"description": "Otlp HTTP Exporter", "dependencies": ["curl", "nlohmann-json"]}, + "otlp-http": {"description": "Otlp HTTP Exporter", "dependencies": ["curl"]}, "otlp-grpc": {"description": "Otlp gRPC Exporter", "dependencies": ["grpc", "abseil", "c-ares", "re2", "openssl", "upb"]} } } From d665f75fb40b5b40ae4031f7ce59f3644d108265 Mon Sep 17 00:00:00 2001 From: duncanpo Date: Tue, 12 Sep 2023 16:47:05 -0400 Subject: [PATCH 10/11] add custom triplets to vcpkg on Mac --- cmake/vcpkg_triplets/arm64-osx-otel-matlab.cmake | 10 ++++++++++ cmake/vcpkg_triplets/x86_64-osx-otel-matlab.cmake | 10 ++++++++++ 2 files changed, 20 insertions(+) create mode 100644 cmake/vcpkg_triplets/arm64-osx-otel-matlab.cmake create mode 100644 cmake/vcpkg_triplets/x86_64-osx-otel-matlab.cmake diff --git a/cmake/vcpkg_triplets/arm64-osx-otel-matlab.cmake b/cmake/vcpkg_triplets/arm64-osx-otel-matlab.cmake new file mode 100644 index 0000000..4e8c1d0 --- /dev/null +++ b/cmake/vcpkg_triplets/arm64-osx-otel-matlab.cmake @@ -0,0 +1,10 @@ +set(VCPKG_TARGET_ARCHITECTURE arm64) +set(VCPKG_CRT_LINKAGE dynamic) +if(${PORT} MATCHES "(curl|zlib)") + set(VCPKG_LIBRARY_LINKAGE dynamic) +else() + set(VCPKG_LIBRARY_LINKAGE static) +endif() + +set(VCPKG_CMAKE_SYSTEM_NAME Darwin) +set(VCPKG_OSX_ARCHITECTURES arm64) diff --git a/cmake/vcpkg_triplets/x86_64-osx-otel-matlab.cmake b/cmake/vcpkg_triplets/x86_64-osx-otel-matlab.cmake new file mode 100644 index 0000000..e33a495 --- /dev/null +++ b/cmake/vcpkg_triplets/x86_64-osx-otel-matlab.cmake @@ -0,0 +1,10 @@ +set(VCPKG_TARGET_ARCHITECTURE x64) +set(VCPKG_CRT_LINKAGE dynamic) +if(${PORT} MATCHES "(curl|zlib)") + set(VCPKG_LIBRARY_LINKAGE dynamic) +else() + set(VCPKG_LIBRARY_LINKAGE static) +endif() + +set(VCPKG_CMAKE_SYSTEM_NAME Darwin) +set(VCPKG_OSX_ARCHITECTURES x86_64) From f54327ca0a5601a70b3756bd4312de85d1a6ec96 Mon Sep 17 00:00:00 2001 From: duncanpo Date: Wed, 13 Sep 2023 10:56:42 -0400 Subject: [PATCH 11/11] automatically get windows_kill on Windows --- test/commonSetupOnce.m | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/test/commonSetupOnce.m b/test/commonSetupOnce.m index 1885696..f70a2a5 100644 --- a/test/commonSetupOnce.m +++ b/test/commonSetupOnce.m @@ -20,9 +20,20 @@ function commonSetupOnce(testCase) testCase.ListPid = @(name)"tasklist /fi ""IMAGENAME eq " + name + ".exe"""; testCase.ReadPidList = @(file)readtable(file, "VariableNamingRule", "preserve", "NumHeaderLines", 3, "MultipleDelimsAsOne", true, "Delimiter", " "); testCase.ExtractPid = @(table)table.Var2; - windows_killroot = string(getenv("WINDOWS_KILL_INSTALL")); - assert(~isempty(windows_killroot), "WINDOWS_KILL_INSTALL environment must be defined.") - testCase.Sigint = @(id)fullfile(windows_killroot,"windows-kill") + " -SIGINT " + id; + windows_killroot = getenv("WINDOWS_KILL_INSTALL"); + windows_killname = "windows-kill"; + if isempty(windows_killroot) + % windows_kill not pre-installed + windows_kill_url = "https://github.com/ElyDotDev/windows-kill/releases/download/1.1.4"; + windows_kill_zipfilename = "windows-kill_x64_1.1.4_lib_release"; + windows_killroot = fullfile(tempdir, windows_kill_zipfilename); + + % look for it in tempdir, download and install if it doesn't exist + if ~exist(fullfile(windows_killroot, windows_killname + ".exe"),"file") + unzip(fullfile(windows_kill_url, windows_kill_zipfilename + ".zip"), tempdir); + end + end + testCase.Sigint = @(id)fullfile(windows_killroot,windows_killname) + " -SIGINT " + id; testCase.Sigterm = @(id)"taskkill /F /pid " + id; elseif isunix && ~ismac testCase.ListPid = @(name)"ps -C " + name;