Skip to content

Commit

Permalink
Merge pull request #147 from mathworks/editable_view
Browse files Browse the repository at this point in the history
Make view properties editable, fixes #68
  • Loading branch information
duncanpo authored Aug 20, 2024
2 parents 24452df + b554d89 commit 4ed584d
Show file tree
Hide file tree
Showing 4 changed files with 328 additions and 136 deletions.
210 changes: 160 additions & 50 deletions sdk/metrics/+opentelemetry/+sdk/+metrics/View.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@
Proxy % Proxy object to interface C++ code
end

properties (SetAccess=immutable)
Name (1,1) string % View name
Description (1,1) string % Description of view
InstrumentName (1,1) string % Name of the instrument this view applies to
InstrumentType (1,1) string % Type of instrument this view applies to
InstrumentUnit (1,1) string % Unit of instrument this view applies to
MeterName (1,1) string % Name of the meter this view applies to
MeterVersion (1,1) string % Version of the meter this view applies to
MeterSchema (1,1) string % Schema URL of the meter this view applies to
AllowedAttributes (1,:) string % List of attribute keys that are kept. All other attributes are ignored.
Aggregation (1,1) string % Customized aggregation type
HistogramBinEdges (1,:) double % Vector of customized bin edges for histogram
properties
Name (1,1) string = "" % View name
Description (1,1) string = "" % Description of view
InstrumentName (1,1) string % Name of the instrument this view applies to
InstrumentType (1,1) string % Type of instrument this view applies to
InstrumentUnit (1,1) string = "" % Unit of instrument this view applies to
MeterName (1,1) string = "" % Name of the meter this view applies to
MeterVersion (1,1) string = "" % Version of the meter this view applies to
MeterSchema (1,1) string = "" % Schema URL of the meter this view applies to
AllowedAttributes (1,:) string % List of attribute keys that are kept. All other attributes are ignored.
Aggregation (1,1) string % Customized aggregation type
HistogramBinEdges (1,:) double = zeros(1,0) % Vector of customized bin edges for histogram
end

methods
Expand All @@ -33,7 +33,7 @@
% V = OPENTELEMETRY.SDK.METRICS.VIEW(PARAM1, VALUE1, PARAM2,
% VALUE2, ...) creates a view object and specifies its
% behavior using parameter name/value pairs. Parameters are:
% "Name" - Name of view. Any metric this view
% "Name" - Name of view. Any metric this view
% applies to will be renamed to this name.
% "Description" - Description of view.
% "InstrumentName" - Specifies an instrument name. This
Expand All @@ -42,7 +42,7 @@
% this name.
% "InstrumentType" - Specifies an instrument type. This
% view will be applied to all metrics
% generated from all instruments of
% generated from all instruments of
% this type.
% "InstrumentUnit" - Specifies an instrument unit. This
% view will be applied to all metrics
Expand All @@ -61,10 +61,10 @@
% generated from all instruments created
% by meters with this schema URL.
% "AllowedAttributes" - Specifies a list of attributes
% that will be kept. All other
% that will be kept. All other
% attributes will be dropped.
% "Aggregation" - Change instruments to use a
% different aggregation beahvior.
% different aggregation beahvior.
% "HistogramBinEdges" - Use a different set of bins
% in all histograms this view
% applies to
Expand All @@ -83,50 +83,160 @@
%
% See also OPENTELEMETRY.SDK.METRICS.METERPROVIDER
arguments
options.Name {mustBeTextScalar} = ""
options.Description {mustBeTextScalar} = ""
options.Name {mustBeTextScalar}
options.Description {mustBeTextScalar}
options.InstrumentName {mustBeTextScalar} = "*"
options.InstrumentType {mustBeTextScalar} = "counter"
options.InstrumentUnit {mustBeTextScalar} = ""
options.MeterName {mustBeTextScalar} = ""
options.MeterVersion {mustBeTextScalar} = ""
options.MeterSchema {mustBeTextScalar} = ""
options.AllowedAttributes {mustBeText, mustBeVector} % no default here
options.InstrumentUnit {mustBeTextScalar}
options.MeterName {mustBeTextScalar}
options.MeterVersion {mustBeTextScalar}
options.MeterSchema {mustBeTextScalar}
options.AllowedAttributes {mustBeText, mustBeVector} = "*"
options.Aggregation {mustBeTextScalar} = "default"
options.HistogramBinEdges {mustBeNumeric, mustBeVector} = zeros(1,0)
options.HistogramBinEdges {mustBeNumeric, mustBeVector}
end

