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 29b5e79 commit 624b9b5
Show file tree
Hide file tree
Showing 1,536 changed files with 272,111 additions and 5,363 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 @@ -258,6 +258,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)
}

// LogHook is composed of hook Level and Type.
Expand Down Expand Up @@ -704,3 +730,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)
}
}
32 changes: 31 additions & 1 deletion configuration/configuration_test.go
Expand Up @@ -125,6 +125,14 @@ var configStruct = Configuration{
Disabled: false,
},
},
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 @@ -169,6 +177,12 @@ http:
- /path/to/ca.pem
headers:
X-Content-Type-Options: [nosniff]
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 @@ -236,6 +250,7 @@ func (suite *ConfigSuite) TestParseInmemory(c *C) {
suite.expectedConfig.Storage = Storage{"inmemory": Parameters{}}
suite.expectedConfig.Reporting = Reporting{}
suite.expectedConfig.Log.Fields = nil
suite.expectedConfig.OpenTelemetry = nil

config, err := Parse(bytes.NewReader([]byte(inmemoryConfigYamlV0_1)))
c.Assert(err, IsNil)
Expand All @@ -256,6 +271,7 @@ func (suite *ConfigSuite) TestParseIncomplete(c *C) {
suite.expectedConfig.Reporting = Reporting{}
suite.expectedConfig.Notifications = Notifications{}
suite.expectedConfig.HTTP.Headers = nil
suite.expectedConfig.OpenTelemetry = nil

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

func (suite *ConfigSuite) TestOpenTelemetry(c *C) {
config, err := Parse(bytes.NewReader([]byte(configYamlV0_1)))
c.Assert(err, IsNil)
c.Assert(config.OpenTelemetry, NotNil)
c.Assert(config, DeepEquals, suite.expectedConfig)

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

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

func checkStructs(c *C, t reflect.Type, structsChecked map[string]struct{}) {
for t.Kind() == reflect.Ptr || t.Kind() == reflect.Map || t.Kind() == reflect.Slice {
t = t.Elem()
Expand Down Expand Up @@ -547,6 +577,6 @@ func copyConfig(config Configuration) *Configuration {
for k, v := range config.HTTP.Headers {
configCopy.HTTP.Headers[k] = v
}

configCopy.OpenTelemetry = config.OpenTelemetry
return configCopy
}
77 changes: 64 additions & 13 deletions go.mod
Expand Up @@ -18,57 +18,108 @@ 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/mitchellh/mapstructure v1.1.2
github.com/mitchellh/mapstructure v1.5.0
github.com/ncw/swift v1.0.47
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.0
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.0.0-20220511200225-c6db032c6c88
golang.org/x/oauth2 v0.0.0-20210514164344-f6687ab2804c
golang.org/x/oauth2 v0.0.0-20220223155221-ee480838109b
google.golang.org/api v0.30.0
google.golang.org/cloud v0.0.0-20151119220103-975617b05ea8
google.golang.org/grpc v1.48.0
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15
gopkg.in/yaml.v2 v2.4.0
)

require (
cloud.google.com/go v0.65.0 // indirect
contrib.go.opencensus.io/exporter/prometheus v0.4.1 // indirect
github.com/Azure/azure-sdk-for-go/sdk/internal v1.1.1 // indirect
github.com/AzureAD/microsoft-authentication-library-for-go v0.8.1 // 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.1.2 // 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.4.2 // 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/google/uuid v1.1.1 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/googleapis/gax-go/v2 v2.0.5 // 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.1.0 // indirect
github.com/kr/text v0.1.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.15.9 // indirect
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-20210115035449-ce105d075bb4 // 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.22.4 // indirect
go.opencensus.io v0.23.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.7.0 // indirect; updated for CVE-2022-27664, CVE-2022-41717
golang.org/x/sys v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
google.golang.org/appengine v1.6.6 // indirect
google.golang.org/genproto v0.0.0-20200825200019-8632dd797987 // indirect
google.golang.org/grpc v1.31.0 // indirect
google.golang.org/protobuf v1.26.0 // indirect
google.golang.org/genproto v0.0.0-20211208223120-3a66f561d7aa // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

0 comments on commit 624b9b5

Please sign in to comment.