mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2025-05-27 22:37:43 +02:00
* 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>
80 lines
2.3 KiB
Go
80 lines
2.3 KiB
Go
// Copyright 2020, OpenTelemetry Authors
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
// Package resource provides functionality for resource, which capture
|
|
// identifying information about the entities for which signals are exported.
|
|
package resource
|
|
|
|
import (
|
|
"reflect"
|
|
|
|
"go.opentelemetry.io/otel/api/core"
|
|
)
|
|
|
|
// Resource describes an entity about which identifying information and metadata is exposed.
|
|
type Resource struct {
|
|
labels map[core.Key]core.Value
|
|
}
|
|
|
|
// New creates a resource from a set of attributes.
|
|
// If there are duplicates keys then the first value of the key is preserved.
|
|
func New(kvs ...core.KeyValue) *Resource {
|
|
res := &Resource{
|
|
labels: map[core.Key]core.Value{},
|
|
}
|
|
for _, kv := range kvs {
|
|
if _, ok := res.labels[kv.Key]; !ok {
|
|
res.labels[kv.Key] = kv.Value
|
|
}
|
|
}
|
|
return res
|
|
}
|
|
|
|
// Merge creates a new resource by combining resource a and b.
|
|
// If there are common key between resource a and b then value from resource a is preserved.
|
|
// If one of the resources is nil then the other resource is returned without creating a new one.
|
|
func Merge(a, b *Resource) *Resource {
|
|
if a == nil {
|
|
return b
|
|
}
|
|
if b == nil {
|
|
return a
|
|
}
|
|
res := &Resource{
|
|
labels: map[core.Key]core.Value{},
|
|
}
|
|
for k, v := range b.labels {
|
|
res.labels[k] = v
|
|
}
|
|
// labels from resource a overwrite labels from resource b.
|
|
for k, v := range a.labels {
|
|
res.labels[k] = v
|
|
}
|
|
return res
|
|
}
|
|
|
|
// Attributes returns a copy of attributes from the resource.
|
|
func (r Resource) Attributes() []core.KeyValue {
|
|
attrs := make([]core.KeyValue, 0, len(r.labels))
|
|
for k, v := range r.labels {
|
|
attrs = append(attrs, core.KeyValue{Key: k, Value: v})
|
|
}
|
|
return attrs
|
|
}
|
|
|
|
// Equal returns true if other Resource is the equal to r.
|
|
func (r Resource) Equal(other Resource) bool {
|
|
return reflect.DeepEqual(r.labels, other.labels)
|
|
}
|