Skip to content

Commit

Permalink
Updating view, changing name and histogram edges is working
Browse files Browse the repository at this point in the history
  • Loading branch information
dnarula-mw committed Dec 6, 2023
1 parent 61b93aa commit 74f9d38
Show file tree
Hide file tree
Showing 8 changed files with 308 additions and 135 deletions.
55 changes: 37 additions & 18 deletions sdk/metrics/+opentelemetry/+sdk/+metrics/View.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,47 @@
end

methods
function obj = View(name, description, unit, instrumentName, instrumentType, ...
meterName, meterVersion, meterSchemaURL, attributeKeys, ...
aggregation, histogramBinEdges, varargin)
function obj = View(options)
arguments
options.name=""
options.description=""
options.unit=""
options.instrumentName=""
options.instrumentType=""
options.meterName=""
options.meterVersion=""
options.meterSchemaURL=""
options.attributeKeys=[]
options.aggregation=""
options.histogramBinEdges=[]
end

instrumentTypeCategory = int32(find(instrumentType==["kCounter", "kHistogram", "kUpDownCounter", "kObservableCounter", "kObservableGauge", "kObservableUpDownCounter"])-1);
options.attributeKeys = ["" options.attributeKeys];
instrumentTypeCategory = int32(find(options.instrumentType==["kCounter", "kHistogram", "kUpDownCounter", "kObservableCounter", "kObservableGauge", "kObservableUpDownCounter"])-1);

aggregationCategory = int32(find(aggregation==["kDrop", "kHistogram", "kLastValue", "kSum", "kDefault"])-1);
aggregationCategory = int32(find(options.aggregation==["kDrop", "kHistogram", "kLastValue", "kSum", "kDefault"])-1);

if(numel(instrumentTypeCategory)==0)
instrumentTypeCategory = -1;
end
if(numel(aggregationCategory)==0)
aggregationCategory = -1;
end
obj.Proxy = libmexclass.proxy.Proxy("Name", "libmexclass.opentelemetry.sdk.ViewProxy", ...
"ConstructorArguments", {name, description, unit, instrumentName, ...
instrumentTypeCategory, meterName, meterVersion, meterSchemaURL, ...
attributeKeys, aggregationCategory, histogramBinEdges, varargin});
obj.Description = description;
obj.Unit = unit;
obj.InstrumentName = instrumentName;
obj.InstrumentType = instrumentType;
obj.MeterName = meterName;
obj.MeterVersion = meterVersion;
obj.MeterSchemaURL = meterSchemaURL;
obj.AttributeKeys = attributeKeys;
obj.Aggregation = aggregation;
obj.HistogramBinEdges = histogramBinEdges;
"ConstructorArguments", {options.name, options.description, options.unit, options.instrumentName, ...
instrumentTypeCategory, options.meterName, options.meterVersion, options.meterSchemaURL, ...
options.attributeKeys, aggregationCategory, options.histogramBinEdges});
obj.Name = options.name;
obj.Description = options.description;
obj.Unit = options.unit;
obj.InstrumentName = options.instrumentName;
obj.InstrumentType = options.instrumentType;
obj.MeterName = options.meterName;
obj.MeterVersion = options.meterVersion;
obj.MeterSchemaURL = options.meterSchemaURL;
obj.AttributeKeys = options.attributeKeys;
obj.Aggregation = options.aggregation;
obj.HistogramBinEdges = options.histogramBinEdges;
end
end
end
43 changes: 43 additions & 0 deletions sdk/metrics/src/MeterProviderProxy.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
// Copyright 2023 The MathWorks, Inc.

#include "opentelemetry/sdk/metrics/instruments.h"
#include "opentelemetry-matlab/sdk/metrics/MeterProviderProxy.h"
#include "opentelemetry-matlab/sdk/metrics/PeriodicExportingMetricReaderProxy.h"
#include "opentelemetry-matlab/sdk/metrics/ViewProxy.h"

#include "libmexclass/proxy/ProxyManager.h"

#include <chrono>
#include <string.h>

