Skip to content

Commit

Permalink
Merge pull request #87 from mathworks/metrics_async
Browse files Browse the repository at this point in the history
Add service name as a resource in examples
  • Loading branch information
duncanpo authored Feb 5, 2024
2 parents 323716b + 818b2d3 commit 1d601b7
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 22 deletions.
6 changes: 4 additions & 2 deletions examples/context_propagation/cpp/client.cc
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.

#include "opentelemetry/ext/http/client/http_client_factory.h"
#include "opentelemetry/ext/http/common/url_parser.h"
Expand Down Expand Up @@ -34,8 +34,10 @@ void InitTracer()
opentelemetry::sdk::trace::SimpleSpanProcessorFactory::Create(std::move(exporter));
std::vector<std::unique_ptr<opentelemetry::sdk::trace::SpanProcessor>> processors;
processors.push_back(std::move(processor));
opentelemetry::sdk::common::AttributeMap servicename = { {"service.name", "OpenTelemetry-Matlab_examples"} };
auto resource = opentelemetry::sdk::resource::Resource::Create(servicename);
std::unique_ptr<opentelemetry::sdk::trace::TracerContext> context =
opentelemetry::sdk::trace::TracerContextFactory::Create(std::move(processors));
opentelemetry::sdk::trace::TracerContextFactory::Create(std::move(processors), resource);
std::shared_ptr<opentelemetry::trace::TracerProvider> provider =
opentelemetry::sdk::trace::TracerProviderFactory::Create(std::move(context));
// Set the global trace provider
Expand Down
6 changes: 4 additions & 2 deletions examples/context_propagation/matlab/mymagic.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
% Web request handler function that takes context information (trace ID and span ID)
% from incoming request and set as current context
%
% Copyright 2023 The MathWorks, Inc.
% Copyright 2023-2024 The MathWorks, Inc.

% initialize tracing
runOnce(@initTracer);
Expand Down Expand Up @@ -40,7 +40,9 @@

function initTracer
% set up global TracerProvider
tp = opentelemetry.sdk.trace.TracerProvider();
resource = dictionary("service.name", "OpenTelemetry-Matlab_examples");
tp = opentelemetry.sdk.trace.TracerProvider(...
opentelemetry.sdk.trace.SimpleSpanProcessor, Resource=resource);
setTracerProvider(tp);

% set up global propagator
Expand Down
3 changes: 2 additions & 1 deletion examples/metrics/async_metrics_example.m
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
exp = opentelemetry.exporters.otlp.defaultMetricExporter();
reader = opentelemetry.sdk.metrics.PeriodicExportingMetricReader(exp, ...
"Interval", seconds(5), "Timeout", seconds(2.5)); % exports every 5 seconds
mp = opentelemetry.sdk.metrics.MeterProvider(reader);
resource = dictionary("service.name", "OpenTelemetry-Matlab_examples");
mp = opentelemetry.sdk.metrics.MeterProvider(reader, Resource=resource);
setMeterProvider(mp);
end

Expand Down
24 changes: 13 additions & 11 deletions examples/metrics/metrics_example.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
% updowncounter, and a histogram. It then enters a loop and updates the
% value of the instruments at each iteration.

% Copyright 2023 The MathWorks, Inc.
% Copyright 2023-2024 The MathWorks, Inc.

% initialize meter provider during first run
runOnce(@initMetrics);
% initialize meter provider
initMetrics;

% create meter and instruments
m = opentelemetry.metrics.getMeter("metrics_example");
Expand All @@ -21,6 +21,9 @@
h.record(50 + 15*randn); % normal distribution with mean 50 and std 15
pause(5);
end

% clean up
cleanupMetrics;
end


Expand All @@ -31,15 +34,14 @@
"Interval", seconds(5), "Timeout", seconds(2.5)); % exports every 5 seconds
% Use custom histogram bins
v = opentelemetry.sdk.metrics.View(InstrumentType="histogram", HistogramBinEdges=0:10:100);
mp = opentelemetry.sdk.metrics.MeterProvider(reader, View=v);
resource = dictionary("service.name", "OpenTelemetry-Matlab_examples");
mp = opentelemetry.sdk.metrics.MeterProvider(reader, View=v, ...
Resource=resource);
setMeterProvider(mp);
end

% This helper ensures the input function is only run once
function runOnce(fh)
persistent hasrun
if isempty(hasrun)
feval(fh);
hasrun = 1;
end
function cleanupMetrics
mp = opentelemetry.metrics.Provider.getMeterProvider();
opentelemetry.sdk.common.Cleanup.shutdown(mp); % shutdown
end

6 changes: 4 additions & 2 deletions examples/trace/trace_example.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
% This example creates spans to form a trace to instrument some simple
% MATLAB code that fits a line through a cluster of data points.

% Copyright 2023 The MathWorks, Inc.
% Copyright 2023-2024 The MathWorks, Inc.

% initialize tracing during first run
runOnce(@initTracer);
Expand Down Expand Up @@ -41,7 +41,9 @@

function initTracer
% set up global TracerProvider
tp = opentelemetry.sdk.trace.TracerProvider();
resource = dictionary("service.name", "OpenTelemetry-Matlab_examples");
tp = opentelemetry.sdk.trace.TracerProvider(...
opentelemetry.sdk.trace.SimpleSpanProcessor, Resource=resource);
setTracerProvider(tp);
end

Expand Down
7 changes: 5 additions & 2 deletions examples/webread/cpp/server.cc
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.

#include "server.h"
#include "opentelemetry/trace/context.h"
Expand All @@ -24,6 +24,7 @@ namespace

using namespace opentelemetry::trace;
namespace context = opentelemetry::context;
namespace common = opentelemetry::common;

uint16_t server_port = 8800;
constexpr const char *server_name = "localhost";
Expand All @@ -35,8 +36,10 @@ void InitTracer()
opentelemetry::sdk::trace::SimpleSpanProcessorFactory::Create(std::move(exporter));
std::vector<std::unique_ptr<opentelemetry::sdk::trace::SpanProcessor>> processors;
processors.push_back(std::move(processor));
opentelemetry::sdk::common::AttributeMap servicename = { {"service.name", "OpenTelemetry-Matlab_examples"} };
auto resource = opentelemetry::sdk::resource::Resource::Create(servicename);
std::unique_ptr<opentelemetry::sdk::trace::TracerContext> context =
opentelemetry::sdk::trace::TracerContextFactory::Create(std::move(processors));
opentelemetry::sdk::trace::TracerContextFactory::Create(std::move(processors), resource);
std::shared_ptr<opentelemetry::trace::TracerProvider> provider =
opentelemetry::sdk::trace::TracerProviderFactory::Create(std::move(context));
// Set the global trace provider
Expand Down
6 changes: 4 additions & 2 deletions examples/webread/matlab/webread_example.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
% This example shows how to propagate context (span ID and trace ID)
% to another process when using webread.

% Copyright 2023 The MathWorks, Inc.
% Copyright 2023-2024 The MathWorks, Inc.

% initialize tracing
runOnce(@initTracer);
Expand All @@ -26,7 +26,9 @@

function initTracer
% set up global TracerProvider
tp = opentelemetry.sdk.trace.TracerProvider();
resource = dictionary("service.name", "OpenTelemetry-Matlab_examples");
tp = opentelemetry.sdk.trace.TracerProvider(...
opentelemetry.sdk.trace.SimpleSpanProcessor, Resource=resource);
setTracerProvider(tp);

% set up global propagator
Expand Down

0 comments on commit 1d601b7

Please sign in to comment.