Skip to content

Commit

Permalink
[wip] add config lib
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Boten <[email protected]>
  • Loading branch information
Alex Boten committed Aug 21, 2023
1 parent 9ac039a commit d25025f
Show file tree
Hide file tree
Showing 6 changed files with 1,016 additions and 0 deletions.
143 changes: 143 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package config // import "go.opentelemetry.io/contrib/config"

import (
"fmt"

"go.opentelemetry.io/collector/confmap"
)

func (sp *SpanProcessor) Unmarshal(conf *confmap.Conf) error {
if conf == nil {
return nil
}

if err := conf.Unmarshal(sp); err != nil {
return fmt.Errorf("invalid span processor configuration: %w", err)
}

if sp.Batch != nil {
return sp.Batch.Exporter.Validate()
}
return fmt.Errorf("unsupported span processor type %s", conf.AllKeys())
}

// Validate checks for valid exporters to be configured for the SpanExporter
func (se *SpanExporter) Validate() error {
if se.Console == nil && se.Otlp == nil {
return fmt.Errorf("invalid exporter configuration")
}
return nil
}

func (mr *MetricReader) Unmarshal(conf *confmap.Conf) error {
if conf == nil {
return nil
}

if err := conf.Unmarshal(mr); err != nil {
return fmt.Errorf("invalid metric reader configuration: %w", err)
}

if mr.Pull != nil {
return mr.Pull.Validate()
}
if mr.Periodic != nil {
return mr.Periodic.Validate()
}

return fmt.Errorf("unsupported metric reader type %s", conf.AllKeys())
}

// Validate checks for valid exporters to be configured for the PullMetricReader
func (pmr *PullMetricReader) Validate() error {
if pmr.Exporter.Prometheus == nil {
return fmt.Errorf("invalid exporter configuration")
}
return nil
}

// Validate checks for valid exporters to be configured for the PeriodicMetricReader
func (pmr *PeriodicMetricReader) Validate() error {
if pmr.Exporter.Otlp == nil && pmr.Exporter.Console == nil {
return fmt.Errorf("invalid exporter configuration")
}
return nil
}

func (v *View) Unmarshal(conf *confmap.Conf) error {
if conf == nil {
return nil
}

if err := conf.Unmarshal(v); err != nil {
return fmt.Errorf("invalid view configuration: %w", err)
}

return v.Validate()
}

func (v *View) Validate() error {
if v.Selector == nil || v.Stream == nil {
return fmt.Errorf("invalid view configuration")
}
return nil
}

func (s *ViewSelector) InstrumentNameStr() string {
if s.InstrumentName == nil {
return ""
}
return *s.InstrumentName
}

func (s *ViewSelector) InstrumentTypeStr() string {
if s.InstrumentType == nil {
return ""
}
return *s.InstrumentType
}

func (s *ViewSelector) MeterNameStr() string {
if s.MeterName == nil {
return ""
}
return *s.MeterName
}

func (s *ViewSelector) MeterVersionStr() string {
if s.MeterVersion == nil {
return ""
}
return *s.MeterVersion
}

func (s *ViewSelector) MeterSchemaUrlStr() string {
if s.MeterSchemaUrl == nil {
return ""
}
return *s.MeterSchemaUrl
}

func (s *ViewStream) NameStr() string {
if s.Name == nil {
return ""
}
return *s.Name
}

func (s *ViewStream) DescriptionStr() string {
if s.Description == nil {
return ""
}
return *s.Description
}

func (e *ViewStreamAggregationExplicitBucketHistogram) RecordMinMaxBool() bool {
if e.RecordMinMax == nil {
return false
}
return *e.RecordMinMax
}
Loading

0 comments on commit d25025f

Please sign in to comment.