obj.Proxy = libmexclass.proxy.Proxy("Name", "libmexclass.opentelemetry.sdk.ViewProxy", ...
"ConstructorArguments", {});

if isfield(options, "Name")
obj.Name = options.Name;
end
if isfield(options, "Description")
obj.Description = options.Description;
end
obj.InstrumentName = options.InstrumentName;
obj.InstrumentType = options.InstrumentType;
if isfield(options, "InstrumentUnit")
obj.InstrumentUnit = options.InstrumentUnit;
end
if isfield(options, "MeterName")
obj.MeterName = options.MeterName;
end
if isfield(options, "MeterVersion")
obj.MeterVersion = options.MeterVersion;
end
if isfield(options, "MeterSchema")
obj.MeterSchema = options.MeterSchema;
end
obj.AllowedAttributes = options.AllowedAttributes;
obj.Aggregation = options.Aggregation;
if isfield(options, "HistogramBinEdges")
obj.HistogramBinEdges = options.HistogramBinEdges;
end
end

function obj = set.Name(obj, name)
arguments
obj
name {mustBeTextScalar}
end
name = string(name);
obj.Proxy.setName(name); %#ok<*MCSUP>
obj.Name = name;
end

function obj = set.Description(obj, desc)
arguments
obj
desc {mustBeTextScalar}
end
desc = string(desc);
obj.Proxy.setDescription(desc);
obj.Description = desc;
end

function obj = set.InstrumentName(obj, instname)
arguments
obj
instname {mustBeTextScalar}
end
instname = string(instname);
obj.Proxy.setInstrumentName(instname);
obj.InstrumentName = instname;
end

function obj = set.InstrumentType(obj, insttype)
arguments
obj
insttype {mustBeTextScalar}
end
instrument_types = ["counter", "histogram", "updowncounter", ...
"observablecounter", "observableupdowncounter", "observablegauge"];
instrument_type = validatestring(options.InstrumentType, instrument_types);
insttype = validatestring(insttype, instrument_types);
obj.Proxy.setInstrumentType(insttype);
obj.InstrumentType = insttype;
end

aggregation_types = ["drop", "histogram", "lastvalue", "sum", "default"];
aggregation_type = validatestring(options.Aggregation, aggregation_types);

% check whether AllowedAttributes is defined
filter_attributes = isfield(options, "AllowedAttributes");
if ~filter_attributes
% put some defaults here, which will be ignored since filter_attributes is false
options.AllowedAttributes = strings(1,0);
function obj = set.InstrumentUnit(obj, instunit)
arguments
obj
instunit {mustBeTextScalar}
end
instunit = string(instunit);
obj.Proxy.setInstrumentUnit(instunit);
obj.InstrumentUnit = instunit;
end

obj.Proxy = libmexclass.proxy.Proxy("Name", "libmexclass.opentelemetry.sdk.ViewProxy", ...
"ConstructorArguments", {options.Name, options.Description, options.InstrumentName, ...
instrument_type, options.InstrumentUnit, options.MeterName, ...
options.MeterVersion, options.MeterSchema, filter_attributes,...
options.AllowedAttributes, aggregation_type, options.HistogramBinEdges});

obj.Name = string(options.Name);
obj.Description = string(options.Description);
obj.InstrumentName = string(options.InstrumentName);
obj.InstrumentType = instrument_type;
obj.InstrumentUnit = string(options.InstrumentUnit);
obj.MeterName = string(options.MeterName);
obj.MeterVersion = string(options.MeterVersion);
obj.MeterSchema = string(options.MeterSchema);
obj.AllowedAttributes = reshape(string(options.AllowedAttributes),1,[]);
obj.Aggregation = aggregation_type;
obj.HistogramBinEdges = reshape(double(options.HistogramBinEdges),1,[]);
function obj = set.MeterName(obj, metername)
arguments
obj
metername {mustBeTextScalar}
end
metername = string(metername);
obj.Proxy.setMeterName(metername)
obj.MeterName = metername;
end

function obj = set.MeterVersion(obj, meterversion)
arguments
obj
meterversion {mustBeTextScalar}
end
meterversion = string(meterversion);
obj.Proxy.setMeterVersion(meterversion);
obj.MeterVersion = meterversion;
end

function obj = set.MeterSchema(obj, meterschema)
arguments
obj
meterschema {mustBeTextScalar}
end
meterschema = string(meterschema);
obj.Proxy.setMeterSchema(meterschema);
obj.MeterSchema = meterschema;
end

