Skip to content

Commit

Permalink
add config parsing module
Browse files Browse the repository at this point in the history
This module parses configuration as defined per the opentelemetry-configuration schema.

Signed-off-by: Alex Boten <aboten@lightstep.com>
  • Loading branch information
Alex Boten committed Sep 27, 2023
1 parent e901cf0 commit fc13fc3
Show file tree
Hide file tree
Showing 12 changed files with 1,130 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Set the description for the `rpc.server.duration` metric in `go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc`. (#4302)
- Add `NewServerHandler` and `NewClientHandler` that return a `grpc.StatsHandler` used for gRPC instrumentation in `go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc`. (#3002)
- Add new Prometheus bridge module in `go.opentelemetry.io/contrib/bridges/prometheus`. (#4227)
- Add module to parse configuration as per opentelemetry-config schema. (#4228)

### Changed

Expand Down
31 changes: 31 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ $(GOTMPL): PACKAGE=go.opentelemetry.io/build-tools/gotmpl
GORELEASE = $(TOOLS)/gorelease
$(GORELEASE): PACKAGE=golang.org/x/exp/cmd/gorelease

GOJSONSCHEMA = $(TOOLS)/go-jsonschema
$(GOJSONSCHEMA): PACKAGE=github.com/atombender/go-jsonschema/cmd/gojsonschema

tools: $(GOLANGCI_LINT) $(MISSPELL) $(GOCOVMERGE) $(STRINGER) $(PORTO) $(MULTIMOD) $(DBOTCONF) $(CROSSLINK) $(GOTMPL) $(GORELEASE)

# Generate
Expand Down Expand Up @@ -283,3 +286,31 @@ COMMIT ?= "HEAD"
add-tags: | $(MULTIMOD)
@[ "${MODSET}" ] || ( echo ">> env var MODSET is not set"; exit 1 )
$(MULTIMOD) verify && $(MULTIMOD) tag -m ${MODSET} -c ${COMMIT}

# The source directory for opentelemetry-configuration schema.
OPENTELEMETRY_CONFIGURATION_JSONSCHEMA_SRC_DIR=tmp/opentelememetry-configuration

# The SHA matching the current version of the opentelemetry-configuration schema to use
OPENTELEMETRY_CONFIGURATION_JSONSCHEMA_VERSION=main

# Cleanup temporary directory
genjsonschema-cleanup:
rm -Rf ${OPENTELEMETRY_CONFIGURATION_JSONSCHEMA_SRC_DIR}

GENERATED_CONFIG=./config/generated_config.go

# Generate structs for configuration from opentelemetry-configuration schema
genjsonschema: genjsonschema-cleanup $(GOJSONSCHEMA)
mkdir -p ${OPENTELEMETRY_CONFIGURATION_JSONSCHEMA_SRC_DIR}
curl -sSL https://api.github.com/repos/open-telemetry/opentelemetry-configuration/tarball/${OPENTELEMETRY_CONFIGURATION_JSONSCHEMA_VERSION} | tar xz --strip 1 -C ${OPENTELEMETRY_CONFIGURATION_JSONSCHEMA_SRC_DIR}
$(GOJSONSCHEMA) \
--package config \
--tags mapstructure \
--output ${GENERATED_CONFIG} \
--schema-package=https://opentelemetry.io/otelconfig/opentelemetry_configuration.json=github.com/open-telemetry/opentelemetry-collector/schema \
${OPENTELEMETRY_CONFIGURATION_JSONSCHEMA_SRC_DIR}/schema/opentelemetry_configuration.json
@echo Modify jsonschema generated files.
sed -f ./config/jsonschema_patch.sed ${GENERATED_CONFIG} > ${GENERATED_CONFIG}.tmp
mv ${GENERATED_CONFIG}.tmp ${GENERATED_CONFIG}
$(MAKE) lint
$(MAKE) genjsonschema-cleanup
123 changes: 123 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

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

import (
"fmt"
)

// Validate checks for a valid batch processor for the SpanProcessor.
func (sp *SpanProcessor) Validate() error {
if sp.Batch != nil {
return sp.Batch.Exporter.Validate()
}
return fmt.Errorf("unsupported span processor type")

Check warning on line 15 in config/config.go

View check run for this annotation

Codecov / codecov/patch

config/config.go#L11-L15

Added lines #L11 - L15 were not covered by tests
}

// 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

Check warning on line 23 in config/config.go

View check run for this annotation

Codecov / codecov/patch

config/config.go#L19-L23

Added lines #L19 - L23 were not covered by tests
}

// Validate checks for either a valid pull or periodic exporter for the MetricReader.
func (mr *MetricReader) Validate() error {
if mr.Pull != nil {
return mr.Pull.Validate()
}
if mr.Periodic != nil {
return mr.Periodic.Validate()
}

Check warning on line 33 in config/config.go

View check run for this annotation

Codecov / codecov/patch

config/config.go#L27-L33

Added lines #L27 - L33 were not covered by tests

return fmt.Errorf("unsupported metric reader type")

Check warning on line 35 in config/config.go

View check run for this annotation

Codecov / codecov/patch

config/config.go#L35

Added line #L35 was not covered by tests
}

// 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

Check warning on line 43 in config/config.go

View check run for this annotation

Codecov / codecov/patch

config/config.go#L39-L43

Added lines #L39 - L43 were not covered by tests
}

// 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

Check warning on line 51 in config/config.go

View check run for this annotation

Codecov / codecov/patch

config/config.go#L47-L51

Added lines #L47 - L51 were not covered by tests
}

// Validate checks for a valid Selector or Stream to be configured for the View.
func (v *View) Validate() error {
if v.Selector == nil || v.Stream == nil {
return fmt.Errorf("invalid view configuration")
}
return nil

Check warning on line 59 in config/config.go

View check run for this annotation

Codecov / codecov/patch

config/config.go#L55-L59

Added lines #L55 - L59 were not covered by tests
}

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

Check warning on line 65 in config/config.go

View check run for this annotation

Codecov / codecov/patch

config/config.go#L64-L65

Added lines #L64 - L65 were not covered by tests
return *s.InstrumentName
}

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

