-
Notifications
You must be signed in to change notification settings - Fork 997
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
Restructure instrument creation code paths #3256
Merged
Merged
Conversation
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
MrAlias
added
pkg:SDK
Related to an SDK package
area:metrics
Part of OpenTelemetry Metrics
labels
Oct 3, 2022
Codecov Report
Additional details and impacted files@@ Coverage Diff @@
## main #3256 +/- ##
=======================================
- Coverage 77.7% 77.6% -0.1%
=======================================
Files 164 164
Lines 11651 11520 -131
=======================================
- Hits 9054 8950 -104
+ Misses 2388 2372 -16
+ Partials 209 198 -11
|
MrAlias
force-pushed
the
inst-prov-unify
branch
from
October 3, 2022 20:54
d64317d
to
c5918b6
Compare
MrAlias
force-pushed
the
inst-prov-unify
branch
from
October 3, 2022 20:55
c5918b6
to
d166139
Compare
MrAlias
changed the title
Restructure instrument providers
Restructure instrument creation code paths
Oct 3, 2022
MrAlias
requested review from
jmacd,
Aneurysm9,
evantorrie,
XSAM,
dashpole,
MadVikingGod,
pellared,
hanyuancheung and
dmathieu
as code owners
October 3, 2022 21:18
5 tasks
This doesn't close #3245, but it does block progress in adding caching to the instrument providers. |
MadVikingGod
approved these changes
Nov 4, 2022
Aneurysm9
approved these changes
Nov 10, 2022
Co-authored-by: Anthony Mirabella <a9@aneurysm9.com>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
area:metrics
Part of OpenTelemetry Metrics
pkg:SDK
Related to an SDK package
Skip Changelog
PRs that do not require a CHANGELOG.md entry
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.
instProvider
typeCurrently, instrument providers are defined for the async and sync instruments both of
int64
andfloat64
types. That produces four instrument providers in total. Each instrument provider has three methods to create three different instruments. Each of these methods does the following:view.Instrument
with the appropriateview.InstrumentKind
based on the instrument provider being used.For example,
opentelemetry-go/sdk/metric/instrument_provider.go
Lines 36 to 53 in 3675379
The only functionally unique part of these methods is in step 2 (step 5 technically involves a cast to an API type, but it ends up not being relevant).
These common functional elements can be abstracted into a common method. However, this can be taken further. Given each instrument provider would do this, we can define a common underlying type for all instrument providers that defines this method. E.g.
Doing so, allows each instrument provider to be reduced to something resembling this:
This both reduces a lot of redundant code and centralizes the functional section development needs to deal with going forward.
Store instrument providers in a meter
Currently, when a
meter
is asked for a new instrument provider it creates a new one every time:opentelemetry-go/sdk/metric/meter.go
Lines 92 to 117 in 3675379
This includes allocating a pointer for a new resolver. An allocation is unavoidable given the resolver contains a slice and even if passed as a value, the new reference to a slice would incur an allocation.
As an alternative to this approach, allocate once-per-provider and do so at the start.
Performance considerations
Using the included benchmark for the change we can see this indeed reduces the overall allocations for new instrument creation:
instProviderKey
typeThe
resolver
andinserter
both accept aview.Instrument
andunit.Unit
. Unify these types into a single newinstProviderKey
that represents the new instrument request received by an instrument provider.This enables easy caching for a future PR (see #3245)
Store the
instrumentation.Scope
in theinsterter
type instead of themeter
The
meter
type currently has a field that holds the associatedinstrumentation.Scope
of the meter.opentelemetry-go/sdk/metric/meter.go
Lines 33 to 35 in b5b6852
This field is only maintained to pass to an instrument provider as one of its fields, which in turn passes it to a
resolver
for each instrument creation, which then passes it to aninserter
. Instead of maintaining state in all these types, just have theinserter
define it as a field directly.