Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .chloggen/openmetrics-created-timestamp.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

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

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: "Add support for OpenMetrics created timestamp"

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: []

# (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:

# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: []
1 change: 1 addition & 0 deletions exporter/prometheusexporter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ The following settings can be optionally configured:
- `resource_to_telemetry_conversion`
- `enabled` (default = false): If `enabled` is `true`, all the resource attributes will be converted to metric labels by default.
- `enable_open_metrics`: (default = `false`): If true, metrics will be exported using the OpenMetrics format. Exemplars are only exported in the OpenMetrics format, and only for histogram and monotonic sum (i.e. counter) metrics.
- `enable_open_metrics_text_created_samples`: (default = `false`): If true, and if `enable_open_metrics` is also true, the new metric `*_created` with the timestamp of the metric creation will additionally be added to counters, histograms, and summaries. See the [OpenMetrics created timestamp documentation](https://github.com/prometheus/OpenMetrics/blob/v1.0.0/specification/OpenMetrics.md#counter-1) for more details.
- `add_metric_suffixes`: (default = `true`): If false, addition of type and unit suffixes is disabled. **Deprecated**: Use `translation_strategy` instead. This setting is ignored when `translation_strategy` is explicitly set.
- `translation_strategy`: Controls how OTLP metric and attribute names are translated into Prometheus metric and label names. When set, this takes precedence over `add_metric_suffixes`. Available options:
- `UnderscoreEscapingWithSuffixes`: Fully escapes metric names for classic Prometheus metric name compatibility, and includes appending type and unit suffixes.
Expand Down
3 changes: 3 additions & 0 deletions exporter/prometheusexporter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ type Config struct {
// EnableOpenMetrics enables the use of the OpenMetrics encoding option for the prometheus exporter.
EnableOpenMetrics bool `mapstructure:"enable_open_metrics"`

// EnableOpenMetricsTextCreatedSamples controls whether the created timestamp is added to metrics.
EnableOpenMetricsTextCreatedSamples bool `mapstructure:"enable_open_metrics_text_created_samples"`

// AddMetricSuffixes controls whether suffixes are added to metric names. Defaults to true.
// Deprecated: Use TranslationStrategy instead. This setting is ignored when TranslationStrategy is explicitly set.
AddMetricSuffixes bool `mapstructure:"add_metric_suffixes"`
Expand Down
1 change: 1 addition & 0 deletions exporter/prometheusexporter/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,11 @@

func createDefaultConfig() component.Config {
return &Config{
ConstLabels: map[string]string{},

Check failure on line 29 in exporter/prometheusexporter/factory.go

View workflow job for this annotation

GitHub Actions / scoped-tests-matrix (ubuntu-latest)

File is not properly formatted (gci)

Check failure on line 29 in exporter/prometheusexporter/factory.go

View workflow job for this annotation

GitHub Actions / lint-matrix (windows, exporter-3)

File is not properly formatted (gci)

Check failure on line 29 in exporter/prometheusexporter/factory.go

View workflow job for this annotation

GitHub Actions / lint-matrix (linux, exporter-3)

File is not properly formatted (gci)
SendTimestamps: false,
MetricExpiration: time.Minute * 5,
EnableOpenMetrics: false,
EnableOpenMetricsTextCreatedSamples: false,
AddMetricSuffixes: true,
}
}
Expand Down
7 changes: 4 additions & 3 deletions exporter/prometheusexporter/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@ func newPrometheusExporter(config *Config, set exporter.Settings) (*prometheusEx
handler: promhttp.HandlerFor(
registry,
promhttp.HandlerOpts{
ErrorHandling: promhttp.ContinueOnError,
ErrorLog: newPromLogger(set.Logger),
EnableOpenMetrics: config.EnableOpenMetrics,
ErrorHandling: promhttp.ContinueOnError,
ErrorLog: newPromLogger(set.Logger),
EnableOpenMetrics: config.EnableOpenMetrics,
EnableOpenMetricsTextCreatedSamples: config.EnableOpenMetricsTextCreatedSamples,
},
),
settings: set.TelemetrySettings,
Expand Down
39 changes: 39 additions & 0 deletions exporter/prometheusexporter/prometheus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -696,3 +696,42 @@ this_one_there_where_{arch="x86",instance="test-instance",job="test-service",os=
})
}
}

func TestPrometheusExporter_OpenMetricsCreatedSamples(t *testing.T) {
addr := testutil.GetAvailableLocalAddress(t)
cfg := &Config{
Namespace: "test",
ServerConfig: confighttp.ServerConfig{
Endpoint: addr,
},
EnableOpenMetrics: true,
EnableOpenMetricsTextCreatedSamples: true,
SendTimestamps: true,
MetricExpiration: 120 * time.Minute,
}

factory := NewFactory()
set := exportertest.NewNopSettings(metadata.Type)
exp, err := factory.CreateMetrics(t.Context(), set, cfg)
require.NoError(t, err)

defer func() { require.NoError(t, exp.Shutdown(t.Context())) }()

require.NoError(t, exp.Start(t.Context(), componenttest.NewNopHost()))

md := metricBuilder(0, "", "test-service", "test-instance")
assert.NoError(t, exp.ConsumeMetrics(t.Context(), md))

req, err := http.NewRequest(http.MethodGet, "http://"+addr+"/metrics", http.NoBody)
require.NoError(t, err)
req.Header.Set("Accept", "application/openmetrics-text;version=1.0.0;escaping=allow-utf-8")
res, err := http.DefaultClient.Do(req)
require.NoError(t, err)
assert.Equal(t, http.StatusOK, res.StatusCode)

blob, _ := io.ReadAll(res.Body)
_ = res.Body.Close()
output := string(blob)

assert.Contains(t, output, "test_this_one_there_where_created")
}
Loading