Skip to content

Commit

Permalink
[config] introduce config package (#4376)
Browse files Browse the repository at this point in the history
* [config] introduce config package

This change introduces the config package with the following changes:

- add go-jsonschema tool to tools.go
- add `make genjsonschema` target to Makefile
- run the genjsonschema target to generate the code from the opentelemetry configuration schema
- add a readme for the package

Signed-off-by: Alex Boten <aboten@lightstep.com>
  • Loading branch information
Alex Boten committed Oct 10, 2023
1 parent eb489c5 commit 4ec3201
Show file tree
Hide file tree
Showing 10 changed files with 851 additions and 4 deletions.
9 changes: 9 additions & 0 deletions .github/dependabot.yml
Expand Up @@ -109,6 +109,15 @@ updates:
schedule:
interval: weekly
day: sunday
- package-ecosystem: gomod
directory: /config
labels:
- dependencies
- go
- Skip Changelog
schedule:
interval: weekly
day: sunday
- package-ecosystem: gomod
directory: /detectors/aws/ec2
labels:
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -12,6 +12,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

- Add the new `go.opentelemetry.io/contrib/instrgen` package to provide auto-generated source code instrumentation. (#3068, #3108)
- Add `"go.opentelemetry.io/contrib/samplers/jaegerremote".WithSamplingStrategyFetcher` which sets custom fetcher implementation. (#4045)
- Add `"go.opentelemetry.io/contrib/config"` package that includes configuration models generated via go-jsonschema (#4376)

### Changed

Expand Down
33 changes: 33 additions & 0 deletions Makefile
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

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

# Generate
Expand Down Expand Up @@ -283,3 +286,33 @@ 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=v0.1.0

# 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) \
--capitalization ID \
--capitalization OTLP \
--struct-name-from-title \
--package config \
--tags mapstructure \
--output ${GENERATED_CONFIG} \
${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
31 changes: 31 additions & 0 deletions config/README.md
@@ -0,0 +1,31 @@
# Configuration Library

This package can be used to parse a configuration file that follows the JSON
Schema defined by the [OpenTelemetry Configuration] schema.

The package contains:

- models generated via the JSON schema using the [go-jsonschema] library
- a `Create` function that interprets [configuration model] and return SDK components (TODO)
- a `Parse` function that parses and validates a [configuration file] (TODO)

## Using the generate model code

The `generated_config.go` code in versioned submodule can be used directly as-is to programmatically
produce a configuration model that can be then used as a parameter to the `Create` function. Note
that the package is versioned to match the release versioning of the opentelemetry-configuration
repository.

## Using the `Create` function (TODO)

## Using the `Parse` function (TODO)

The original code from the package comes from the [OpenTelemetry Collector's service] telemetry
configuration code. The intent being to share this code across implementations and reduce
duplication where possible.

[OpenTelemetry Configuration]: https://github.com/open-telemetry/opentelemetry-configuration/
[go-jsonschema]: https://github.com/omissis/go-jsonschema
[configuration model]: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/configuration/file-configuration.md#configuration-model
[configuration file]: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/configuration/file-configuration.md#configuration-file
[OpenTelemetry Collector's service]: https://github.com/open-telemetry/opentelemetry-collector/blob/7c5ecef11dff4ce5501c9683b277a25a61ea0f1a/service/telemetry/generated_config.go

0 comments on commit 4ec3201

Please sign in to comment.