2727import com .google .monitoring .v3 .TypedValue ;
2828import io .opentelemetry .api .common .AttributeKey ;
2929import io .opentelemetry .api .common .Attributes ;
30+ import io .opentelemetry .api .common .AttributesBuilder ;
3031import io .opentelemetry .sdk .common .InstrumentationScopeInfo ;
3132import io .opentelemetry .sdk .metrics .data .DoublePointData ;
3233import io .opentelemetry .sdk .metrics .data .HistogramPointData ;
3334import io .opentelemetry .sdk .metrics .data .LongPointData ;
3435import io .opentelemetry .sdk .metrics .data .MetricData ;
3536import io .opentelemetry .sdk .metrics .data .PointData ;
37+ import io .opentelemetry .sdk .resources .Resource ;
3638import java .util .Collection ;
3739import java .util .HashMap ;
3840import java .util .List ;
3941import java .util .Map ;
4042import java .util .Objects ;
43+ import java .util .function .Predicate ;
4144import java .util .stream .Collectors ;
4245
4346/**
@@ -55,10 +58,20 @@ public final class AggregateByLabelMetricTimeSeriesBuilder implements MetricTime
5558 private final Map <MetricWithLabels , TimeSeries .Builder > pendingTimeSeries = new HashMap <>();
5659 private final String projectId ;
5760 private final String prefix ;
61+ private final Predicate <AttributeKey <?>> resourceAttributeFilter ;
5862
63+ @ Deprecated
5964 public AggregateByLabelMetricTimeSeriesBuilder (String projectId , String prefix ) {
6065 this .projectId = projectId ;
6166 this .prefix = prefix ;
67+ this .resourceAttributeFilter = MetricConfiguration .NO_RESOURCE_ATTRIBUTES ;
68+ }
69+
70+ public AggregateByLabelMetricTimeSeriesBuilder (
71+ String projectId , String prefix , Predicate <AttributeKey <?>> resourceAttributeFilter ) {
72+ this .projectId = projectId ;
73+ this .prefix = prefix ;
74+ this .resourceAttributeFilter = resourceAttributeFilter ;
6275 }
6376
6477 @ Override
@@ -96,15 +109,21 @@ public void recordPoint(MetricData metricData, HistogramPointData pointData) {
96109 }
97110
98111 private void recordPointInTimeSeries (MetricData metric , PointData point , Point builtPoint ) {
99- MetricDescriptor descriptor = mapMetricDescriptor (this .prefix , metric , point );
112+ MetricDescriptor descriptor =
113+ mapMetricDescriptor (
114+ this .prefix , metric , point , extraLabelsFromResource (metric .getResource ()));
100115 if (descriptor == null ) {
101116 // Unsupported type.
102117 return ;
103118 }
104119 descriptors .putIfAbsent (descriptor .getType (), descriptor );
105120 Attributes metricAttributes =
106- attachInstrumentationLibraryLabels (
107- point .getAttributes (), metric .getInstrumentationScopeInfo ());
121+ Attributes .builder ()
122+ .putAll (
123+ instrumentationLibraryLabels (
124+ point .getAttributes (), metric .getInstrumentationScopeInfo ()))
125+ .putAll (extraLabelsFromResource (metric .getResource ()))
126+ .build ();
108127 MetricWithLabels key = new MetricWithLabels (descriptor .getType (), metricAttributes );
109128 pendingTimeSeries
110129 .computeIfAbsent (key , k -> makeTimeSeriesHeader (metric , metricAttributes , descriptor ))
@@ -119,7 +138,13 @@ private TimeSeries.Builder makeTimeSeriesHeader(
119138 .setResource (mapResource (metric .getResource ()));
120139 }
121140
122- private Attributes attachInstrumentationLibraryLabels (
141+ private Attributes extraLabelsFromResource (Resource resource ) {
142+ AttributesBuilder attrBuilder = resource .getAttributes ().toBuilder ();
143+ attrBuilder .removeIf (resourceAttributeFilter .negate ());
144+ return attrBuilder .build ();
145+ }
146+
147+ private Attributes instrumentationLibraryLabels (
123148 Attributes attributes , InstrumentationScopeInfo instrumentationScopeInfo ) {
124149 return attributes .toBuilder ()
125150 .put (
0 commit comments