You've already forked opentelemetry-go
mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2025-06-23 00:07:52 +02:00
Add support for Resources in the SDK (#552)
* Add support for Resources in the SDK Add `Config` types for the push `Controller` and the `SDK`. Included with this are helper functions to configure the `ErrorHandler` and `Resource`. Add a `Resource` to the Meter `Descriptor`. The choice to add the `Resource` here (instead of say a `Record` or the `Instrument` itself) was motivated by the definition of the `Descriptor` as the way to uniquely describe a metric instrument. Update the push `Controller` and default `SDK` to pass down their configured `Resource` from instantiation to the metric instruments. * Update New SDK constructor documentation * Change NewDescriptor constructor to take opts Add DescriptorConfig and DescriptorOption to configure the metric Descriptor with the description, unit, keys, and resource. Update all function calls to NewDescriptor to use new function signature. * Apply suggestions from code review Co-Authored-By: Rahul Patel <rghetia@yahoo.com> * Update and add copyright notices * Update push controller creator func Pass the configured ErrorHandler for the controller to the SDK. * Update Resource integration with the SDK Add back the Resource field to the Descriptor that was moved in the last merge with master. Add a resource.Provider interface. Have the default SDK implement the new resource.Provider interface and integrate the new interface into the newSync/newAsync workflows. Now, if the SDK has a Resource defined it will be passed to all Descriptors created for the instruments it creates. * Remove nil check for metric SDK config * Fix and add test for API Options Add an `Equal` method to the Resource so it can be compared with github.com/google/go-cmp/cmp. Add additional test of the API Option unit tests to ensure WithResource correctly sets a new resource. * Move the resource.Provider interface to the API package Move the interface to where it is used. Fix spelling. * Remove errant line * Remove nil checks for the push controller config * Fix check SDK implements Resourcer * Apply suggestions from code review Co-Authored-By: Rahul Patel <rghetia@yahoo.com> Co-authored-by: Rahul Patel <rghetia@yahoo.com>
This commit is contained in:
@ -18,6 +18,7 @@ import (
|
||||
"context"
|
||||
|
||||
"go.opentelemetry.io/otel/api/core"
|
||||
"go.opentelemetry.io/otel/sdk/resource"
|
||||
)
|
||||
|
||||
// MeterImpl is a convenient interface for SDK and test
|
||||
@ -133,6 +134,29 @@ func Configure(opts []Option) Config {
|
||||
return config
|
||||
}
|
||||
|
||||
// Resourcer is implemented by any value that has a Resource method,
|
||||
// which returns the Resource associated with the value.
|
||||
// The Resource method is used to set the Resource for Descriptors of new
|
||||
// metric instruments.
|
||||
type Resourcer interface {
|
||||
Resource() resource.Resource
|
||||
}
|
||||
|
||||
// insertResource inserts a WithResource option at the beginning of opts
|
||||
// using the resource defined by impl if impl implements Resourcer.
|
||||
//
|
||||
// If opts contains a WithResource option already, that Option will take
|
||||
// precedence and overwrite the Resource set from impl.
|
||||
//
|
||||
// The returned []Option may uses the same underlying array as opts.
|
||||
func insertResource(impl MeterImpl, opts []Option) []Option {
|
||||
if r, ok := impl.(Resourcer); ok {
|
||||
// default to the impl resource and override if passed in opts.
|
||||
return append([]Option{WithResource(r.Resource())}, opts...)
|
||||
}
|
||||
return opts
|
||||
}
|
||||
|
||||
// WrapMeterImpl constructs a `Meter` implementation from a
|
||||
// `MeterImpl` implementation.
|
||||
func WrapMeterImpl(impl MeterImpl) Meter {
|
||||
@ -159,6 +183,7 @@ func (m *wrappedMeterImpl) RecordBatch(ctx context.Context, ls LabelSet, ms ...M
|
||||
}
|
||||
|
||||
func (m *wrappedMeterImpl) newSync(name string, metricKind Kind, numberKind core.NumberKind, opts []Option) (SyncImpl, error) {
|
||||
opts = insertResource(m.impl, opts)
|
||||
return m.impl.NewSyncInstrument(NewDescriptor(name, metricKind, numberKind, opts...))
|
||||
}
|
||||
|
||||
@ -219,6 +244,7 @@ func WrapFloat64MeasureInstrument(syncInst SyncImpl, err error) (Float64Measure,
|
||||
}
|
||||
|
||||
func (m *wrappedMeterImpl) newAsync(name string, mkind Kind, nkind core.NumberKind, opts []Option, callback func(func(core.Number, LabelSet))) (AsyncImpl, error) {
|
||||
opts = insertResource(m.impl, opts)
|
||||
return m.impl.NewAsyncInstrument(
|
||||
NewDescriptor(name, mkind, nkind, opts...),
|
||||
callback)
|
||||
|
Reference in New Issue
Block a user