Skip to content

Commit 8f649f3

Browse files
committed
add gmp exporter
1 parent ddb7d98 commit 8f649f3

File tree

9 files changed

+10997
-1
lines changed

9 files changed

+10997
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package collector
16+
17+
import (
18+
"go.opentelemetry.io/collector/model/pdata"
19+
semconv "go.opentelemetry.io/collector/model/semconv/v1.8.0"
20+
monitoredrespb "google.golang.org/genproto/googleapis/api/monitoredres"
21+
)
22+
23+
func MapToPrometheusTarget(res pdata.Resource) *monitoredrespb.MonitoredResource {
24+
attrs := res.Attributes()
25+
// Prepend namespace if it exists to match what is specified in
26+
// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/datamodel.md#resource-attributes-1
27+
job := getStringOrEmpty(attrs, semconv.AttributeServiceName)
28+
serviceNamespace := getStringOrEmpty(attrs, semconv.AttributeServiceNamespace)
29+
if serviceNamespace != "" {
30+
job = serviceNamespace + "/" + job
31+
}
32+
return &monitoredrespb.MonitoredResource{
33+
Type: "prometheus_target",
34+
Labels: map[string]string{
35+
"location": getStringOrEmpty(attrs, semconv.AttributeCloudAvailabilityZone),
36+
"cluster": getStringOrEmpty(attrs, semconv.AttributeK8SClusterName),
37+
"namespace": getStringOrEmpty(attrs, semconv.AttributeK8SNamespaceName),
38+
"job": job,
39+
"instance": getStringOrEmpty(attrs, semconv.AttributeServiceInstanceID),
40+
},
41+
}
42+
}
43+
44+
func GoogleManagedPrometheusMetricName(baseName string, metric pdata.Metric) string {
45+
return baseName + gmpMetricSuffix(metric)
46+
}
47+
48+
func gmpMetricSuffix(metric pdata.Metric) string {
49+
switch metric.DataType() {
50+
case pdata.MetricDataTypeSum:
51+
return "/counter"
52+
case pdata.MetricDataTypeGauge:
53+
return "/gauge"
54+
case pdata.MetricDataTypeSummary:
55+
return "/summary"
56+
case pdata.MetricDataTypeHistogram:
57+
return "/histogram"
58+
default:
59+
return ""
60+
}
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package googlemanagedprometheus
16+
17+
import (
18+
"fmt"
19+
20+
"go.opentelemetry.io/collector/config"
21+
"go.opentelemetry.io/collector/exporter/exporterhelper"
22+
23+
"github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/collector"
24+
)
25+
26+
// Config defines configuration for Google Cloud Managed Service for Prometheus exporter.
27+
type Config struct {
28+
config.ExporterSettings `mapstructure:",squash"`
29+
GMPConfig `mapstructure:",squash"`
30+
31+
// Timeout for all API calls. If not set, defaults to 12 seconds.
32+
exporterhelper.TimeoutSettings `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct.
33+
exporterhelper.QueueSettings `mapstructure:"sending_queue"`
34+
exporterhelper.RetrySettings `mapstructure:"retry_on_failure"`
35+
}
36+
37+
// GMPConfig is a subset of the collector config.
38+
type GMPConfig struct {
39+
ProjectID string `mapstructure:"project"`
40+
UserAgent string `mapstructure:"user_agent"`
41+
ClientConfig collector.ClientConfig `mapstructure:",squash"`
42+
}
43+
44+
func (cfg *Config) Validate() error {
45+
if err := cfg.ExporterSettings.Validate(); err != nil {
46+
return fmt.Errorf("exporter settings are invalid :%w", err)
47+
}
48+
return nil
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package googlemanagedprometheus
16+
17+
import (
18+
"context"
19+
"time"
20+
21+
"go.opentelemetry.io/collector/component"
22+
"go.opentelemetry.io/collector/config"
23+
"go.opentelemetry.io/collector/exporter/exporterhelper"
24+
25+
"github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/collector"
26+
)
27+
28+
const (
29+
// The value of "type" key in configuration.
30+
typeStr = "googlemanagedprometheus"
31+
defaultTimeout = 12 * time.Second // Consistent with Cloud Monitoring's timeout
32+
)
33+
34+
// NewFactory creates a factory for the googlemanagedprometheus exporter
35+
func NewFactory() component.ExporterFactory {
36+
return component.NewExporterFactory(
37+
typeStr,
38+
createDefaultConfig,
39+
component.WithMetricsExporter(createMetricsExporter),
40+
)
41+
}
42+
43+
// createDefaultConfig creates the default configuration for exporter.
44+
func createDefaultConfig() config.Exporter {
45+
return &Config{
46+
ExporterSettings: config.NewExporterSettings(config.NewComponentID(typeStr)),
47+
TimeoutSettings: exporterhelper.TimeoutSettings{Timeout: defaultTimeout},
48+
RetrySettings: exporterhelper.NewDefaultRetrySettings(),
49+
QueueSettings: exporterhelper.NewDefaultQueueSettings(),
50+
GMPConfig: GMPConfig{
51+
UserAgent: "opentelemetry-collector-contrib {{version}}",
52+
},
53+
}
54+
}
55+
56+
// createMetricsExporter creates a metrics exporter based on this config.
57+
func createMetricsExporter(
58+
ctx context.Context,
59+
params component.ExporterCreateSettings,
60+
cfg config.Exporter) (component.MetricsExporter, error) {
61+
eCfg := cfg.(*Config)
62+
mExp, err := collector.NewGoogleCloudMetricsExporter(ctx, eCfg.GMPConfig.toCollectorConfig(), params.TelemetrySettings.Logger, params.BuildInfo.Version, eCfg.Timeout)
63+
if err != nil {
64+
return nil, err
65+
}
66+
return exporterhelper.NewMetricsExporter(
67+
cfg,
68+
params,
69+
mExp.PushMetrics,
70+
exporterhelper.WithShutdown(mExp.Shutdown),
71+
// Disable exporterhelper Timeout, since we are using a custom mechanism
72+
// within exporter itself
73+
exporterhelper.WithTimeout(exporterhelper.TimeoutSettings{Timeout: 0}),
74+
exporterhelper.WithQueue(eCfg.QueueSettings),
75+
exporterhelper.WithRetry(eCfg.RetrySettings))
76+
}
77+
78+
func (c *GMPConfig) toCollectorConfig() collector.Config {
79+
// start with whatever the default collector config is.
80+
cfg := collector.DefaultConfig()
81+
// hard-code some config options to make it work with GMP
82+
cfg.MetricConfig.Prefix = "prometheus.googleapis.com"
83+
cfg.MetricConfig.SkipCreateMetricDescriptor = true
84+
cfg.MetricConfig.InstrumentationLibraryLabels = false
85+
cfg.MetricConfig.ServiceResourceLabels = false
86+
cfg.MetricConfig.GetMetricName = collector.GoogleManagedPrometheusMetricName
87+
cfg.MapMonitoredResource = collector.MapToPrometheusTarget
88+
// map the GMP config's fields to the collector config
89+
cfg.ProjectID = c.ProjectID
90+
cfg.UserAgent = c.UserAgent
91+
cfg.MetricConfig.ClientConfig = c.ClientConfig
92+
return cfg
93+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
module github.com/dashpole/opentelemetry-operations-go/exporter/collector/googlemanagedprometheus
2+
3+
go 1.17
4+
5+
require (
6+
github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/collector v0.26.0
7+
go.opentelemetry.io/collector v0.48.0
8+
)
9+
10+
require (
11+
cloud.google.com/go/compute v1.5.0 // indirect
12+
cloud.google.com/go/monitoring v1.4.0 // indirect
13+
cloud.google.com/go/trace v1.2.0 // indirect
14+
github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/trace v1.3.0 // indirect
15+
github.com/cenkalti/backoff/v4 v4.1.2 // indirect
16+
github.com/go-logr/logr v1.2.3 // indirect
17+
github.com/go-logr/stdr v1.2.2 // indirect
18+
github.com/gogo/protobuf v1.3.2 // indirect
19+
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
20+
github.com/golang/protobuf v1.5.2 // indirect
21+
github.com/google/go-cmp v0.5.7 // indirect
22+
github.com/googleapis/gax-go/v2 v2.2.0 // indirect
23+
github.com/knadh/koanf v1.4.0 // indirect
24+
github.com/mitchellh/copystructure v1.2.0 // indirect
25+
github.com/mitchellh/mapstructure v1.4.3 // indirect
26+
github.com/mitchellh/reflectwalk v1.0.2 // indirect
27+
github.com/spf13/cast v1.4.1 // indirect
28+
go.opencensus.io v0.23.0 // indirect
29+
go.opentelemetry.io/collector/model v0.48.0 // indirect
30+
go.opentelemetry.io/otel v1.6.1 // indirect
31+
go.opentelemetry.io/otel/metric v0.28.0 // indirect
32+
go.opentelemetry.io/otel/sdk v1.6.1 // indirect
33+
go.opentelemetry.io/otel/trace v1.6.1 // indirect
34+
go.uber.org/atomic v1.9.0 // indirect
35+
go.uber.org/multierr v1.8.0 // indirect
36+
go.uber.org/zap v1.21.0 // indirect
37+
golang.org/x/net v0.0.0-20220325170049-de3da57026de // indirect
38+
golang.org/x/oauth2 v0.0.0-20220309155454-6242fa91716a // indirect
39+
golang.org/x/sys v0.0.0-20220328115105-d36c6a25d886 // indirect
40+
golang.org/x/text v0.3.7 // indirect
41+
google.golang.org/api v0.74.0 // indirect
42+
google.golang.org/appengine v1.6.7 // indirect
43+
google.golang.org/genproto v0.0.0-20220405205423-9d709892a2bf // indirect
44+
google.golang.org/grpc v1.45.0 // indirect
45+
google.golang.org/protobuf v1.28.0 // indirect
46+
)
47+
48+
replace (
49+
github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/collector => ../../collector
50+
github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/trace => ../../trace
51+
)

0 commit comments

Comments
 (0)