function obj = set.AllowedAttributes(obj, attrs)
arguments
obj
attrs {mustBeText, mustBeVector}
end
attrs = reshape(string(attrs),1,[]);
obj.Proxy.setAllowedAttributes(attrs);
obj.AllowedAttributes = attrs;
end

function obj = set.Aggregation(obj, agg)
arguments
obj
agg {mustBeTextScalar}
end
aggregation_types = ["drop", "histogram", "lastvalue", "sum", "default"];
agg = validatestring(agg, aggregation_types);
obj.Proxy.setAggregation(agg);
obj.Aggregation = agg;
end

function obj = set.HistogramBinEdges(obj, binedges)
arguments
obj
binedges {mustBeNumeric, mustBeVector}
end
binedges = reshape(double(binedges),1,[]);
obj.Proxy.setHistogramBinEdges(binedges);
obj.HistogramBinEdges = binedges;
end
end
end
50 changes: 38 additions & 12 deletions sdk/metrics/include/opentelemetry-matlab/sdk/metrics/ViewProxy.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2023 The MathWorks, Inc.
// Copyright 2023-2024 The MathWorks, Inc.

#pragma once

Expand Down Expand Up @@ -28,27 +28,53 @@ namespace nostd = opentelemetry::nostd;
namespace libmexclass::opentelemetry::sdk {
class ViewProxy : public libmexclass::proxy::Proxy {
public:
ViewProxy(std::string name, std::string description, std::string instrumentName,
metrics_sdk::InstrumentType instrumentType, std::string instrumentUnit, std::string meterName,
std::string meterVersion, std::string meterSchema, std::unordered_map<std::string, bool> allowedAttributes,
bool filterAttributes, metrics_sdk::AggregationType aggregationType, std::vector<double> histogramBinEdges)
: Name(std::move(name)), Description(std::move(description)), InstrumentName(std::move(instrumentName)), InstrumentType(instrumentType),
InstrumentUnit(std::move(instrumentUnit)), MeterName(std::move(meterName)), MeterVersion(std::move(meterVersion)), MeterSchema(std::move(meterSchema)),
AllowedAttributes(std::move(allowedAttributes)), FilterAttributes(filterAttributes), Aggregation(aggregationType), HistogramBinEdges(std::move(histogramBinEdges)) {}

ViewProxy()
: FilterAttributes(false) {
REGISTER_METHOD(ViewProxy, setName);
REGISTER_METHOD(ViewProxy, setDescription);
REGISTER_METHOD(ViewProxy, setInstrumentName);
REGISTER_METHOD(ViewProxy, setInstrumentType);
REGISTER_METHOD(ViewProxy, setInstrumentUnit);
REGISTER_METHOD(ViewProxy, setMeterName);
REGISTER_METHOD(ViewProxy, setMeterVersion);
REGISTER_METHOD(ViewProxy, setMeterSchema);
REGISTER_METHOD(ViewProxy, setAllowedAttributes);
REGISTER_METHOD(ViewProxy, setAggregation);
REGISTER_METHOD(ViewProxy, setHistogramBinEdges);
}

static libmexclass::proxy::MakeResult make(const libmexclass::proxy::FunctionArguments& constructor_arguments);

void setName(libmexclass::proxy::method::Context& context);

void setDescription(libmexclass::proxy::method::Context& context);

void setInstrumentName(libmexclass::proxy::method::Context& context);

void setInstrumentType(libmexclass::proxy::method::Context& context);

void setInstrumentUnit(libmexclass::proxy::method::Context& context);

void setMeterName(libmexclass::proxy::method::Context& context);

void setMeterVersion(libmexclass::proxy::method::Context& context);

void setMeterSchema(libmexclass::proxy::method::Context& context);

void setAllowedAttributes(libmexclass::proxy::method::Context& context);

void setAggregation(libmexclass::proxy::method::Context& context);

void setHistogramBinEdges(libmexclass::proxy::method::Context& context);

std::unique_ptr<metrics_sdk::View> getView();

std::unique_ptr<metrics_sdk::InstrumentSelector> getInstrumentSelector();

std::unique_ptr<metrics_sdk::MeterSelector> getMeterSelector();

private:
std::unique_ptr<metrics_sdk::View> View;

std::unique_ptr<metrics_sdk::InstrumentSelector> InstrumentSelector;

std::string InstrumentName;
metrics_sdk::InstrumentType InstrumentType;
std::string InstrumentUnit;
Expand Down
Loading

0 comments on commit 4ed584d

Please sign in to comment.