Skip to content

Commit

Permalink
[pkg/translator/prw] Export _created times series if possible. (#17417)
Browse files Browse the repository at this point in the history
* [pkg/translator/prw] Export _created times series if possible.

Relevant to Summaries, Histograms and Monotonic Sum metrics.
See #17412

* Make export of _created configurable.

* fixup! Make export of _created configurable.
  • Loading branch information
kovrus authored Jan 17, 2023
1 parent 1dcd91a commit 6e4baef
Show file tree
Hide file tree
Showing 11 changed files with 510 additions and 46 deletions.
17 changes: 17 additions & 0 deletions .chloggen/create_create-time-series-prw.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: bug_fix

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: prometheusremotewriteexporter

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Export `_created` metric for Summary, Histogram and Monotonic Sum metric points if `StartTimeUnixNano` is set.

# One or more tracking issues related to the change
issues: [17412, 12426]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:
The export of tbe `_created` metric is configurable in the Prometheus remote write exporter. It is disabled by default.
4 changes: 4 additions & 0 deletions exporter/prometheusremotewriteexporter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ The following settings can be optionally configured:
- `enabled` (default = false): If `enabled` is `true`, all the resource attributes will be converted to metric labels by default.
- `target_info`: customize `target_info` metric
- `enabled` (default = true): If `enabled` is `true`, a `target_info` metric will be generated for each resource metric (see https://github.com/open-telemetry/opentelemetry-specification/pull/2381).
- `export_created_metric`:
- `enabled` (default = false): If `enabled` is `true`, a `_created` metric is
exported for Summary, Histogram, and Monotonic Sum metric points if
`StartTimeUnixNano` is set.

Example:

Expand Down
13 changes: 13 additions & 0 deletions exporter/prometheusremotewriteexporter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ type Config struct {

// TargetInfo allows customizing the target_info metric
TargetInfo *TargetInfo `mapstructure:"target_info,omitempty"`

// CreatedMetric allows customizing creation of _created metrics
CreatedMetric *CreatedMetric `mapstructure:"export_created_metric,omitempty"`
}

type CreatedMetric struct {
// Enabled if true the _created metrics could be exported
Enabled bool `mapstructure:"enabled"`
}

type TargetInfo struct {
Expand Down Expand Up @@ -95,5 +103,10 @@ func (cfg *Config) Validate() error {
Enabled: true,
}
}
if cfg.CreatedMetric == nil {
cfg.CreatedMetric = &CreatedMetric{
Enabled: false,
}
}
return nil
}
1 change: 1 addition & 0 deletions exporter/prometheusremotewriteexporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func TestLoadConfig(t *testing.T) {
TargetInfo: &TargetInfo{
Enabled: true,
},
CreatedMetric: &CreatedMetric{Enabled: true},
},
},
{
Expand Down
49 changes: 25 additions & 24 deletions exporter/prometheusremotewriteexporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,17 @@ const maxBatchByteSize = 3000000

// prwExporter converts OTLP metrics to Prometheus remote write TimeSeries and sends them to a remote endpoint.
type prwExporter struct {
namespace string
externalLabels map[string]string
endpointURL *url.URL
client *http.Client
wg *sync.WaitGroup
closeChan chan struct{}
concurrency int
userAgentHeader string
clientSettings *confighttp.HTTPClientSettings
settings component.TelemetrySettings
disableTargetInfo bool

wal *prweWAL
endpointURL *url.URL
client *http.Client
wg *sync.WaitGroup
closeChan chan struct{}
concurrency int
userAgentHeader string
clientSettings *confighttp.HTTPClientSettings
settings component.TelemetrySettings

wal *prweWAL
exporterSettings prometheusremotewrite.Settings
}

// newPRWExporter initializes a new prwExporter instance and sets fields accordingly.
Expand All @@ -74,16 +72,19 @@ func newPRWExporter(cfg *Config, set exporter.CreateSettings) (*prwExporter, err
userAgentHeader := fmt.Sprintf("%s/%s", strings.ReplaceAll(strings.ToLower(set.BuildInfo.Description), " ", "-"), set.BuildInfo.Version)

prwe := &prwExporter{
namespace: cfg.Namespace,
externalLabels: sanitizedLabels,
endpointURL: endpointURL,
wg: new(sync.WaitGroup),
closeChan: make(chan struct{}),
userAgentHeader: userAgentHeader,
concurrency: cfg.RemoteWriteQueue.NumConsumers,
clientSettings: &cfg.HTTPClientSettings,
settings: set.TelemetrySettings,
disableTargetInfo: !cfg.TargetInfo.Enabled,
endpointURL: endpointURL,
wg: new(sync.WaitGroup),
closeChan: make(chan struct{}),
userAgentHeader: userAgentHeader,
concurrency: cfg.RemoteWriteQueue.NumConsumers,
clientSettings: &cfg.HTTPClientSettings,
settings: set.TelemetrySettings,
exporterSettings: prometheusremotewrite.Settings{
Namespace: cfg.Namespace,
ExternalLabels: sanitizedLabels,
DisableTargetInfo: !cfg.TargetInfo.Enabled,
ExportCreatedMetric: cfg.CreatedMetric.Enabled,
},
}
if cfg.WAL == nil {
return prwe, nil
Expand Down Expand Up @@ -136,7 +137,7 @@ func (prwe *prwExporter) PushMetrics(ctx context.Context, md pmetric.Metrics) er
case <-prwe.closeChan:
return errors.New("shutdown has been called")
default:
tsMap, err := prometheusremotewrite.FromMetrics(md, prometheusremotewrite.Settings{Namespace: prwe.namespace, ExternalLabels: prwe.externalLabels, DisableTargetInfo: prwe.disableTargetInfo})
tsMap, err := prometheusremotewrite.FromMetrics(md, prwe.exporterSettings)
if err != nil {
err = consumererror.NewPermanent(err)
}
Expand Down
17 changes: 14 additions & 3 deletions exporter/prometheusremotewriteexporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ func Test_NewPRWExporter(t *testing.T) {
TargetInfo: &TargetInfo{
Enabled: true,
},
CreatedMetric: &CreatedMetric{
Enabled: false,
},
}
buildInfo := component.BuildInfo{
Description: "OpenTelemetry Collector",
Expand Down Expand Up @@ -124,9 +127,8 @@ func Test_NewPRWExporter(t *testing.T) {
}
assert.NoError(t, err)
require.NotNil(t, prwe)
assert.NotNil(t, prwe.namespace)
assert.NotNil(t, prwe.exporterSettings)
assert.NotNil(t, prwe.endpointURL)
assert.NotNil(t, prwe.externalLabels)
assert.NotNil(t, prwe.closeChan)
assert.NotNil(t, prwe.wg)
assert.NotNil(t, prwe.userAgentHeader)
Expand All @@ -145,6 +147,9 @@ func Test_Start(t *testing.T) {
TargetInfo: &TargetInfo{
Enabled: true,
},
CreatedMetric: &CreatedMetric{
Enabled: false,
},
}
buildInfo := component.BuildInfo{
Description: "OpenTelemetry Collector",
Expand Down Expand Up @@ -472,7 +477,7 @@ func Test_PushMetrics(t *testing.T) {
name: "intSum_case",
metrics: intSumBatch,
reqTestFunc: checkFunc,
expectedTimeSeries: 3,
expectedTimeSeries: 5,
httpResponseCode: http.StatusAccepted,
},
{
Expand Down Expand Up @@ -685,6 +690,9 @@ func Test_PushMetrics(t *testing.T) {
TargetInfo: &TargetInfo{
Enabled: true,
},
CreatedMetric: &CreatedMetric{
Enabled: true,
},
}

if useWAL {
Expand Down Expand Up @@ -867,6 +875,9 @@ func TestWALOnExporterRoundTrip(t *testing.T) {
TargetInfo: &TargetInfo{
Enabled: true,
},
CreatedMetric: &CreatedMetric{
Enabled: false,
},
}

set := exportertest.NewNopCreateSettings()
Expand Down
3 changes: 3 additions & 0 deletions exporter/prometheusremotewriteexporter/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,5 +111,8 @@ func createDefaultConfig() component.Config {
TargetInfo: &TargetInfo{
Enabled: true,
},
CreatedMetric: &CreatedMetric{
Enabled: false,
},
}
}
2 changes: 2 additions & 0 deletions exporter/prometheusremotewriteexporter/testdata/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ prometheusremotewrite/2:
key2: value2
resource_to_telemetry_conversion:
enabled: true
export_created_metric:
enabled: true
remote_write_queue:
queue_size: 2000
num_consumers: 10
Expand Down
Loading

0 comments on commit 6e4baef

Please sign in to comment.