-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Add Stackdriver Exporter and configs. #7
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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 hidden or 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,26 @@ | ||
| // 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 stackdriverexporter | ||
|
|
||
| import "github.com/open-telemetry/opentelemetry-service/config/configmodels" | ||
|
|
||
| // Config defines configuration for Stackdriver exporter. | ||
| type Config struct { | ||
| configmodels.ExporterSettings `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct. | ||
| ProjectID string `mapstructure:"project"` | ||
| EnableTracing bool `mapstructure:"enable-tracing"` | ||
| EnableMetrics bool `mapstructure:"enable-metrics"` | ||
| Prefix string `mapstructure:"metric-prefix"` | ||
| } |
This file contains hidden or 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,52 @@ | ||
| // 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 stackdriverexporter | ||
|
|
||
| import ( | ||
| "path" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/open-telemetry/opentelemetry-service/config" | ||
| "github.com/open-telemetry/opentelemetry-service/config/configmodels" | ||
| "github.com/open-telemetry/opentelemetry-service/exporter" | ||
| ) | ||
|
|
||
| var _ = config.RegisterTestFactories() | ||
|
|
||
| func TestLoadConfig(t *testing.T) { | ||
|
|
||
| factory := exporter.GetFactory(typeStr) | ||
|
|
||
| cfg, err := config.LoadConfigFile(t, path.Join(".", "testdata", "config.yaml")) | ||
|
|
||
| require.NoError(t, err) | ||
| require.NotNil(t, cfg) | ||
|
|
||
| assert.Equal(t, len(cfg.Exporters), 2) | ||
|
|
||
| r0 := cfg.Exporters["stackdriver"] | ||
| assert.Equal(t, r0, factory.CreateDefaultConfig()) | ||
|
|
||
| r1 := cfg.Exporters["stackdriver/customname"].(*Config) | ||
| assert.Equal(t, r1.ExporterSettings, | ||
| configmodels.ExporterSettings{ | ||
| TypeVal: typeStr, | ||
| NameVal: "stackdriver/customname", | ||
| Enabled: true, | ||
| }) | ||
| } | ||
This file contains hidden or 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,66 @@ | ||
| // 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 stackdriverexporter | ||
|
|
||
| import ( | ||
| "github.com/open-telemetry/opentelemetry-service/config/configmodels" | ||
songy23 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "github.com/open-telemetry/opentelemetry-service/consumer" | ||
| "github.com/open-telemetry/opentelemetry-service/exporter" | ||
| "go.uber.org/zap" | ||
| ) | ||
|
|
||
| var _ = exporter.RegisterFactory(&exporterFactory{}) | ||
songy23 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| const ( | ||
| // The value of "type" key in configuration. | ||
| typeStr = "stackdriver" | ||
| ) | ||
|
|
||
| // exporterFactory is the factory for OpenCensus exporter. | ||
| type exporterFactory struct { | ||
| } | ||
|
|
||
| // Type gets the type of the Exporter config created by this factory. | ||
| func (f *exporterFactory) Type() string { | ||
| return typeStr | ||
| } | ||
|
|
||
| // CreateDefaultConfig creates the default configuration for exporter. | ||
| func (f *exporterFactory) CreateDefaultConfig() configmodels.Exporter { | ||
| return &Config{ | ||
| ExporterSettings: configmodels.ExporterSettings{ | ||
| TypeVal: typeStr, | ||
| NameVal: typeStr, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| // CreateTraceExporter creates a trace exporter based on this config. | ||
| func (f *exporterFactory) CreateTraceExporter(logger *zap.Logger, cfg configmodels.Exporter) (consumer.TraceConsumer, exporter.StopFunc, error) { | ||
| eCfg := cfg.(*Config) | ||
| if !eCfg.EnableTracing { | ||
| return nil, nil, nil | ||
| } | ||
| return newStackdriverTraceExporter(eCfg.ProjectID, eCfg.Prefix) | ||
| } | ||
|
|
||
| // CreateMetricsExporter creates a metrics exporter based on this config. | ||
| func (f *exporterFactory) CreateMetricsExporter(logger *zap.Logger, cfg configmodels.Exporter) (consumer.MetricsConsumer, exporter.StopFunc, error) { | ||
| eCfg := cfg.(*Config) | ||
| if !eCfg.EnableMetrics { | ||
| return nil, nil, nil | ||
| } | ||
| return newStackdriverMetricsExporter(eCfg.ProjectID, eCfg.Prefix) | ||
| } | ||
This file contains hidden or 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,42 @@ | ||
| // 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 stackdriverexporter | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "go.uber.org/zap" | ||
|
|
||
songy23 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| "github.com/stretchr/testify/assert" | ||
|
|
||
| "github.com/open-telemetry/opentelemetry-service/exporter" | ||
| ) | ||
|
|
||
| func TestCreateDefaultConfig(t *testing.T) { | ||
| factory := exporter.GetFactory(typeStr) | ||
| cfg := factory.CreateDefaultConfig() | ||
| assert.NotNil(t, cfg, "failed to create default config") | ||
| } | ||
|
|
||
| func TestCreateExporter(t *testing.T) { | ||
| factory := exporter.GetFactory(typeStr) | ||
| cfg := factory.CreateDefaultConfig() | ||
|
|
||
| _, _, err := factory.CreateTraceExporter(zap.NewNop(), cfg) | ||
| assert.Nil(t, err) | ||
|
|
||
| _, _, err = factory.CreateMetricsExporter(zap.NewNop(), cfg) | ||
| assert.Nil(t, err) | ||
| } | ||
This file contains hidden or 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,121 @@ | ||
| // 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 stackdriverexporter contains the wrapper for OpenTelemetry-Stackdriver | ||
| // exporter to be used in OpenTelemetry-Service. | ||
| package stackdriverexporter | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "time" | ||
|
|
||
| "contrib.go.opencensus.io/exporter/stackdriver" | ||
| "go.opencensus.io/trace" | ||
|
|
||
| "github.com/open-telemetry/opentelemetry-service/consumer" | ||
| "github.com/open-telemetry/opentelemetry-service/consumer/consumerdata" | ||
| "github.com/open-telemetry/opentelemetry-service/exporter/exporterwrapper" | ||
| ) | ||
|
|
||
| type stackdriverConfig struct { | ||
| ProjectID string `mapstructure:"project,omitempty"` | ||
| EnableTracing bool `mapstructure:"enable-tracing,omitempty"` | ||
| EnableMetrics bool `mapstructure:"enable-metrics,omitempty"` | ||
| MetricPrefix string `mapstructure:"metric-prefix,omitempty"` | ||
| } | ||
|
|
||
| // TODO: Add metrics support to the exporterwrapper. | ||
| type stackdriverExporter struct { | ||
| exporter *stackdriver.Exporter | ||
| } | ||
|
|
||
| var _ consumer.MetricsConsumer = (*stackdriverExporter)(nil) | ||
|
|
||
| func newStackdriverTraceExporter(ProjectID, MetricPrefix string) (consumer.TraceConsumer, func() error, error) { | ||
| sde, serr := newStackdriverExporter(ProjectID, MetricPrefix) | ||
| if serr != nil { | ||
| return nil, nil, fmt.Errorf("cannot configure Stackdriver Trace exporter: %v", serr) | ||
| } | ||
|
|
||
| tExp, err := exporterwrapper.NewExporterWrapper("stackdriver_trace", "ocservice.exporter.Stackdriver.ConsumeTraceData", sde) | ||
| if err != nil { | ||
| return nil, nil, err | ||
| } | ||
| // TODO: Examine "contrib.go.opencensus.io/exporter/stackdriver" to see | ||
| // if trace.ExportSpan was constraining and if perhaps the Stackdriver | ||
| // upload can use the context and information from the Node. | ||
|
|
||
| doneFn := func() error { | ||
| sde.Flush() | ||
| return nil | ||
| } | ||
|
|
||
| return tExp, doneFn, nil | ||
| } | ||
|
|
||
| func newStackdriverMetricsExporter(ProjectID, MetricPrefix string) (consumer.MetricsConsumer, func() error, error) { | ||
| sde, serr := newStackdriverExporter(ProjectID, MetricPrefix) | ||
| if serr != nil { | ||
| return nil, nil, fmt.Errorf("cannot configure Stackdriver metric exporter: %v", serr) | ||
| } | ||
|
|
||
| mExp := &stackdriverExporter{ | ||
| exporter: sde, | ||
| } | ||
|
|
||
| doneFn := func() error { | ||
| sde.Flush() | ||
| return nil | ||
| } | ||
|
|
||
| return mExp, doneFn, nil | ||
| } | ||
|
|
||
| func newStackdriverExporter(ProjectID, MetricPrefix string) (*stackdriver.Exporter, error) { | ||
| // TODO: For each ProjectID, create a different exporter | ||
| // or at least a unique Stackdriver client per ProjectID. | ||
|
|
||
| return stackdriver.NewExporter(stackdriver.Options{ | ||
| // If the project ID is an empty string, it will be set by default based on | ||
| // the project this is running on in GCP. | ||
| ProjectID: ProjectID, | ||
|
|
||
| MetricPrefix: MetricPrefix, | ||
|
|
||
| // Stackdriver Metrics mandates a minimum of 60 seconds for | ||
| // reporting metrics. We have to enforce this as per the advisory | ||
| // at https://cloud.google.com/monitoring/custom-metrics/creating-metrics#writing-ts | ||
| // which says: | ||
| // | ||
| // "If you want to write more than one point to the same time series, then use a separate call | ||
| // to the timeSeries.create method for each point. Don't make the calls faster than one time per | ||
| // minute. If you are adding data points to different time series, then there is no rate limitation." | ||
| BundleDelayThreshold: 61 * time.Second, | ||
| }) | ||
| } | ||
|
|
||
| func (sde *stackdriverExporter) ConsumeMetricsData(ctx context.Context, md consumerdata.MetricsData) error { | ||
| ctx, span := trace.StartSpan(ctx, | ||
| "opentelemetry.service.exporter.stackdriver.ExportMetricsData", | ||
| trace.WithSampler(trace.NeverSample())) | ||
| defer span.End() | ||
|
|
||
| err := sde.exporter.ExportMetricsProto(ctx, md.Node, md.Resource, md.Metrics) | ||
| if err != nil { | ||
| span.SetStatus(trace.Status{Code: trace.StatusCodeInternal, Message: err.Error()}) | ||
| } | ||
|
|
||
| return err | ||
| } |
This file contains hidden or 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,21 @@ | ||
| receivers: | ||
| examplereceiver: | ||
|
|
||
| processors: | ||
| exampleprocessor: | ||
|
|
||
| exporters: | ||
| stackdriver: | ||
| stackdriver/customname: | ||
| enabled: true | ||
| project: my-project | ||
| enable-tracing: true | ||
| enable-metrics: true | ||
| metric-prefix: prefix | ||
|
|
||
| pipelines: | ||
| traces: | ||
| receivers: [examplereceiver] | ||
| processors: [exampleprocessor] | ||
| exporters: [stackdriver] | ||
|
|
This file contains hidden or 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,12 @@ | ||
| module github.com/open-telemetry/opentelemetry-service-contrib | ||
|
|
||
| go 1.12 | ||
|
|
||
| require ( | ||
| contrib.go.opencensus.io/exporter/stackdriver v0.12.2 | ||
| github.com/open-telemetry/opentelemetry-service v0.0.0-20190715221013-8721e3beaf1c | ||
| github.com/spf13/viper v1.4.0 | ||
| github.com/stretchr/testify v1.3.0 | ||
| go.opencensus.io v0.22.0 | ||
| go.uber.org/zap v1.10.0 | ||
| ) |
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.
Uh oh!
There was an error while loading. Please reload this page.