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.
Initial DogStatsD implementation (#15)
Initial metrics exporter through DogStatsD with support for all metric types but summary and distribution
- Loading branch information
Showing
12 changed files
with
774 additions
and
32 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,87 @@ | ||
// 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 datadogexporter | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/DataDog/datadog-go/statsd" | ||
"go.opentelemetry.io/collector/consumer/pdata" | ||
"go.opentelemetry.io/collector/exporter/exporterhelper" | ||
"go.uber.org/zap" | ||
) | ||
|
||
type dogStatsDExporter struct { | ||
logger *zap.Logger | ||
cfg *Config | ||
client *statsd.Client | ||
} | ||
|
||
func newDogStatsDExporter(logger *zap.Logger, cfg *Config) (*dogStatsDExporter, error) { | ||
|
||
client, err := statsd.New( | ||
cfg.MetricsURL, | ||
statsd.WithNamespace("opentelemetry."), | ||
statsd.WithTags(cfg.Tags), | ||
) | ||
|
||
if err != nil { | ||
return nil, fmt.Errorf("Failed to initialize DogStatsD client: %s", err) | ||
} | ||
|
||
return &dogStatsDExporter{logger, cfg, client}, nil | ||
} | ||
|
||
func (exp *dogStatsDExporter) PushMetricsData(_ context.Context, md pdata.Metrics) (int, error) { | ||
metrics, droppedTimeSeries, err := MapMetrics(exp, md) | ||
|
||
if err != nil { | ||
return droppedTimeSeries, err | ||
} | ||
|
||
for name, data := range metrics { | ||
for _, metric := range data { | ||
switch metric.GetType() { | ||
case Count: | ||
err = exp.client.Count(name, metric.GetValue().(int64), metric.GetTags(), metric.GetRate()) | ||
case Gauge: | ||
err = exp.client.Gauge(name, metric.GetValue().(float64), metric.GetTags(), metric.GetRate()) | ||
} | ||
|
||
if err != nil { | ||
return droppedTimeSeries, err | ||
} | ||
} | ||
} | ||
|
||
return droppedTimeSeries, nil | ||
} | ||
|
||
func (exp *dogStatsDExporter) GetLogger() *zap.Logger { | ||
return exp.logger | ||
} | ||
|
||
func (exp *dogStatsDExporter) GetConfig() *Config { | ||
return exp.cfg | ||
} | ||
|
||
func (exp *dogStatsDExporter) GetQueueSettings() exporterhelper.QueueSettings { | ||
return exporterhelper.CreateDefaultQueueSettings() | ||
} | ||
|
||
func (exp *dogStatsDExporter) GetRetrySettings() exporterhelper.RetrySettings { | ||
return exporterhelper.CreateDefaultRetrySettings() | ||
} |
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,116 @@ | ||
// 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 datadogexporter | ||
|
||
import ( | ||
"context" | ||
"path" | ||
"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/configcheck" | ||
"go.opentelemetry.io/collector/config/configmodels" | ||
"go.opentelemetry.io/collector/config/configtest" | ||
"go.uber.org/zap" | ||
) | ||
|
||
// Test that the factory creates the default configuration | ||
func TestCreateDefaultConfig(t *testing.T) { | ||
factory := NewFactory() | ||
cfg := factory.CreateDefaultConfig() | ||
|
||
assert.Equal(t, cfg, &Config{ | ||
Site: DefaultSite, | ||
Tags: DefaultTags, | ||
Mode: DefaultMode, | ||
}, "failed to create default config") | ||
|
||
assert.NoError(t, configcheck.ValidateConfig(cfg)) | ||
} | ||
|
||
func TestCreateAgentMetricsExporter(t *testing.T) { | ||
logger := zap.NewNop() | ||
|
||
factories, err := componenttest.ExampleComponents() | ||
assert.NoError(t, err) | ||
|
||
factory := NewFactory() | ||
factories.Exporters[configmodels.Type(typeStr)] = factory | ||
cfg, err := configtest.LoadConfigFile(t, path.Join(".", "testdata", "config.yaml"), factories) | ||
|
||
require.NoError(t, err) | ||
require.NotNil(t, cfg) | ||
|
||
ctx := context.Background() | ||
exp, err := factory.CreateMetricsExporter( | ||
ctx, | ||
component.ExporterCreateParams{Logger: logger}, | ||
cfg.Exporters["datadog"], | ||
) | ||
assert.Nil(t, err) | ||
assert.NotNil(t, exp) | ||
} | ||
|
||
func TestCreateAPIMetricsExporter(t *testing.T) { | ||
logger := zap.NewNop() | ||
|
||
factories, err := componenttest.ExampleComponents() | ||
assert.NoError(t, err) | ||
|
||
factory := NewFactory() | ||
factories.Exporters[configmodels.Type(typeStr)] = factory | ||
cfg, err := configtest.LoadConfigFile(t, path.Join(".", "testdata", "config.yaml"), factories) | ||
|
||
require.NoError(t, err) | ||
require.NotNil(t, cfg) | ||
|
||
ctx := context.Background() | ||
exp, err := factory.CreateMetricsExporter( | ||
ctx, | ||
component.ExporterCreateParams{Logger: logger}, | ||
cfg.Exporters["datadog/2"], | ||
) | ||
|
||
// Not implemented | ||
assert.NotNil(t, err) | ||
assert.Nil(t, exp) | ||
} | ||
|
||
func TestCreateAPITraceExporter(t *testing.T) { | ||
logger := zap.NewNop() | ||
|
||
factories, err := componenttest.ExampleComponents() | ||
assert.NoError(t, err) | ||
|
||
factory := NewFactory() | ||
factories.Exporters[configmodels.Type(typeStr)] = factory | ||
cfg, err := configtest.LoadConfigFile(t, path.Join(".", "testdata", "config.yaml"), factories) | ||
|
||
require.NoError(t, err) | ||
require.NotNil(t, cfg) | ||
|
||
ctx := context.Background() | ||
exp, err := factory.CreateTraceExporter( | ||
ctx, | ||
component.ExporterCreateParams{Logger: logger}, | ||
cfg.Exporters["datadog/2"], | ||
) | ||
|
||
// Not implemented | ||
assert.NotNil(t, err) | ||
assert.Nil(t, exp) | ||
} |
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
Oops, something went wrong.