Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

baggage labels processor #2994

Closed
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.sdk.metrics.processor;

import io.opentelemetry.api.baggage.Baggage;
import io.opentelemetry.api.metrics.common.Labels;
import io.opentelemetry.api.metrics.common.LabelsBuilder;
import io.opentelemetry.context.Context;

/**
* A labels processor which extracts labels from {@link io.opentelemetry.api.baggage.Baggage}.
* Delegates actual extraction implementation to {@link BaggageMetricsLabelsExtractor}
*/
public final class BaggageLabelsProcessor implements LabelsProcessor {
private final BaggageMetricsLabelsExtractor baggageMetricsLabelsExtractor;

public BaggageLabelsProcessor(BaggageMetricsLabelsExtractor baggageMetricsLabelsExtractor) {
this.baggageMetricsLabelsExtractor = baggageMetricsLabelsExtractor;
}

@Override
public Labels onLabelsBound(Context ctx, Labels labels) {
LabelsBuilder labelsBuilder = labels.toBuilder();
baggageMetricsLabelsExtractor.fromBaggage(Baggage.fromContext(ctx)).forEach(labelsBuilder::put);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we think this would want to be more opt-in as to which baggage labels are applied, rather than blindly applying all of them?


return labelsBuilder.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.sdk.metrics.processor;

import io.opentelemetry.api.baggage.Baggage;
import io.opentelemetry.api.metrics.common.Labels;

/** Uses {@link Baggage} to extract labels for metrics. Used with {@link BaggageLabelsProcessor} */
public interface BaggageMetricsLabelsExtractor {

Labels fromBaggage(Baggage ctx);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.sdk.metrics.processor;

import static org.assertj.core.api.Assertions.assertThat;

import io.opentelemetry.api.metrics.common.Labels;
import io.opentelemetry.api.metrics.common.LabelsBuilder;
import io.opentelemetry.context.Context;
import org.junit.jupiter.api.Test;

public class BaggageLabelsProcessorTest {

@Test
void testLabelsExtractedAndAdded() {
Labels extractedLabels = Labels.of("aa", "bb");
BaggageMetricsLabelsExtractor extractor = ctx -> extractedLabels;
BaggageLabelsProcessor labelsProcessor = new BaggageLabelsProcessor(extractor);
Labels originalLabels = Labels.of("a", "b");
Labels newLabels = labelsProcessor.onLabelsBound(Context.current(), originalLabels);

LabelsBuilder mergedLabels = newLabels.toBuilder();
extractedLabels.asMap().forEach((k, v) -> newLabels.toBuilder().put(k, v));
assertThat(newLabels).isEqualTo(mergedLabels.build());
}
}