|
| 1 | +// Copyright 2023 The MathWorks, Inc. |
| 2 | + |
| 3 | +#include "opentelemetry-matlab/sdk/metrics/ViewProxy.h" |
| 4 | + |
| 5 | +#include "libmexclass/proxy/ProxyManager.h" |
| 6 | + |
| 7 | +#include <chrono> |
| 8 | + |
| 9 | +namespace libmexclass::opentelemetry::sdk { |
| 10 | +ViewProxy::ViewProxy(std::unique_ptr<metrics_sdk::View> view, std::unique_ptr<metrics_sdk::InstrumentSelector> instrumentSelector, std::unique_ptr<metrics_sdk::MeterSelector> meterSelector){ |
| 11 | + View = std::move(view); |
| 12 | + InstrumentSelector = std::move(instrumentSelector); |
| 13 | + MeterSelector = std::move(meterSelector); |
| 14 | + REGISTER_METHOD(ViewProxy, getView); |
| 15 | + REGISTER_METHOD(ViewProxy, getInstrumentSelector); |
| 16 | + REGISTER_METHOD(ViewProxy, getMeterSelector); |
| 17 | +} |
| 18 | + |
| 19 | +libmexclass::proxy::MakeResult ViewProxy::make(const libmexclass::proxy::FunctionArguments& constructor_arguments) { |
| 20 | + libmexclass::proxy::MakeResult out; |
| 21 | + |
| 22 | + //Create View |
| 23 | + matlab::data::StringArray name_mda = constructor_arguments[0]; |
| 24 | + auto name = name_mda[0]; |
| 25 | + |
| 26 | + matlab::data::StringArray description_mda = constructor_arguments[1]; |
| 27 | + auto description = description_mda[0]; |
| 28 | + |
| 29 | + matlab::data::StringArray unit_mda = constructor_arguments[2]; |
| 30 | + auto unit = unit_mda[0]; |
| 31 | + |
| 32 | + matlab::data::TypedArray<int> aggregation_type_mda = constructor_arguments[9]; |
| 33 | + auto aggregation_type = static_cast<metrics_sdk::AggregationType>(static_cast<int>(aggregation_type_mda[0])); |
| 34 | + |
| 35 | + std::shared_ptr<metrics_sdk::HistogramAggregationConfig> aggregation_config = std::shared_ptr<metrics_sdk::HistogramAggregationConfig>(new metrics_sdk::HistogramAggregationConfig()); |
| 36 | + if(aggregation_type == metrics_sdk::AggregationType::kHistogram){ |
| 37 | + matlab::data::TypedArray<double> histogramBinEdges_mda = constructor_arguments[10]; |
| 38 | + std::vector<double> histogramBinEdges; |
| 39 | + for (auto h : histogramBinEdges_mda) { |
| 40 | + histogramBinEdges.push_back(h); |
| 41 | + } |
| 42 | + aggregation_config->boundaries_ = histogramBinEdges; |
| 43 | + } |
| 44 | + |
| 45 | + std::unique_ptr<metrics_sdk::AttributesProcessor> attributes_processor; |
| 46 | + matlab::data::StringArray attributes_mda = constructor_arguments[8]; |
| 47 | + if(attributes_mda.getNumberOfElements()==0){ |
| 48 | + attributes_processor = std::unique_ptr<metrics_sdk::AttributesProcessor>(new metrics_sdk::DefaultAttributesProcessor()); |
| 49 | + }else{ |
| 50 | + std::unordered_map<std::string, bool> allowed_attribute_keys; |
| 51 | + for (auto a : attributes_mda) { |
| 52 | + allowed_attribute_keys[a] = true; |
| 53 | + } |
| 54 | + attributes_processor = std::unique_ptr<metrics_sdk::AttributesProcessor>(new metrics_sdk::FilteringAttributesProcessor(allowed_attribute_keys)); |
| 55 | + } |
| 56 | + |
| 57 | + auto view = metrics_sdk::ViewFactory::Create(name, description, |
| 58 | + unit, aggregation_type, std::move(aggregation_config), std::move(attributes_processor)); |
| 59 | + |
| 60 | + // Create Instrument Selector |
| 61 | + matlab::data::TypedArray<int> instrument_type_mda = constructor_arguments[4]; |
| 62 | + auto instrument_type = static_cast<metrics_sdk::InstrumentType>(static_cast<int>(instrument_type_mda[0])); |
| 63 | + |
| 64 | + matlab::data::StringArray instrument_name_mda = constructor_arguments[3]; |
| 65 | + auto instrument_name = static_cast<std::string>(instrument_name_mda[0]); |
| 66 | + auto instrument_name_view = nostd::string_view(instrument_name); |
| 67 | + |
| 68 | + auto unit_view = nostd::string_view(static_cast<std::string>(unit)); |
| 69 | + |
| 70 | + auto instrumentSelector = metrics_sdk::InstrumentSelectorFactory::Create(instrument_type, |
| 71 | + instrument_name, unit_view); |
| 72 | + |
| 73 | + |
| 74 | + // Create Meter Selector |
| 75 | + matlab::data::StringArray meter_name_mda = constructor_arguments[5]; |
| 76 | + auto meter_name = static_cast<std::string>(meter_name_mda[0]); |
| 77 | + auto meter_name_view = nostd::string_view(meter_name); |
| 78 | + |
| 79 | + matlab::data::StringArray meter_version_mda = constructor_arguments[6]; |
| 80 | + auto meter_version = static_cast<std::string>(meter_version_mda[0]); |
| 81 | + auto meter_version_view = nostd::string_view(meter_version); |
| 82 | + |
| 83 | + matlab::data::StringArray meter_schema_mda = constructor_arguments[7]; |
| 84 | + auto meter_schema = static_cast<std::string>(meter_schema_mda[0]); |
| 85 | + auto meter_schema_view = nostd::string_view(meter_schema); |
| 86 | + |
| 87 | + auto meterSelector = metrics_sdk::MeterSelectorFactory::Create(meter_name_view, |
| 88 | + meter_version_view, meter_schema_view); |
| 89 | + |
| 90 | + |
| 91 | + |
| 92 | + // out = std::make_shared<ViewProxy>(nostd::shared_ptr<metrics_sdk::View>( |
| 93 | + // std::move(metrics_sdk::ViewFactory::Create(name, description, |
| 94 | + // unit, aggregation_type, std::move(aggregation_config), std::move(attributes_processor))))); |
| 95 | + |
| 96 | + |
| 97 | + // return out; |
| 98 | + |
| 99 | + return std::make_shared<ViewProxy>(std::move(view), std::move(instrumentSelector), std::move(meterSelector)); |
| 100 | +} |
| 101 | + |
| 102 | +std::unique_ptr<metrics_sdk::View> ViewProxy::getView(libmexclass::proxy::method::Context& context){ |
| 103 | + return std::move(View); |
| 104 | +} |
| 105 | + |
| 106 | +std::unique_ptr<metrics_sdk::InstrumentSelector> ViewProxy::getInstrumentSelector(libmexclass::proxy::method::Context& context){ |
| 107 | + return std::move(InstrumentSelector); |
| 108 | +} |
| 109 | + |
| 110 | +std::unique_ptr<metrics_sdk::MeterSelector> ViewProxy::getMeterSelector(libmexclass::proxy::method::Context& context){ |
| 111 | + return std::move(MeterSelector); |
| 112 | +} |
| 113 | + |
| 114 | +} |
0 commit comments