Skip to content

Commit

Permalink
add gmp exporter
Browse files Browse the repository at this point in the history
  • Loading branch information
dashpole committed Apr 8, 2022
1 parent 692f43a commit 8ee2e5a
Show file tree
Hide file tree
Showing 9 changed files with 10,997 additions and 1 deletion.
61 changes: 61 additions & 0 deletions exporter/collector/googlemanagedprometheus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright 2022 Google LLC
//
// 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
//
// https://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 collector

import (
"go.opentelemetry.io/collector/model/pdata"
semconv "go.opentelemetry.io/collector/model/semconv/v1.8.0"
monitoredrespb "google.golang.org/genproto/googleapis/api/monitoredres"
)

func MapToPrometheusTarget(res pdata.Resource) *monitoredrespb.MonitoredResource {
attrs := res.Attributes()
// Prepend namespace if it exists to match what is specified in
// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/datamodel.md#resource-attributes-1
job := getStringOrEmpty(attrs, semconv.AttributeServiceName)
serviceNamespace := getStringOrEmpty(attrs, semconv.AttributeServiceNamespace)
if serviceNamespace != "" {
job = serviceNamespace + "/" + job
}
return &monitoredrespb.MonitoredResource{
Type: "prometheus_target",
Labels: map[string]string{
"location": getStringOrEmpty(attrs, semconv.AttributeCloudAvailabilityZone),
"cluster": getStringOrEmpty(attrs, semconv.AttributeK8SClusterName),
"namespace": getStringOrEmpty(attrs, semconv.AttributeK8SNamespaceName),
"job": job,
"instance": getStringOrEmpty(attrs, semconv.AttributeServiceInstanceID),
},
}
}

func GoogleManagedPrometheusMetricName(baseName string, metric pdata.Metric) string {
return baseName + gmpMetricSuffix(metric)
}

func gmpMetricSuffix(metric pdata.Metric) string {
switch metric.DataType() {
case pdata.MetricDataTypeSum:
return "/counter"
case pdata.MetricDataTypeGauge:
return "/gauge"
case pdata.MetricDataTypeSummary:
return "/summary"
case pdata.MetricDataTypeHistogram:
return "/histogram"
default:
return ""
}
}
49 changes: 49 additions & 0 deletions exporter/collector/googlemanagedprometheus/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2022 Google LLC
//
// 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
//
// https://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 googlemanagedprometheus

import (
"fmt"

"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/exporter/exporterhelper"

"github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/collector"
)

