Skip to content

Commit

Permalink
opamp agent extension, hello world
Browse files Browse the repository at this point in the history
part of open-telemetry#16462

Signed-off-by: Sean Porter <portertech@gmail.com>
  • Loading branch information
portertech committed Dec 2, 2022
1 parent 97530ad commit df750c9
Show file tree
Hide file tree
Showing 10 changed files with 758 additions and 0 deletions.
1 change: 1 addition & 0 deletions extension/opampextension/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include ../../Makefile.Common
42 changes: 42 additions & 0 deletions extension/opampextension/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package opampextension

import (
"errors"

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

// Config has the configuration for the opamp extension.
type Config struct {
config.ExtensionSettings `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct

// Endpoint is the OpAMP server URL. Transport based on the scheme of the URL.
Endpoint string `mapstructure:"endpoint"`

// InstanceUID is a ULID formatted as a 36 character string in canonical
// representation. Auto-generated on start if missing.
InstanceUID string `mapstructure:"instance_uid"`
}

// Validate checks if the extension configuration is valid
func (cfg *Config) Validate() error {
// no need to validate less than 0 case for uint64
if len(cfg.InstanceUID) > 26 {
return errors.New("instance_uid has more than 26 characters")
}
return nil
}
56 changes: 56 additions & 0 deletions extension/opampextension/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package opampextension

import (
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/confmap"
"go.opentelemetry.io/collector/confmap/confmaptest"
)

func TestUnmarshalDefaultConfig(t *testing.T) {
factory := NewFactory()
cfg := factory.CreateDefaultConfig()
assert.NoError(t, component.UnmarshalConfig(confmap.New(), cfg))
assert.Equal(t, factory.CreateDefaultConfig(), cfg)
}

func TestUnmarshalConfig(t *testing.T) {
cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
require.NoError(t, err)
factory := NewFactory()
cfg := factory.CreateDefaultConfig()
assert.NoError(t, component.UnmarshalConfig(cm, cfg))
assert.Equal(t,
&Config{
ExtensionSettings: config.NewExtensionSettings(component.NewID(typeStr)),
Endpoint: "wss://127.0.0.1:4320/v1/opamp",
InstanceUID: "01BX5ZZKBKACTAV9WEVGEMMVRZ",
}, cfg)
}

func TestConfigValidate(t *testing.T) {
cfg := &Config{InstanceUID: "01BX5ZZKBKACTAV9WEVGEMMVRZFAIL"}
err := cfg.Validate()
require.Error(t, err)
assert.Equal(t, "instance_uid has more than 26 characters", err.Error())
}
41 changes: 41 additions & 0 deletions extension/opampextension/factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package opampextension

import (
"context"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config"
)

const (
// The value of extension "type" in configuration.
typeStr = "opamp"
)

func NewFactory() component.ExtensionFactory {
return component.NewExtensionFactory(typeStr, createDefaultConfig, createExtension, component.StabilityLevelBeta)
}

func createDefaultConfig() component.ExtensionConfig {
return &Config{
ExtensionSettings: config.NewExtensionSettings(component.NewID(typeStr)),
}
}

func createExtension(_ context.Context, set component.ExtensionCreateSettings, cfg component.ExtensionConfig) (component.Extension, error) {
return newOpampAgent(cfg.(*Config), set.Logger)
}
44 changes: 44 additions & 0 deletions extension/opampextension/factory_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package opampextension

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/config"
)

func TestFactory_CreateDefaultConfig(t *testing.T) {
cfg := createDefaultConfig()
assert.Equal(t, &Config{ExtensionSettings: config.NewExtensionSettings(component.NewID(typeStr))}, cfg)

assert.NoError(t, componenttest.CheckConfigStruct(cfg))
ext, err := createExtension(context.Background(), componenttest.NewNopExtensionCreateSettings(), cfg)
require.NoError(t, err)
require.NotNil(t, ext)
}

func TestFactory_CreateExtension(t *testing.T) {
cfg := createDefaultConfig().(*Config)
ext, err := createExtension(context.Background(), componenttest.NewNopExtensionCreateSettings(), cfg)
require.NoError(t, err)
require.NotNil(t, ext)
}
40 changes: 40 additions & 0 deletions extension/opampextension/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module github.com/open-telemetry/opentelemetry-collector-contrib/extension/opampextension

go 1.18

require (
github.com/stretchr/testify v1.8.1
go.opentelemetry.io/collector v0.66.1-0.20221202180633-b3e9d209d368
go.opentelemetry.io/collector/component v0.66.1-0.20221202180633-b3e9d209d368
go.uber.org/zap v1.24.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/knadh/koanf v1.4.4 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // 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/pmezard/go-difflib v1.0.0 // indirect
go.opentelemetry.io/collector/confmap v0.0.0-20221201172708-2bdff61fa52a // indirect
go.opentelemetry.io/collector/consumer v0.66.0 // indirect
go.opentelemetry.io/collector/featuregate v0.66.0 // indirect
go.opentelemetry.io/collector/pdata v0.66.0 // indirect
go.opentelemetry.io/otel v1.11.1 // indirect
go.opentelemetry.io/otel/metric v0.33.0 // indirect
go.opentelemetry.io/otel/trace v1.11.1 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
golang.org/x/sys v0.2.0 // indirect
golang.org/x/text v0.4.0 // indirect
google.golang.org/genproto v0.0.0-20211208223120-3a66f561d7aa // indirect
google.golang.org/grpc v1.51.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

0 comments on commit df750c9

Please sign in to comment.