forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add resourcedetection processor * Add env and gce resource detectors * Resource Detection Processor: increase test coverage & fix README * Cache detected resource against processor name to avoid running resource detection code more than once * Refactor resources detection processor to use resource provider
- Loading branch information
1 parent
6b5c200
commit d81294a
Showing
23 changed files
with
3,277 additions
and
0 deletions.
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 @@ | ||
include ../../Makefile.Common |
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,29 @@ | ||
# Resource Detection Processor | ||
|
||
Supported pipeline types: metrics, traces | ||
|
||
The resource detection processor can be used to detect resource information from the host, | ||
in a format that conforms to the OpenTelemetry resource semantic conventions, and append or | ||
override the resource value in traces and metrics with this information. | ||
|
||
Currently supported detectors include: | ||
|
||
* Environment Variable: Reads resource information from the `OTEL_RESOURCE` environment | ||
variable. This is expected to be in the format `<key1>=<value1>,<key2>=<value2>,...`, the | ||
details of which are currently pending confirmation in the OpenTelemetry specification. | ||
|
||
* GCE Metadata: Uses the [Google Cloud Client Libraries for Go](https://github.com/googleapis/google-cloud-go) | ||
to read resource information from the GCE metadata server as documented by | ||
https://cloud.google.com/compute/docs/storing-retrieving-metadata | ||
|
||
## Configuration | ||
|
||
```yaml | ||
# a list of resource detectors to run, valid options are: "env", "gce" | ||
detectors: [ <string> ] | ||
# determines if existing resource attributes should be overridden or preserved, defaults to true | ||
override: <bool> | ||
``` | ||
The full list of settings exposed for this extension are documented [here](./config.go) | ||
with detailed sample configurations [here](./testdata/config.yaml). |
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 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 resourcedetectionprocessor | ||
|
||
import ( | ||
"time" | ||
|
||
"go.opentelemetry.io/collector/config/configmodels" | ||
) | ||
|
||
// Config defines configuration for Resource processor. | ||
type Config struct { | ||
configmodels.ProcessorSettings `mapstructure:",squash"` | ||
// Detectors is an ordered list of named detectors that should be | ||
// run to attempt to detect resource information. | ||
Detectors []string `mapstructure:"detectors"` | ||
// Timeout specifies the maximum amount of time that we will wait | ||
// before assuming a detector has failed. Defaults to 5s. | ||
Timeout time.Duration `mapstructure:"timeout"` | ||
// Override indicates whether any existing resource attributes | ||
// should be overridden or preserved. Defaults to true. | ||
Override bool `mapstructure:"override"` | ||
} |
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,51 @@ | ||
// 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 resourcedetectionprocessor | ||
|
||
import ( | ||
"path" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"go.opentelemetry.io/collector/config" | ||
"go.opentelemetry.io/collector/config/configmodels" | ||
) | ||
|
||
func TestLoadConfig(t *testing.T) { | ||
factories, err := config.ExampleComponents() | ||
assert.NoError(t, err) | ||
|
||
factory := &Factory{} | ||
factories.Processors[typeStr] = &Factory{} | ||
|
||
cfg, err := config.LoadConfigFile(t, path.Join(".", "testdata", "config.yaml"), factories) | ||
assert.NoError(t, err) | ||
assert.NotNil(t, cfg) | ||
|
||
p1 := cfg.Processors["resourcedetection"] | ||
assert.Equal(t, p1, factory.CreateDefaultConfig()) | ||
|
||
p2 := cfg.Processors["resourcedetection/2"] | ||
assert.Equal(t, p2, &Config{ | ||
ProcessorSettings: configmodels.ProcessorSettings{ | ||
TypeVal: "resourcedetection", | ||
NameVal: "resourcedetection/2", | ||
}, | ||
Detectors: []string{"env", "gce"}, | ||
Timeout: 2 * time.Second, | ||
Override: false, | ||
}) | ||
} |
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 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 resourcedetectionprocessor implements a processor for specifying resource | ||
// labels to be added to OpenCensus trace data and metrics data. | ||
package resourcedetectionprocessor |
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,139 @@ | ||
// 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 resourcedetectionprocessor | ||
|
||
import ( | ||
"context" | ||
"strings" | ||
"sync" | ||
"time" | ||
|
||
"go.opentelemetry.io/collector/component" | ||
"go.opentelemetry.io/collector/config/configmodels" | ||
"go.opentelemetry.io/collector/consumer" | ||
"go.uber.org/zap" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal" | ||
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal/env" | ||
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor/internal/gcp/gce" | ||
) | ||
|
||
const ( | ||
// The value of "type" key in configuration. | ||
typeStr = "resourcedetection" | ||
) | ||
|
||
// Factory is the factory for resourcedetection processor. | ||
type Factory struct { | ||
resourceProviderFactory *internal.ResourceProviderFactory | ||
|
||
// providers stores a provider for each named processor that | ||
// may a different set of detectors configured. | ||
providers map[string]*internal.ResourceProvider | ||
lock sync.Mutex | ||
} | ||
|
||
// NewFactory creates a new factory for resourcedetection processor. | ||
func NewFactory() *Factory { | ||
resourceProviderFactory := internal.NewProviderFactory(map[internal.DetectorType]internal.Detector{ | ||
env.TypeStr: &env.Detector{}, | ||
gce.TypeStr: gce.NewDetector(), | ||
}) | ||
|
||
return &Factory{ | ||
resourceProviderFactory: resourceProviderFactory, | ||
providers: map[string]*internal.ResourceProvider{}, | ||
} | ||
} | ||
|
||
// Type gets the type of the Option config created by this factory. | ||
func (*Factory) Type() configmodels.Type { | ||
return typeStr | ||
} | ||
|
||
// CreateDefaultConfig creates the default configuration for processor. | ||
func (*Factory) CreateDefaultConfig() configmodels.Processor { | ||
return &Config{ | ||
ProcessorSettings: configmodels.ProcessorSettings{ | ||
TypeVal: typeStr, | ||
NameVal: typeStr, | ||
}, | ||
Detectors: []string{env.TypeStr}, | ||
Timeout: 5 * time.Second, | ||
Override: true, | ||
} | ||
} | ||
|
||
// CreateTraceProcessor creates a trace processor based on this config. | ||
func (f *Factory) CreateTraceProcessor( | ||
ctx context.Context, | ||
params component.ProcessorCreateParams, | ||
nextConsumer consumer.TraceConsumer, | ||
cfg configmodels.Processor, | ||
) (component.TraceProcessor, error) { | ||
oCfg := cfg.(*Config) | ||
|
||
provider, err := f.getResourceProvider(ctx, params.Logger, cfg.Name(), oCfg.Timeout, oCfg.Detectors) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return newResourceTraceProcessor(ctx, nextConsumer, provider, oCfg.Override), nil | ||
} | ||
|
||
// CreateMetricsProcessor creates a metrics processor based on this config. | ||
func (f *Factory) CreateMetricsProcessor( | ||
ctx context.Context, | ||
params component.ProcessorCreateParams, | ||
nextConsumer consumer.MetricsConsumer, | ||
cfg configmodels.Processor, | ||
) (component.MetricsProcessor, error) { | ||
oCfg := cfg.(*Config) | ||
|
||
provider, err := f.getResourceProvider(ctx, params.Logger, cfg.Name(), oCfg.Timeout, oCfg.Detectors) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return newResourceMetricProcessor(ctx, nextConsumer, provider, oCfg.Override), nil | ||
} | ||
|
||
func (f *Factory) getResourceProvider( | ||
ctx context.Context, | ||
logger *zap.Logger, | ||
processorName string, | ||
timeout time.Duration, | ||
configuredDetectors []string, | ||
) (*internal.ResourceProvider, error) { | ||
f.lock.Lock() | ||
defer f.lock.Unlock() | ||
|
||
if provider, ok := f.providers[processorName]; ok { | ||
return provider, nil | ||
} | ||
|
||
detectorTypes := make([]internal.DetectorType, 0, len(configuredDetectors)) | ||
for _, key := range configuredDetectors { | ||
detectorTypes = append(detectorTypes, internal.DetectorType(strings.TrimSpace(key))) | ||
} | ||
|
||
provider, err := f.resourceProviderFactory.CreateResourceProvider(logger, timeout, detectorTypes...) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
f.providers[processorName] = provider | ||
return provider, nil | ||
} |
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,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 resourcedetectionprocessor | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"go.opentelemetry.io/collector/component" | ||
"go.opentelemetry.io/collector/config/configcheck" | ||
) | ||
|
||
func TestCreateDefaultConfig(t *testing.T) { | ||
var factory Factory | ||
cfg := factory.CreateDefaultConfig() | ||
assert.NoError(t, configcheck.ValidateConfig(cfg)) | ||
assert.NotNil(t, cfg) | ||
} | ||
|
||
func TestCreateProcessor(t *testing.T) { | ||
factory := NewFactory() | ||
cfg := factory.CreateDefaultConfig() | ||
|
||
tp, err := factory.CreateTraceProcessor(context.Background(), component.ProcessorCreateParams{}, nil, cfg) | ||
assert.NoError(t, err) | ||
assert.NotNil(t, tp) | ||
|
||
mp, err := factory.CreateMetricsProcessor(context.Background(), component.ProcessorCreateParams{}, nil, cfg) | ||
assert.NoError(t, err) | ||
assert.NotNil(t, mp) | ||
} |
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,13 @@ | ||
module github.com/open-telemetry/opentelemetry-collector-contrib/processor/resourcedetectionprocessor | ||
|
||
go 1.14 | ||
|
||
require ( | ||
cloud.google.com/go v0.45.1 | ||
github.com/census-instrumentation/opencensus-proto v0.2.1 | ||
github.com/opentracing/opentracing-go v1.1.1-0.20190913142402-a7454ce5950e // indirect | ||
github.com/stretchr/testify v1.5.1 | ||
go.opentelemetry.io/collector v0.4.1-0.20200622191610-a8db6271f90a | ||
go.uber.org/atomic v1.5.1 | ||
go.uber.org/zap v1.13.0 | ||
) |
Oops, something went wrong.