// Config defines configuration for Google Cloud Managed Service for Prometheus exporter.
type Config struct {
config.ExporterSettings `mapstructure:",squash"`
GMPConfig `mapstructure:",squash"`

// Timeout for all API calls. If not set, defaults to 12 seconds.
exporterhelper.TimeoutSettings `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct.
exporterhelper.QueueSettings `mapstructure:"sending_queue"`
exporterhelper.RetrySettings `mapstructure:"retry_on_failure"`
}

// GMPConfig is a subset of the collector config.
type GMPConfig struct {
ProjectID string `mapstructure:"project"`
UserAgent string `mapstructure:"user_agent"`
ClientConfig collector.ClientConfig `mapstructure:",squash"`
}

func (cfg *Config) Validate() error {
if err := cfg.ExporterSettings.Validate(); err != nil {
return fmt.Errorf("exporter settings are invalid :%w", err)
}
return nil
}
93 changes: 93 additions & 0 deletions exporter/collector/googlemanagedprometheus/factory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Copyright 2022 Google LLC
//
// 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
//
// https://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 googlemanagedprometheus

import (
"context"
"time"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/exporter/exporterhelper"

"github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/collector"
)

const (
// The value of "type" key in configuration.
typeStr = "googlemanagedprometheus"
defaultTimeout = 12 * time.Second // Consistent with Cloud Monitoring's timeout
)

// NewFactory creates a factory for the googlemanagedprometheus exporter
func NewFactory() component.ExporterFactory {
return component.NewExporterFactory(
typeStr,
createDefaultConfig,
component.WithMetricsExporter(createMetricsExporter),
)
}

// createDefaultConfig creates the default configuration for exporter.
func createDefaultConfig() config.Exporter {
return &Config{
ExporterSettings: config.NewExporterSettings(config.NewComponentID(typeStr)),
TimeoutSettings: exporterhelper.TimeoutSettings{Timeout: defaultTimeout},
RetrySettings: exporterhelper.NewDefaultRetrySettings(),
QueueSettings: exporterhelper.NewDefaultQueueSettings(),
GMPConfig: GMPConfig{
UserAgent: "opentelemetry-collector-contrib {{version}}",
},
}
}

// createMetricsExporter creates a metrics exporter based on this config.
func createMetricsExporter(
ctx context.Context,
params component.ExporterCreateSettings,
cfg config.Exporter) (component.MetricsExporter, error) {
eCfg := cfg.(*Config)
mExp, err := collector.NewGoogleCloudMetricsExporter(ctx, eCfg.GMPConfig.toCollectorConfig(), params.TelemetrySettings.Logger, params.BuildInfo.Version, eCfg.Timeout)
if err != nil {
return nil, err
}
return exporterhelper.NewMetricsExporter(
cfg,
params,
mExp.PushMetrics,
exporterhelper.WithShutdown(mExp.Shutdown),
// Disable exporterhelper Timeout, since we are using a custom mechanism
// within exporter itself
exporterhelper.WithTimeout(exporterhelper.TimeoutSettings{Timeout: 0}),
exporterhelper.WithQueue(eCfg.QueueSettings),
exporterhelper.WithRetry(eCfg.RetrySettings))
}

func (c *GMPConfig) toCollectorConfig() collector.Config {
// start with whatever the default collector config is.
cfg := collector.DefaultConfig()
// hard-code some config options to make it work with GMP
cfg.MetricConfig.Prefix = "prometheus.googleapis.com"
cfg.MetricConfig.SkipCreateMetricDescriptor = true
cfg.MetricConfig.InstrumentationLibraryLabels = false
cfg.MetricConfig.ServiceResourceLabels = false
cfg.MetricConfig.GetMetricName = collector.GoogleManagedPrometheusMetricName
cfg.MapMonitoredResource = collector.MapToPrometheusTarget
// map the GMP config's fields to the collector config
cfg.ProjectID = c.ProjectID
cfg.UserAgent = c.UserAgent
cfg.MetricConfig.ClientConfig = c.ClientConfig
return cfg
}
51 changes: 51 additions & 0 deletions exporter/collector/googlemanagedprometheus/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
module github.com/dashpole/opentelemetry-operations-go/exporter/collector/googlemanagedprometheus

go 1.17

require (
github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/collector v0.26.0
go.opentelemetry.io/collector v0.48.0
)

require (
cloud.google.com/go/compute v1.5.0 // indirect
cloud.google.com/go/monitoring v1.4.0 // indirect
cloud.google.com/go/trace v1.2.0 // indirect
github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/trace v1.3.0 // indirect
github.com/cenkalti/backoff/v4 v4.1.2 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.7 // indirect
github.com/googleapis/gax-go/v2 v2.2.0 // indirect
github.com/knadh/koanf v1.4.0 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/mapstructure v1.4.3 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/spf13/cast v1.4.1 // indirect
go.opencensus.io v0.23.0 // indirect
go.opentelemetry.io/collector/model v0.48.0 // indirect
go.opentelemetry.io/otel v1.6.1 // indirect
go.opentelemetry.io/otel/metric v0.28.0 // indirect
go.opentelemetry.io/otel/sdk v1.6.1 // indirect
go.opentelemetry.io/otel/trace v1.6.1 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
go.uber.org/zap v1.21.0 // indirect
golang.org/x/net v0.0.0-20220325170049-de3da57026de // indirect
golang.org/x/oauth2 v0.0.0-20220309155454-6242fa91716a // indirect
golang.org/x/sys v0.0.0-20220328115105-d36c6a25d886 // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/api v0.74.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20220405205423-9d709892a2bf // indirect
google.golang.org/grpc v1.45.0 // indirect
google.golang.org/protobuf v1.28.0 // indirect
)

replace (
github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/collector => ../../collector
github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/trace => ../../trace
)
Loading

0 comments on commit 8ee2e5a

Please sign in to comment.