Skip to content

Commit

Permalink
[otel-tracing] Initial opentelemetry support
Browse files Browse the repository at this point in the history
Signed-off-by: David van der Spek <vanderspek.david@gmail.com>
  • Loading branch information
davidspek committed Aug 17, 2023
1 parent 6ccc551 commit c794b99
Show file tree
Hide file tree
Showing 1,286 changed files with 232,520 additions and 1,839 deletions.
6 changes: 5 additions & 1 deletion cmd/registry/config-example.yml
Expand Up @@ -10,7 +10,7 @@ storage:
http:
addr: :5000
headers:
X-Content-Type-Options: [nosniff]
X-Content-Type-Options: [ nosniff ]
auth:
htpasswd:
realm: basic-realm
Expand All @@ -20,3 +20,7 @@ health:
enabled: true
interval: 10s
threshold: 3
otel:
exporter:
name: otlp
endpoint: 127.0.0.1:4317
54 changes: 54 additions & 0 deletions configuration/configuration.go
Expand Up @@ -269,6 +269,32 @@ type Configuration struct {
Classes []string `yaml:"classes"`
} `yaml:"repository,omitempty"`
} `yaml:"policy,omitempty"`

// OpenTelemetry configuration
OpenTelemetry *OpenTelemetryConfig `yaml:"otel,omitempty"`
}

// OpenTelemetryConfig provides open telemetry configuration
type OpenTelemetryConfig struct {
// ServiceName is telemetry define one server name
ServiceName string `yaml:"serviceName,omitempty"`
// Exporter is config exporter
Exporter ExporterConfig `yaml:"exporter"`
// TraceSamplingRatio is trace sampling ratio,default is 1
TraceSamplingRatio float64 `yaml:"traceSamplingRatio,omitempty"`
}

// ExporterConfig provides open telemetry exporter configuration
type ExporterConfig struct {
// Name must be otlp
Name string `yaml:"name"`
// Endpoint is otlp server addr
Endpoint string `yaml:"endpoint"`
}

func (otel OpenTelemetryConfig) String() string {
return fmt.Sprintf("ServiceName=[%s],ExporterName=[%s],ExporterEndpoint=[%s],TraceSamplingRatio=[%+v]",
otel.ServiceName, otel.Exporter.Name, otel.Exporter.Endpoint, otel.TraceSamplingRatio)
}

// Catalog is composed of MaxEntries.
Expand Down Expand Up @@ -735,3 +761,31 @@ func Parse(rd io.Reader) (*Configuration, error) {

return config, nil
}

const (
// ExporterTypeOTLP represents the open telemetry exporter OTLP
// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/otlp.md
ExporterTypeOTLP = "otlp"

// ServiceName is trace service name
ServiceName = "distribution"

// DefaultSamplingRatio default sample ratio
DefaultSamplingRatio = 1
)

// Validate OpenTelemetry config
func (otel *OpenTelemetryConfig) Validate() error {
switch otel.Exporter.Name {
case ExporterTypeOTLP:
if otel.ServiceName == "" {
otel.ServiceName = ServiceName
}
if otel.TraceSamplingRatio == 0 {
otel.TraceSamplingRatio = DefaultSamplingRatio
}
return nil
default:
return fmt.Errorf("invalid argument,unsupported exporter : %s", otel.Exporter.Name)
}
}
34 changes: 32 additions & 2 deletions configuration/configuration_test.go
Expand Up @@ -165,6 +165,14 @@ var configStruct = Configuration{
ReadTimeout: time.Millisecond * 10,
WriteTimeout: time.Millisecond * 10,
},
OpenTelemetry: &OpenTelemetryConfig{
ServiceName: "distribution",
Exporter: ExporterConfig{
Name: "otlp",
Endpoint: "127.0.0.1:4317",
},
TraceSamplingRatio: 0.5,
},
}

