-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP - Add remote sampling extension #432
Closed
annanay25
wants to merge
1
commit into
open-telemetry:master
from
annanay25:remote-sampling-extension
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright 2019, 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 remotesamplingextension | ||
|
||
import ( | ||
"github.com/open-telemetry/opentelemetry-collector/config/configmodels" | ||
) | ||
|
||
// Config has the configuration settings for the remote sampling extension, | ||
// used to fetch sampling configuration from the upstream Jaeger collector instance. | ||
// | ||
// It is an extension of configmodels.ExtensionSettings | ||
type Config struct { | ||
configmodels.ExtensionSettings `mapstructure:",squash"` | ||
|
||
// Port is the port used to publish the health check status. | ||
// Default is `5778` (https://www.jaegertracing.io/docs/1.15/deployment/#agent). | ||
Port uint16 `mapstructure:"port"` | ||
|
||
// Addr is the upstream Jaeger collector address that can be used to fetch | ||
// sampling configurations. The default value is `:14250`. | ||
Addr string `mapstructure:"addr"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright 2019, 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 remotesamplingextension | ||
|
||
import ( | ||
"path" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector/config" | ||
"github.com/open-telemetry/opentelemetry-collector/config/configmodels" | ||
) | ||
|
||
func TestLoadConfig(t *testing.T) { | ||
factories, err := config.ExampleComponents() | ||
assert.Nil(t, err) | ||
|
||
factory := &Factory{} | ||
factories.Extensions[typeStr] = factory | ||
cfg, err := config.LoadConfigFile(t, path.Join(".", "testdata", "config.yaml"), factories) | ||
|
||
require.Nil(t, err) | ||
require.NotNil(t, cfg) | ||
|
||
ext0 := cfg.Extensions["remotesampling"] | ||
assert.Equal(t, factory.CreateDefaultConfig(), ext0) | ||
|
||
ext1 := cfg.Extensions["remotesampling/1"] | ||
assert.Equal(t, | ||
&Config{ | ||
ExtensionSettings: configmodels.ExtensionSettings{ | ||
TypeVal: "remotesampling", | ||
NameVal: "remotesampling/1", | ||
}, | ||
Port: 5779, | ||
Addr: "0.0.0.0:14251", | ||
}, | ||
ext1) | ||
|
||
assert.Equal(t, 1, len(cfg.Service.Extensions)) | ||
assert.Equal(t, "remotesampling/1", cfg.Service.Extensions[0]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright 2019, 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 remotesamplingextension implements an extension that serves as a proxy | ||
// and routes client requests for sampling config to the Jaeger collector. | ||
package remotesamplingextension |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Copyright 2019, 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 remotesamplingextension | ||
|
||
import ( | ||
"errors" | ||
"sync/atomic" | ||
|
||
"go.uber.org/zap" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector/config/configmodels" | ||
"github.com/open-telemetry/opentelemetry-collector/extension" | ||
) | ||
|
||
const ( | ||
// The value of extension "type" in configuration. | ||
typeStr = "remotesampling" | ||
) | ||
|
||
// Factory is the factory for the extension. | ||
type Factory struct { | ||
} | ||
|
||
var _ (extension.Factory) = (*Factory)(nil) | ||
|
||
// Type gets the type of the config created by this factory. | ||
func (f *Factory) Type() string { | ||
return typeStr | ||
} | ||
|
||
// CreateDefaultConfig creates the default configuration for the extension. | ||
func (f *Factory) CreateDefaultConfig() configmodels.Extension { | ||
return &Config{ | ||
ExtensionSettings: configmodels.ExtensionSettings{ | ||
TypeVal: typeStr, | ||
NameVal: typeStr, | ||
}, | ||
Port: 5778, | ||
Addr: "0.0.0.0:14250", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a meaningful default for this field? Maybe it should just default to empty string? |
||
} | ||
} | ||
|
||
// CreateExtension creates the extension based on this config. | ||
func (f *Factory) CreateExtension( | ||
logger *zap.Logger, | ||
cfg configmodels.Extension, | ||
) (extension.ServiceExtension, error) { | ||
config := cfg.(*Config) | ||
if config.Addr == "" { | ||
return nil, errors.New("\"Addr\" is required when using the \"remotesampling\" extension") | ||
} | ||
if config.Port == 0 { | ||
return nil, errors.New("\"Port\" is required when using the \"remotesampling\" extension") | ||
} | ||
|
||
// The runtime settings are global to the application, so while in principle it | ||
// is possible to have more than one instance, running multiple will mean that | ||
// the settings of the last started instance will prevail. In order to avoid | ||
// this issue we will allow the creation of a single instance once per process | ||
// while keeping the private function that allow the creation of multiple | ||
// instances for unit tests. Summary: only a single instance can be created | ||
// via the factory. | ||
if !atomic.CompareAndSwapInt32(&instanceState, instanceNotCreated, instanceCreated) { | ||
return nil, errors.New("only a single instance can be created per process") | ||
} | ||
|
||
return newServer(*config, logger) | ||
} | ||
|
||
// See comment in CreateExtension how these are used. | ||
var instanceState int32 | ||
|
||
const ( | ||
instanceNotCreated int32 = 0 | ||
instanceCreated int32 = 1 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Copyright 2019, 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 remotesamplingextension | ||
|
||
import ( | ||
"sync/atomic" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"go.uber.org/zap" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector/config/configcheck" | ||
"github.com/open-telemetry/opentelemetry-collector/config/configmodels" | ||
) | ||
|
||
func TestFactory_Type(t *testing.T) { | ||
factory := Factory{} | ||
require.Equal(t, typeStr, factory.Type()) | ||
} | ||
|
||
func TestFactory_CreateDefaultConfig(t *testing.T) { | ||
factory := Factory{} | ||
cfg := factory.CreateDefaultConfig() | ||
assert.Equal(t, &Config{ | ||
ExtensionSettings: configmodels.ExtensionSettings{ | ||
NameVal: typeStr, | ||
TypeVal: typeStr, | ||
}, | ||
Port: 5778, | ||
Addr: "0.0.0.0:14250", | ||
}, | ||
cfg) | ||
|
||
assert.NoError(t, configcheck.ValidateConfig(cfg)) | ||
ext, err := factory.CreateExtension(zap.NewNop(), cfg) | ||
require.NoError(t, err) | ||
require.NotNil(t, ext) | ||
|
||
// Restore instance tracking from factory, for other tests. | ||
atomic.StoreInt32(&instanceState, instanceNotCreated) | ||
} | ||
|
||
func TestFactory_CreateExtension(t *testing.T) { | ||
factory := Factory{} | ||
cfg := factory.CreateDefaultConfig().(*Config) | ||
|
||
ext, err := factory.CreateExtension(zap.NewNop(), cfg) | ||
require.NoError(t, err) | ||
require.NotNil(t, ext) | ||
|
||
// Restore instance tracking from factory, for other tests. | ||
atomic.StoreInt32(&instanceState, instanceNotCreated) | ||
} | ||
|
||
func TestFactory_CreateExtensionOnlyOnce(t *testing.T) { | ||
factory := Factory{} | ||
cfg := factory.CreateDefaultConfig().(*Config) | ||
|
||
logger := zap.NewNop() | ||
ext, err := factory.CreateExtension(logger, cfg) | ||
require.NoError(t, err) | ||
require.NotNil(t, ext) | ||
|
||
ext1, err := factory.CreateExtension(logger, cfg) | ||
require.Error(t, err) | ||
require.Nil(t, ext1) | ||
|
||
// Restore instance tracking from factory, for other tests. | ||
atomic.StoreInt32(&instanceState, instanceNotCreated) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this field be more descriptive to help the operator know what it's for. CollectorAddress?