|
| 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 | +} |
0 commit comments