Check warning on line 72 in config/config.go

View check run for this annotation

Codecov / codecov/patch

config/config.go#L71-L72

Added lines #L71 - L72 were not covered by tests
return string(*s.InstrumentType)
}

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

Check warning on line 79 in config/config.go

View check run for this annotation

Codecov / codecov/patch

config/config.go#L78-L79

Added lines #L78 - L79 were not covered by tests
return *s.MeterName
}

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

Check warning on line 86 in config/config.go

View check run for this annotation

Codecov / codecov/patch

config/config.go#L85-L86

Added lines #L85 - L86 were not covered by tests
return *s.MeterVersion
}

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

Check warning on line 93 in config/config.go

View check run for this annotation

Codecov / codecov/patch

config/config.go#L92-L93

Added lines #L92 - L93 were not covered by tests
return *s.MeterSchemaUrl
}

func (s *ViewSelector) unitStr() string {
if s.Unit == nil {
return ""
}
return *s.Unit

Check warning on line 101 in config/config.go

View check run for this annotation

Codecov / codecov/patch

config/config.go#L101

Added line #L101 was not covered by tests
}

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

Check warning on line 107 in config/config.go

View check run for this annotation

Codecov / codecov/patch

config/config.go#L106-L107

Added lines #L106 - L107 were not covered by tests
return *s.Name
}

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

Check warning on line 114 in config/config.go

View check run for this annotation

Codecov / codecov/patch

config/config.go#L113-L114

Added lines #L113 - L114 were not covered by tests
return *s.Description
}

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

Check warning on line 122 in config/config.go

View check run for this annotation

Codecov / codecov/patch

config/config.go#L118-L122

Added lines #L118 - L122 were not covered by tests
}

0 comments on commit fc13fc3

Please sign in to comment.