forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
internal/exp/metrics: identity types (open-telemetry#31017)
Adds a new internal, _experimental_ package `metrics/identity` which implements identity types for resource, scope, metric and stream. This is closely related to work being done in open-telemetry#30707 and open-telemetry#30827. The package is specifically experimental, as it shall be treated as an internal component to above processors which may change at any moment as long as those are under active initial development. /cc @jpkrohling @djaglowski @RichieSams
- Loading branch information
1 parent
7cebad3
commit acdd4d3
Showing
15 changed files
with
329 additions
and
0 deletions.
There are no files selected for viewing
Validating CODEOWNERS rules …
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
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 @@ | ||
include ../../../Makefile.Common |
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,28 @@ | ||
module github.com/open-telemetry/opentelemetry-collector-contrib/internal/exp/metrics | ||
|
||
replace github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil => ../../../pkg/pdatautil | ||
|
||
require ( | ||
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil v0.0.0-00010101000000-000000000000 | ||
go.opentelemetry.io/collector/pdata v1.1.0 | ||
) | ||
|
||
require ( | ||
github.com/cespare/xxhash/v2 v2.2.0 // indirect | ||
github.com/gogo/protobuf v1.3.2 // indirect | ||
github.com/golang/protobuf v1.5.3 // indirect | ||
github.com/json-iterator/go v1.1.12 // indirect | ||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect | ||
github.com/modern-go/reflect2 v1.0.2 // indirect | ||
go.uber.org/multierr v1.11.0 // indirect | ||
golang.org/x/net v0.18.0 // indirect | ||
golang.org/x/sys v0.14.0 // indirect | ||
golang.org/x/text v0.14.0 // indirect | ||
google.golang.org/genproto/googleapis/rpc v0.0.0-20231106174013-bbf56f31fb17 // indirect | ||
google.golang.org/grpc v1.61.0 // indirect | ||
google.golang.org/protobuf v1.32.0 // indirect | ||
) | ||
|
||
go 1.21 | ||
|
||
toolchain go1.21.1 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,8 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// identity types for metrics and sample streams. | ||
// | ||
// Use the `Of*(T) -> I` functions to obtain a unique, comparable (==) and | ||
// hashable (map key) identity value I for T. | ||
package identity // import "github.com/open-telemetry/opentelemetry-collector-contrib/internal/exp/metrics/identity" |
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,71 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package identity // import "github.com/open-telemetry/opentelemetry-collector-contrib/internal/exp/metrics/identity" | ||
|
||
import ( | ||
"hash" | ||
|
||
"go.opentelemetry.io/collector/pdata/pcommon" | ||
"go.opentelemetry.io/collector/pdata/pmetric" | ||
) | ||
|
||
type metric = Metric | ||
|
||
type Metric struct { | ||
scope | ||
|
||
name string | ||
unit string | ||
ty pmetric.MetricType | ||
|
||
monotonic bool | ||
temporality pmetric.AggregationTemporality | ||
} | ||
|
||
func (i Metric) Hash() hash.Hash64 { | ||
sum := i.scope.Hash() | ||
sum.Write([]byte(i.name)) | ||
sum.Write([]byte(i.unit)) | ||
|
||
var mono byte | ||
if i.monotonic { | ||
mono = 1 | ||
} | ||
sum.Write([]byte{byte(i.ty), mono, byte(i.temporality)}) | ||
return sum | ||
} | ||
|
||
func (i Metric) Scope() Scope { | ||
return i.scope | ||
} | ||
|
||
func OfMetric(scope Scope, m pmetric.Metric) Metric { | ||
id := Metric{ | ||
scope: scope, | ||
name: m.Name(), | ||
unit: m.Unit(), | ||
ty: m.Type(), | ||
} | ||
|
||
switch m.Type() { | ||
case pmetric.MetricTypeSum: | ||
sum := m.Sum() | ||
id.monotonic = sum.IsMonotonic() | ||
id.temporality = sum.AggregationTemporality() | ||
case pmetric.MetricTypeExponentialHistogram: | ||
exp := m.ExponentialHistogram() | ||
id.monotonic = true | ||
id.temporality = exp.AggregationTemporality() | ||
case pmetric.MetricTypeHistogram: | ||
hist := m.Histogram() | ||
id.monotonic = true | ||
id.temporality = hist.AggregationTemporality() | ||
} | ||
|
||
return id | ||
} | ||
|
||
func OfResourceMetric(res pcommon.Resource, scope pcommon.InstrumentationScope, metric pmetric.Metric) Metric { | ||
return OfMetric(OfScope(OfResource(res), scope), metric) | ||
} |
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,31 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package identity // import "github.com/open-telemetry/opentelemetry-collector-contrib/internal/exp/metrics/identity" | ||
|
||
import ( | ||
"hash" | ||
"hash/fnv" | ||
|
||
"go.opentelemetry.io/collector/pdata/pcommon" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil" | ||
) | ||
|
||
type resource = Resource | ||
|
||
type Resource struct { | ||
attrs [16]byte | ||
} | ||
|
||
func (r Resource) Hash() hash.Hash64 { | ||
sum := fnv.New64a() | ||
sum.Write(r.attrs[:]) | ||
return sum | ||
} | ||
|
||
func OfResource(r pcommon.Resource) Resource { | ||
return Resource{ | ||
attrs: pdatautil.MapHash(r.Attributes()), | ||
} | ||
} |
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,43 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package identity // import "github.com/open-telemetry/opentelemetry-collector-contrib/internal/exp/metrics/identity" | ||
|
||
import ( | ||
"hash" | ||
|
||
"go.opentelemetry.io/collector/pdata/pcommon" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil" | ||
) | ||
|
||
type scope = Scope | ||
|
||
type Scope struct { | ||
resource resource | ||
|
||
name string | ||
version string | ||
attrs [16]byte | ||
} | ||
|
||
func (s Scope) Hash() hash.Hash64 { | ||
sum := s.resource.Hash() | ||
sum.Write([]byte(s.name)) | ||
sum.Write([]byte(s.version)) | ||
sum.Write(s.attrs[:]) | ||
return sum | ||
} | ||
|
||
func (s Scope) Resource() Resource { | ||
return s.resource | ||
} | ||
|
||
func OfScope(res Resource, scope pcommon.InstrumentationScope) Scope { | ||
return Scope{ | ||
resource: res, | ||
name: scope.Name(), | ||
version: scope.Version(), | ||
attrs: pdatautil.MapHash(scope.Attributes()), | ||
} | ||
} |
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,37 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package identity // import "github.com/open-telemetry/opentelemetry-collector-contrib/internal/exp/metrics/identity" | ||
|
||
import ( | ||
"hash" | ||
|
||
"go.opentelemetry.io/collector/pdata/pcommon" | ||
"go.opentelemetry.io/collector/pdata/pmetric" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil" | ||
) | ||
|
||
type Stream struct { | ||
metric | ||
attrs [16]byte | ||
} | ||
|
||
func (i Stream) Hash() hash.Hash64 { | ||
sum := i.metric.Hash() | ||
sum.Write(i.attrs[:]) | ||
return sum | ||
} | ||
|
||
func (i Stream) Metric() Metric { | ||
return i.metric | ||
} | ||
|
||
func OfStream[DataPoint attrPoint](m Metric, dp DataPoint) Stream { | ||
return Stream{metric: m, attrs: pdatautil.MapHash(dp.Attributes())} | ||
} | ||
|
||
type attrPoint interface { | ||
pmetric.NumberDataPoint | pmetric.HistogramDataPoint | pmetric.ExponentialHistogramDataPoint | pmetric.SummaryDataPoint | ||
Attributes() pcommon.Map | ||
} |
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,24 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package identity // import "github.com/open-telemetry/opentelemetry-collector-contrib/internal/exp/metrics/identity" | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
func (r Resource) String() string { | ||
return fmt.Sprintf("resource/%x", r.Hash().Sum64()) | ||
} | ||
|
||
func (s Scope) String() string { | ||
return fmt.Sprintf("scope/%x", s.Hash().Sum64()) | ||
} | ||
|
||
func (m Metric) String() string { | ||
return fmt.Sprintf("metric/%x", m.Hash().Sum64()) | ||
} | ||
|
||
func (s Stream) String() string { | ||
return fmt.Sprintf("stream/%x", s.Hash().Sum64()) | ||
} |
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,3 @@ | ||
status: | ||
codeowners: | ||
active: [sh0rez, RichieSams] |
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