-
Notifications
You must be signed in to change notification settings - Fork 135
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RUM-5555 Benchmarks: Collect Memory Metric #1993
Merged
+522
−177
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5003898
RUM-5555 Collect memory metric
maxep 5f2ced6
Update README.md
maxep 121b2fd
Add docs
maxep 8cfc521
Update README.md
maxep 5a2ee6f
Remove exporter base class
maxep 92a2b0a
Use enum for scenarios
maxep 71769c4
Apply CR review suggestions
maxep File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
/* | ||
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
* This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
* Copyright 2019-Present Datadog, Inc. | ||
*/ | ||
|
||
#if OTEL_API | ||
#error("Benchmarks depends on opentelemetry-swift. Please open the project with 'make benchmark-tests-open'.") | ||
#endif | ||
|
||
import Foundation | ||
import OpenTelemetryApi | ||
import OpenTelemetrySdk | ||
|
||
let instrumentationName = "benchmarks" | ||
let instrumentationVersion = "1.0.0" | ||
|
||
/// Benchmark entrypoint to configure opentelemetry with metrics meters | ||
/// and tracer. | ||
public enum Benchmarks { | ||
/// Configuration of the Benchmarks library. | ||
public struct Configuration { | ||
/// Context of Benchmarks measures. | ||
/// The context properties will be added to metrics as tags. | ||
public struct Context { | ||
var applicationIdentifier: String | ||
var applicationName: String | ||
var applicationVersion: String | ||
var sdkVersion: String | ||
var deviceModel: String | ||
var osName: String | ||
var osVersion: String | ||
var run: String | ||
var scenario: String | ||
var branch: String | ||
|
||
public init( | ||
applicationIdentifier: String, | ||
applicationName: String, | ||
applicationVersion: String, | ||
sdkVersion: String, | ||
deviceModel: String, | ||
osName: String, | ||
osVersion: String, | ||
run: String, | ||
scenario: String, | ||
branch: String | ||
) { | ||
self.applicationIdentifier = applicationIdentifier | ||
self.applicationName = applicationName | ||
self.applicationVersion = applicationVersion | ||
self.sdkVersion = sdkVersion | ||
self.deviceModel = deviceModel | ||
self.osName = osName | ||
self.osVersion = osVersion | ||
self.run = run | ||
self.scenario = scenario | ||
self.branch = branch | ||
} | ||
} | ||
|
||
var clientToken: String | ||
var apiKey: String | ||
var context: Context | ||
|
||
public init( | ||
clientToken: String, | ||
apiKey: String, | ||
context: Context | ||
) { | ||
self.clientToken = clientToken | ||
self.apiKey = apiKey | ||
self.context = context | ||
} | ||
} | ||
|
||
/// Configure OpenTelemetry metrics meter and start measuring Memory. | ||
/// | ||
/// - Parameter configuration: The Benchmark configuration. | ||
public static func metrics(with configuration: Configuration) { | ||
let loggerProvider = LoggerProviderBuilder() | ||
.build() | ||
|
||
let metricExporter = MetricExporter( | ||
configuration: MetricExporter.Configuration( | ||
apiKey: configuration.apiKey, | ||
version: instrumentationVersion | ||
) | ||
) | ||
|
||
let meterProvider = MeterProviderBuilder() | ||
.with(pushInterval: 10) | ||
.with(processor: MetricProcessorSdk()) | ||
.with(exporter: metricExporter) | ||
.with(resource: Resource()) | ||
.build() | ||
|
||
let logger = loggerProvider | ||
.loggerBuilder(instrumentationScopeName: instrumentationName) | ||
.build() | ||
|
||
let meter = meterProvider.get( | ||
instrumentationName: instrumentationName, | ||
instrumentationVersion: instrumentationVersion | ||
) | ||
|
||
let labels = [ | ||
"device_model": configuration.context.deviceModel, | ||
"os": configuration.context.osName, | ||
"os_version": configuration.context.osVersion, | ||
"run": configuration.context.run, | ||
"scenario": configuration.context.scenario, | ||
"application_id": configuration.context.applicationIdentifier, | ||
"sdk_version": configuration.context.sdkVersion, | ||
"branch": configuration.context.branch, | ||
] | ||
|
||
_ = meter.createDoubleObservableGauge(name: "ios.benchmark.memory") { metric in | ||
do { | ||
let mem = try Memory.footprint() | ||
metric.observe(value: mem, labels: labels) | ||
} catch { | ||
logger.logRecordBuilder() | ||
.setSeverity(.error) | ||
.setAttributes(labels.mapValues { .string($0) }) | ||
.setBody("Failed to read Memory Metric: \(error)") | ||
.emit() | ||
} | ||
} | ||
|
||
OpenTelemetry.registerMeterProvider(meterProvider: meterProvider) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question/ How does it work? We create Gauge metric ✅ but when / how frequently will this closure be called?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is based on Asynchronous Gauge from otel specs where Callback functions will be called only when the Meter is being observed.
The meter is observed when there is a push, in our case it will be every 10s
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it, thanks!