// configYamlV0_1 is a Version 0.1 yaml document representing configStruct
Expand Down Expand Up @@ -221,6 +229,12 @@ redis:
dialtimeout: 10ms
readtimeout: 10ms
writetimeout: 10ms
otel:
serviceName: distribution
exporter:
name: otlp
endpoint: 127.0.0.1:4317
traceSamplingRatio: 0.5
`

// inmemoryConfigYamlV0_1 is a Version 0.1 yaml document specifying an inmemory
Expand Down Expand Up @@ -305,6 +319,7 @@ func (suite *ConfigSuite) TestParseInmemory(c *check.C) {
IdleTimeout time.Duration `yaml:"idletimeout,omitempty"`
} `yaml:"pool,omitempty"`
}{}
suite.expectedConfig.OpenTelemetry = nil

config, err := Parse(bytes.NewReader([]byte(inmemoryConfigYamlV0_1)))
c.Assert(err, check.IsNil)
Expand Down Expand Up @@ -342,6 +357,7 @@ func (suite *ConfigSuite) TestParseIncomplete(c *check.C) {
IdleTimeout time.Duration `yaml:"idletimeout,omitempty"`
} `yaml:"pool,omitempty"`
}{}
suite.expectedConfig.OpenTelemetry = nil

// Note: this also tests that REGISTRY_STORAGE and
// REGISTRY_STORAGE_FILESYSTEM_ROOTDIRECTORY can be used together
Expand Down Expand Up @@ -560,7 +576,21 @@ func (suite *ConfigSuite) TestParseEnvMany(c *check.C) {
c.Assert(err, check.IsNil)
}

func checkStructs(c *check.C, t reflect.Type, structsChecked map[string]struct{}) {
func (suite *ConfigSuite) TestOpenTelemetry(c *C) {

Check failure on line 579 in configuration/configuration_test.go

View workflow job for this annotation

GitHub Actions / test (1.18)

undefined: C

Check failure on line 579 in configuration/configuration_test.go

View workflow job for this annotation

GitHub Actions / test (1.19.10)

undefined: C
config, err := Parse(bytes.NewReader([]byte(configYamlV0_1)))
c.Assert(err, IsNil)

Check failure on line 581 in configuration/configuration_test.go

View workflow job for this annotation

GitHub Actions / test (1.18)

undefined: IsNil

Check failure on line 581 in configuration/configuration_test.go

View workflow job for this annotation

GitHub Actions / test (1.19.10)

undefined: IsNil
c.Assert(config.OpenTelemetry, NotNil)

Check failure on line 582 in configuration/configuration_test.go

View workflow job for this annotation

GitHub Actions / test (1.18)

undefined: NotNil

Check failure on line 582 in configuration/configuration_test.go

View workflow job for this annotation

GitHub Actions / test (1.19.10)

undefined: NotNil
c.Assert(config, DeepEquals, suite.expectedConfig)

Check failure on line 583 in configuration/configuration_test.go

View workflow job for this annotation

GitHub Actions / test (1.18)

undefined: DeepEquals

Check failure on line 583 in configuration/configuration_test.go

View workflow job for this annotation

GitHub Actions / test (1.19.10)

undefined: DeepEquals

err = config.OpenTelemetry.Validate()
c.Assert(err, IsNil)

Check failure on line 586 in configuration/configuration_test.go

View workflow job for this annotation

GitHub Actions / test (1.18)

undefined: IsNil

Check failure on line 586 in configuration/configuration_test.go

View workflow job for this annotation

GitHub Actions / test (1.19.10)

undefined: IsNil

config.OpenTelemetry.Exporter.Name = "test"
err = config.OpenTelemetry.Validate()
c.Assert(err, NotNil)

Check failure on line 590 in configuration/configuration_test.go

View workflow job for this annotation

GitHub Actions / test (1.18)

undefined: NotNil

Check failure on line 590 in configuration/configuration_test.go

View workflow job for this annotation

GitHub Actions / test (1.19.10)

undefined: NotNil
}

func checkStructs(c *C, t reflect.Type, structsChecked map[string]struct{}) {

Check failure on line 593 in configuration/configuration_test.go

View workflow job for this annotation

GitHub Actions / test (1.18)

undefined: C

Check failure on line 593 in configuration/configuration_test.go

View workflow job for this annotation

GitHub Actions / test (1.19.10)

undefined: C
for t.Kind() == reflect.Ptr || t.Kind() == reflect.Map || t.Kind() == reflect.Slice {
t = t.Elem()
}
Expand Down Expand Up @@ -636,6 +666,6 @@ func copyConfig(config Configuration) *Configuration {
}

configCopy.Redis = config.Redis

configCopy.OpenTelemetry = config.OpenTelemetry
return configCopy
}
68 changes: 59 additions & 9 deletions go.mod
Expand Up @@ -19,65 +19,115 @@ require (
github.com/gorilla/handlers v1.5.1
github.com/gorilla/mux v1.8.0
github.com/hashicorp/golang-lru v0.5.4
github.com/klauspost/compress v1.16.5
github.com/mitchellh/mapstructure v1.1.2
github.com/mitchellh/mapstructure v1.5.0
github.com/opencontainers/go-digest v1.0.0
github.com/opencontainers/image-spec v1.0.2
github.com/prometheus/client_golang v1.12.1 // indirect; updated to latest
github.com/prometheus/client_golang v1.12.2 // indirect; updated to latest
github.com/sirupsen/logrus v1.8.1
github.com/spf13/cobra v1.6.1
github.com/stretchr/testify v1.8.1
github.com/yvasiyarov/gorelic v0.0.0-20141212073537-a9bba5b9ab50
go.opentelemetry.io/collector v0.58.0
go.opentelemetry.io/otel v1.9.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.8.0
go.opentelemetry.io/otel/sdk v1.9.0
go.opentelemetry.io/otel/trace v1.9.0
golang.org/x/crypto v0.7.0
golang.org/x/oauth2 v0.6.0
google.golang.org/api v0.114.0
google.golang.org/grpc v1.53.0
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c
gopkg.in/yaml.v2 v2.4.0
)

require (
cloud.google.com/go v0.110.0 // indirect
contrib.go.opencensus.io/exporter/prometheus v0.4.1 // indirect
github.com/Azure/azure-sdk-for-go/sdk/internal v1.3.0 // indirect
github.com/AzureAD/microsoft-authentication-library-for-go v1.0.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bitly/go-simplejson v0.5.0 // indirect
github.com/bugsnag/osext v0.0.0-20130617224835-0dd3f918b21b // indirect
github.com/bugsnag/panicwrap v0.0.0-20151223152923-e2c28503fcd0 // indirect
github.com/cenkalti/backoff/v4 v4.1.3 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cyphar/filepath-securejoin v0.2.3 // indirect
github.com/felixge/httpsnoop v1.0.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/felixge/httpsnoop v1.0.3 // indirect
github.com/go-kit/log v0.2.0 // indirect
github.com/go-logfmt/logfmt v0.5.1 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/googleapis/gax-go/v2 v2.7.1 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/kr/pretty v0.2.1 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.15.9
github.com/knadh/koanf v1.4.2 // indirect
github.com/kr/pretty v0.3.0 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/osext v0.0.0-20151018003038-5e2d6d41470f // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/mostynb/go-grpc-compression v1.1.17 // indirect
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.32.1 // indirect
github.com/prometheus/common v0.37.0 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
github.com/prometheus/statsd_exporter v0.21.0 // indirect
github.com/rogpeppe/go-internal v1.6.1 // indirect
github.com/rs/cors v1.8.2 // indirect
github.com/shirou/gopsutil/v3 v3.22.7 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/tklauser/go-sysconf v0.3.10 // indirect
github.com/tklauser/numcpus v0.4.0 // indirect
github.com/yusufpapurcu/wmi v1.2.2 // indirect
github.com/yvasiyarov/go-metrics v0.0.0-20140926110328-57bccd1ccd43 // indirect
github.com/yvasiyarov/newrelic_platform_go v0.0.0-20140908184405-b21fdbd4370f // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/collector/pdata v0.56.0 // indirect
go.opentelemetry.io/collector/semconv v0.56.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.34.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.34.0 // indirect
go.opentelemetry.io/contrib/zpages v0.34.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.8.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.8.0 // indirect
go.opentelemetry.io/otel/exporters/prometheus v0.31.0 // indirect
go.opentelemetry.io/otel/metric v0.31.0 // indirect
go.opentelemetry.io/otel/sdk/metric v0.31.0 // indirect
go.opentelemetry.io/proto/otlp v0.18.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
go.uber.org/zap v1.21.0 // indirect
golang.org/x/net v0.8.0 // indirect; updated for CVE-2022-27664, CVE-2022-41717
golang.org/x/sys v0.6.0 // indirect
golang.org/x/text v0.8.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230320184635-7606e756e683 // indirect
google.golang.org/grpc v1.53.0 // indirect
google.golang.org/protobuf v1.29.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

require (
cloud.google.com/go/compute v1.18.0 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
cloud.google.com/go/iam v0.12.0 // indirect
github.com/cyphar/filepath-securejoin v0.2.3 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.2.3 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
Expand Down

0 comments on commit c794b99

Please sign in to comment.