namespace libmexclass::opentelemetry::sdk {
libmexclass::proxy::MakeResult MeterProviderProxy::make(const libmexclass::proxy::FunctionArguments& constructor_arguments) {
Expand Down Expand Up @@ -62,6 +64,47 @@ void MeterProviderProxy::addMetricReader(libmexclass::proxy::method::Context& co
void MeterProviderProxy::addView(libmexclass::proxy::method::Context& context) {
matlab::data::TypedArray<uint64_t> viewid_mda = context.inputs[0];
libmexclass::proxy::ID viewid = viewid_mda[0];

// auto i = std::static_pointer_cast<ViewProxy>(libmexclass::proxy::ProxyManager::getProxy(viewid))->getInstrumentSelector(context);
// if((int)(i->GetInstrumentType())!=0){
// exit(0);
// }
// auto instrument_name = "mycounter";
// auto instrument_name_view = nostd::string_view(instrument_name);
// if(!i->GetNameFilter()->Match(instrument_name_view)){
// exit(0);
// }
// auto instrument_unit = "unit";
// auto instrument_unit_view = nostd::string_view(instrument_unit);
// if(!i->GetUnitFilter()->Match(instrument_unit_view)){
// exit(0);
// }
// auto m = std::static_pointer_cast<ViewProxy>(libmexclass::proxy::ProxyManager::getProxy(viewid))->getMeterSelector(context);
// auto meter_name = "mymeter";
// auto meter_name_view = nostd::string_view(meter_name);
// if(!m->GetNameFilter()->Match(meter_name_view)){
// exit(0);
// }
// auto meter_version = "1.2.0";
// auto meter_version_view = nostd::string_view(meter_version);
// if(!m->GetVersionFilter()->Match(meter_version_view)){
// exit(0);
// }
// auto meter_schema = "";
// auto meter_schema_view = nostd::string_view(meter_schema);
// if(!m->GetSchemaFilter()->Match(meter_schema_view)){
// exit(0);
// }
// auto v = std::static_pointer_cast<ViewProxy>(libmexclass::proxy::ProxyManager::getProxy(viewid))->getView(context);
// if(v->GetName().compare("View")!=0){
// exit(0);
// }
// if(v->GetDescription().compare("description")!=0){
// exit(0);
// }
// if((int)(v->GetAggregationType())!=0){
// exit(0);
// }

static_cast<metrics_sdk::MeterProvider&>(*CppMeterProvider).AddView(
std::static_pointer_cast<ViewProxy>(libmexclass::proxy::ProxyManager::getProxy(viewid))->getInstrumentSelector(context),
Expand Down
63 changes: 37 additions & 26 deletions sdk/metrics/src/ViewProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ libmexclass::proxy::MakeResult ViewProxy::make(const libmexclass::proxy::Functio
auto unit = unit_mda[0];

matlab::data::TypedArray<int> aggregation_type_mda = constructor_arguments[9];
auto aggregation_type = static_cast<metrics_sdk::AggregationType>(static_cast<int>(aggregation_type_mda[0]));
metrics_sdk::AggregationType aggregation_type;
if(static_cast<int>(aggregation_type_mda[0]) == -1){
aggregation_type = metrics_sdk::AggregationType::kDefault;
}else{
aggregation_type = static_cast<metrics_sdk::AggregationType>(static_cast<int>(aggregation_type_mda[0]));
}

std::shared_ptr<metrics_sdk::HistogramAggregationConfig> aggregation_config = std::shared_ptr<metrics_sdk::HistogramAggregationConfig>(new metrics_sdk::HistogramAggregationConfig());
if(aggregation_type == metrics_sdk::AggregationType::kHistogram){
Expand All @@ -45,58 +50,64 @@ libmexclass::proxy::MakeResult ViewProxy::make(const libmexclass::proxy::Functio
}

std::unique_ptr<metrics_sdk::AttributesProcessor> attributes_processor;
matlab::data::StringArray attributes_mda = constructor_arguments[8];
if(attributes_mda.getNumberOfElements()==0){
matlab::data::TypedArray<matlab::data::MATLABString> attributes_mda = constructor_arguments[8];
if(attributes_mda.getNumberOfElements()==1){
attributes_processor = std::unique_ptr<metrics_sdk::AttributesProcessor>(new metrics_sdk::DefaultAttributesProcessor());
}else{
std::unordered_map<std::string, bool> allowed_attribute_keys;
for (auto a : attributes_mda) {
allowed_attribute_keys[a] = true;
for (int a=1; a<attributes_mda.getNumberOfElements(); a++) {
allowed_attribute_keys[attributes_mda[a]] = true;
}
attributes_processor = std::unique_ptr<metrics_sdk::AttributesProcessor>(new metrics_sdk::FilteringAttributesProcessor(allowed_attribute_keys));
}

auto view = metrics_sdk::ViewFactory::Create(name, description,
unit, aggregation_type, std::move(aggregation_config), std::move(attributes_processor));
unit, aggregation_type, aggregation_config, std::move(attributes_processor));

// Create Instrument Selector
matlab::data::TypedArray<int> instrument_type_mda = constructor_arguments[4];
auto instrument_type = static_cast<metrics_sdk::InstrumentType>(static_cast<int>(instrument_type_mda[0]));
metrics_sdk::InstrumentType instrument_type;
if(static_cast<int>(instrument_type_mda[0]) == -1){
instrument_type = metrics_sdk::InstrumentType::kCounter;
}else{
instrument_type = static_cast<metrics_sdk::InstrumentType>(static_cast<int>(instrument_type_mda[0]));
}

matlab::data::StringArray instrument_name_mda = constructor_arguments[3];
auto instrument_name = static_cast<std::string>(instrument_name_mda[0]);
auto instrument_name_view = nostd::string_view(instrument_name);
auto instrument_name_view = static_cast<std::string>(instrument_name_mda[0]);
// auto instrument_name_view = nostd::string_view(instrument_name);

auto unit_view = nostd::string_view(static_cast<std::string>(unit));
auto unit_view = static_cast<std::string>(unit);
// auto unit_view = nostd::string_view(static_cast<std::string>(unit));

auto instrumentSelector = metrics_sdk::InstrumentSelectorFactory::Create(instrument_type,
instrument_name, unit_view);

instrument_name_view, unit_view);

// Create Meter Selector
matlab::data::StringArray meter_name_mda = constructor_arguments[5];
auto meter_name = static_cast<std::string>(meter_name_mda[0]);
auto meter_name_view = nostd::string_view(meter_name);
auto meter_name_view = static_cast<std::string>(meter_name_mda[0]);
// auto meter_name_view = nostd::string_view(meter_name);

matlab::data::StringArray meter_version_mda = constructor_arguments[6];
auto meter_version = static_cast<std::string>(meter_version_mda[0]);
auto meter_version_view = nostd::string_view(meter_version);
auto meter_version_view = static_cast<std::string>(meter_version_mda[0]);
// auto meter_version_view = nostd::string_view(meter_version);

matlab::data::StringArray meter_schema_mda = constructor_arguments[7];
auto meter_schema = static_cast<std::string>(meter_schema_mda[0]);
auto meter_schema_view = nostd::string_view(meter_schema);
auto meter_schema_view = static_cast<std::string>(meter_schema_mda[0]);
// auto meter_schema_view = nostd::string_view(meter_schema);

auto meterSelector = metrics_sdk::MeterSelectorFactory::Create(meter_name_view,
meter_version_view, meter_schema_view);



// out = std::make_shared<ViewProxy>(nostd::shared_ptr<metrics_sdk::View>(
// std::move(metrics_sdk::ViewFactory::Create(name, description,
// unit, aggregation_type, std::move(aggregation_config), std::move(attributes_processor)))));


// return out;
// std::unique_ptr<metrics_sdk::InstrumentSelector> instrumentSelector{
// new metrics_sdk::InstrumentSelector(instrument_type,
// instrument_name_view, unit_view)};
// std::unique_ptr<metrics_sdk::MeterSelector> meterSelector{
// new metrics_sdk::MeterSelector(meter_name_view,
// meter_version_view, meter_schema_view)};
// std::unique_ptr<metrics_sdk::View> view{
// new metrics_sdk::View{name, description,
// unit, aggregation_type, aggregation_config, std::move(attributes_processor)}};

return std::make_shared<ViewProxy>(std::move(view), std::move(instrumentSelector), std::move(meterSelector));
}
Expand Down
Loading

0 comments on commit 74f9d38

Please sign in to comment.