Skip to content

Commit

Permalink
Rebased extension and changed fmt.Errorf into errors.New
Browse files Browse the repository at this point in the history
  • Loading branch information
jerrytfleung committed Jan 17, 2024
1 parent 5c91dc6 commit d3193cb
Show file tree
Hide file tree
Showing 8 changed files with 601 additions and 78 deletions.
6 changes: 3 additions & 3 deletions extension/solarwindsapmsettingsextension/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<!-- end autogenerated section -->

## Overview
The Solarwinds APM Settings extension gets Solarwinds APM specific settings from Solarwinds APM collector and outputs `/tmp/solarwinds-apm-settings.json` periodically
The Solarwinds APM Settings extension gets Solarwinds APM specific settings from Solarwinds APM collector and `/tmp/solarwinds-apm-settings.json` & `/tmp/solarwinds-apm-settings-raw` periodically.

## Configuration

Expand All @@ -22,7 +22,7 @@ extensions:
solarwindsapmsettings:
endpoint: "<endpoint>"
key: "<token>:<name>"
interval: 1m
interval: 10s
```
### endpoint (Required)
Expand All @@ -34,4 +34,4 @@ The service key in format `<token>:<name>` for `getSettings` from Solarwinds APM
### interval (Optional)
Periodic interval to get Solarwinds APM specific settings from Solarwinds APM collector.

Default: `1m`
Default: `10s`
15 changes: 7 additions & 8 deletions extension/solarwindsapmsettingsextension/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package solarwindsapmsettingsextension

import (
"fmt"
"errors"
"strconv"
"strings"
"time"
Expand All @@ -15,25 +15,24 @@ type Config struct {

func (cfg *Config) Validate() error {
if len(cfg.Endpoint) == 0 {
return fmt.Errorf("endpoint must not be empty")
return errors.New("endpoint must not be empty")
}
endpointArr := strings.Split(cfg.Endpoint, ":")
if len(endpointArr) != 2 {
return fmt.Errorf("endpoint should be in \"<host>:<port>\" format")
return errors.New("endpoint should be in \"<host>:<port>\" format")
}
if _, err := strconv.Atoi(endpointArr[1]); err != nil {
return fmt.Errorf("the <port> portion of endpoint has to be an integer")
return errors.New("the <port> portion of endpoint has to be an integer")
}
if len(cfg.Key) == 0 {
return fmt.Errorf("key must not be empty")
return errors.New("key must not be empty")
}
keyArr := strings.Split(cfg.Key, ":")
if len(keyArr) != 2 {
return fmt.Errorf("key should be in \"<token>:<service_name>\" format")
return errors.New("key should be in \"<token>:<service_name>\" format")
}

if _, err := time.ParseDuration(cfg.Interval); err != nil {
return fmt.Errorf("interval has to be a duration string. Valid time units are \"ns\", \"us\" (or \"µs\"), \"ms\", \"s\", \"m\", \"h\"")
return errors.New("interval has to be a duration string. Valid time units are \"ns\", \"us\" (or \"µs\"), \"ms\", \"s\", \"m\", \"h\"")
}
return nil
}
15 changes: 8 additions & 7 deletions extension/solarwindsapmsettingsextension/config_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package solarwindsapmsettingsextension

import (
"fmt"
"github.com/stretchr/testify/require"
"errors"
"testing"

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

func TestValidate(t *testing.T) {
Expand All @@ -15,38 +16,38 @@ func TestValidate(t *testing.T) {
{
name: "nothing",
cfg: &Config{},
err: fmt.Errorf("endpoint must not be empty"),
err: errors.New("endpoint must not be empty"),
},
{
name: "empty key",
cfg: &Config{
Endpoint: "host:12345",
},
err: fmt.Errorf("key must not be empty"),
err: errors.New("key must not be empty"),
},
{
name: "invalid endpoint",
cfg: &Config{
Endpoint: "invalid",
Key: "token:name",
},
err: fmt.Errorf("endpoint should be in \"<host>:<port>\" format"),
err: errors.New("endpoint should be in \"<host>:<port>\" format"),
},
{
name: "invalid endpoint format but port is not an integer",
cfg: &Config{
Endpoint: "host:abc",
Key: "token:name",
},
err: fmt.Errorf("the <port> portion of endpoint has to be an integer"),
err: errors.New("the <port> portion of endpoint has to be an integer"),
},
{
name: "invalid key",
cfg: &Config{
Endpoint: "host:12345",
Key: "invalid",
},
err: fmt.Errorf("key should be in \"<token>:<service_name>\" format"),
err: errors.New("key should be in \"<token>:<service_name>\" format"),
},
}
for _, tc := range tests {
Expand Down
10 changes: 6 additions & 4 deletions extension/solarwindsapmsettingsextension/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"crypto/tls"
"encoding/binary"
"encoding/json"
"fmt"
"errors"
"github.com/gogo/protobuf/proto"
"github.com/solarwindscloud/apm-proto/go/collectorpb"
"go.opentelemetry.io/collector/component"
Expand Down Expand Up @@ -184,7 +184,7 @@ func (extension *solarwindsapmSettingsExtension) Start(ctx context.Context, _ co
var err error
extension.conn, err = grpc.Dial(extension.config.Endpoint, grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{})))
if err != nil {
return fmt.Errorf("Failed to dial: " + err.Error())
return errors.New("Failed to dial: " + err.Error())
} else {
extension.logger.Info("Dailed to " + extension.config.Endpoint)
}
Expand Down Expand Up @@ -217,7 +217,9 @@ func (extension *solarwindsapmSettingsExtension) Start(ctx context.Context, _ co

func (extension *solarwindsapmSettingsExtension) Shutdown(_ context.Context) error {
extension.logger.Debug("Shutting down solarwinds apm settings extension")
extension.conn.Close()
extension.cancel()
err := extension.conn.Close()
if err != nil {
return errors.New("Failed to close the gRPC connection to solarwinds APM collector " + err.Error())
}
return nil
}
6 changes: 4 additions & 2 deletions extension/solarwindsapmsettingsextension/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package solarwindsapmsettingsextension

import (
"context"
"github.com/solarwinds/opentelemetry-collector-contrib/extension/solarwindsapmsettingsextension/internal/metadata"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/extension"

"github.com/solarwinds/opentelemetry-collector-contrib/extension/solarwindsapmsettingsextension/internal/metadata"
)

const (
DefaultInterval = "1m"
DefaultInterval = "10s"
)

func createDefaultConfig() component.Config {
Expand Down
53 changes: 36 additions & 17 deletions extension/solarwindsapmsettingsextension/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,55 @@ go 1.20

require (
github.com/gogo/protobuf v1.3.2
github.com/solarwindscloud/apm-proto v0.0.0-20230912021952-7c5e94624446
github.com/solarwindscloud/apm-proto v0.0.0-20231107001908-432e697887b6
github.com/stretchr/testify v1.8.4
go.opentelemetry.io/collector/component v0.85.0
go.opentelemetry.io/collector/extension v0.85.0
go.opentelemetry.io/collector/component v0.92.1-0.20240117180253-4371e14440ee
go.opentelemetry.io/collector/extension v0.92.1-0.20240117180253-4371e14440ee
go.opentelemetry.io/otel/metric v1.22.0
go.opentelemetry.io/otel/trace v1.22.0
go.uber.org/zap v1.26.0
google.golang.org/grpc v1.58.1
google.golang.org/protobuf v1.31.0
google.golang.org/grpc v1.60.1
google.golang.org/protobuf v1.32.0
)

require (
contrib.go.opencensus.io/exporter/prometheus v0.4.2 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-kit/log v0.2.1 // indirect
github.com/go-logfmt/logfmt v0.5.1 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/hashicorp/go-version v1.6.0 // indirect
github.com/knadh/koanf/maps v0.1.1 // indirect
github.com/knadh/koanf/providers/confmap v0.1.0 // indirect
github.com/knadh/koanf/v2 v2.0.1 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/mapstructure v1.5.1-0.20220423185008-bf980b35cac4 // indirect
github.com/mitchellh/mapstructure v1.5.1-0.20231216201459-8508981c8b6c // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.opentelemetry.io/collector/config/configtelemetry v0.85.0 // indirect
go.opentelemetry.io/collector/confmap v0.85.0 // indirect
go.opentelemetry.io/collector/featuregate v1.0.0-rcv0014 // indirect
go.opentelemetry.io/collector/pdata v1.0.0-rcv0014 // indirect
go.opentelemetry.io/otel v1.18.0 // indirect
go.opentelemetry.io/otel/metric v1.18.0 // indirect
go.opentelemetry.io/otel/trace v1.18.0 // indirect
github.com/prometheus/client_golang v1.18.0 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.46.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/prometheus/statsd_exporter v0.22.7 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/collector/config/configtelemetry v0.92.0 // indirect
go.opentelemetry.io/collector/confmap v0.92.0 // indirect
go.opentelemetry.io/collector/featuregate v1.0.1 // indirect
go.opentelemetry.io/collector/pdata v1.0.1 // indirect
go.opentelemetry.io/otel v1.22.0 // indirect
go.opentelemetry.io/otel/exporters/prometheus v0.44.1-0.20231201153405-6027c1ae76f2 // indirect
go.opentelemetry.io/otel/sdk v1.21.0 // indirect
go.opentelemetry.io/otel/sdk/metric v1.21.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/net v0.15.0 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/text v0.13.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230913181813-007df8e322eb // indirect
golang.org/x/net v0.20.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240116215550-a9fa1716bcac // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit d3193cb

Please sign in to comment.