@@ -23,9 +23,11 @@ import (
2323 "net/http"
2424 "sort"
2525 "sync"
26+ "time"
2627
2728 "go.opencensus.io/trace"
2829
30+ "github.com/golang/protobuf/ptypes"
2931 commonpb "github.com/census-instrumentation/opencensus-proto/gen-go/agent/common/v1"
3032 metricspb "github.com/census-instrumentation/opencensus-proto/gen-go/metrics/v1"
3133 resourcepb "github.com/census-instrumentation/opencensus-proto/gen-go/resource/v1"
@@ -44,10 +46,11 @@ type Exporter struct {
4446
4547// Options customizes a created Prometheus Exporter.
4648type Options struct {
47- Namespace string
48- OnError func (err error )
49- ConstLabels prometheus.Labels // ConstLabels will be set as labels on all views.
50- Registry * prometheus.Registry
49+ Namespace string
50+ OnError func (err error )
51+ ConstLabels prometheus.Labels // ConstLabels will be set as labels on all views.
52+ Registry * prometheus.Registry
53+ SendTimestamps bool
5154}
5255
5356// New is the constructor to make an Exporter with the defined Options.
@@ -237,7 +240,7 @@ func (c *collector) protoTimeSeriesToPrometheusMetrics(ctx context.Context, metr
237240
238241 pmetrics := make ([]prometheus.Metric , 0 , len (ts .Points ))
239242 for _ , point := range ts .Points {
240- pmet , err := protoMetricToPrometheusMetric (ctx , point , desc , derivedPrometheusValueType , labelValues )
243+ pmet , err := protoMetricToPrometheusMetric (ctx , point , desc , derivedPrometheusValueType , labelValues , c . opts . SendTimestamps )
241244 if err == nil {
242245 pmetrics = append (pmetrics , pmet )
243246 } else {
@@ -277,7 +280,12 @@ func protoLabelKeysToLabels(protoLabelKeys []*metricspb.LabelKey) []string {
277280 return labelKeys
278281}
279282
280- func protoMetricToPrometheusMetric (ctx context.Context , point * metricspb.Point , desc * prometheus.Desc , derivedPrometheusType prometheus.ValueType , labelValues []string ) (prometheus.Metric , error ) {
283+ func protoMetricToPrometheusMetric (ctx context.Context , point * metricspb.Point , desc * prometheus.Desc , derivedPrometheusType prometheus.ValueType , labelValues []string , sendTimestamps bool ) (prometheus.Metric , error ) {
284+ timestamp , err := ptypes .Timestamp (point .Timestamp )
285+ if err != nil {
286+ timestamp = time .Now ()
287+ }
288+
281289 switch value := point .Value .(type ) {
282290 case * metricspb.Point_DistributionValue :
283291 dValue := value .DistributionValue
@@ -308,14 +316,26 @@ func protoMetricToPrometheusMetric(ctx context.Context, point *metricspb.Point,
308316 cumCount += countPerBucket
309317 points [bucket ] = cumCount
310318 }
311- return prometheus .NewConstHistogram (desc , uint64 (dValue .Count ), dValue .Sum , points , labelValues ... )
319+ metric , err := prometheus .NewConstHistogram (desc , uint64 (dValue .Count ), dValue .Sum , points , labelValues ... )
320+ if err != nil || ! sendTimestamps {
321+ return metric , err
322+ }
323+ return prometheus .NewMetricWithTimestamp (timestamp , metric ), nil
312324
313325 case * metricspb.Point_Int64Value :
314326 // Derive the Prometheus
315- return prometheus .NewConstMetric (desc , derivedPrometheusType , float64 (value .Int64Value ), labelValues ... )
327+ metric , err := prometheus .NewConstMetric (desc , derivedPrometheusType , float64 (value .Int64Value ), labelValues ... )
328+ if err != nil || ! sendTimestamps {
329+ return metric , err
330+ }
331+ return prometheus .NewMetricWithTimestamp (timestamp , metric ), nil
316332
317333 case * metricspb.Point_DoubleValue :
318- return prometheus .NewConstMetric (desc , derivedPrometheusType , value .DoubleValue , labelValues ... )
334+ metric , err := prometheus .NewConstMetric (desc , derivedPrometheusType , value .DoubleValue , labelValues ... )
335+ if err != nil || ! sendTimestamps {
336+ return metric , err
337+ }
338+ return prometheus .NewMetricWithTimestamp (timestamp , metric ), nil
319339
320340 default :
321341 return nil , fmt .Errorf ("Unhandled type: %T" , point .Value )
0 commit comments