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

Improve grpc metadata overwrite test #1

Merged
merged 1 commit into from
May 30, 2024
Merged
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
Expand Up @@ -5,28 +5,63 @@

package io.opentelemetry.instrumentation.grpc.v1_6;

import static org.junit.Assert.assertTrue;
import static org.assertj.core.api.Assertions.assertThat;

import io.grpc.Metadata;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.api.trace.SpanId;
import io.opentelemetry.api.trace.TraceFlags;
import io.opentelemetry.api.trace.TraceId;
import io.opentelemetry.api.trace.TraceState;
import io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator;
import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.grpc.v1_6.propagator.GrpcPropagator;
import io.opentelemetry.context.propagation.TextMapGetter;
import io.opentelemetry.context.propagation.TextMapPropagator;
import javax.annotation.Nullable;
import org.junit.jupiter.api.Test;

class MetadataSetterTest {

@Test
void checkThatIterableSizeEqualsOne() {
GrpcPropagator testGrpcPropagator = new GrpcPropagator();
Metadata metadata = new Metadata();
testGrpcPropagator.inject(Context.current(), metadata, MetadataSetter.INSTANCE);

int size = 0;
for (String s :
metadata.getAll(Metadata.Key.of(testGrpcPropagator.fields().get(0), Metadata.ASCII_STRING_MARSHALLER))) {
if (s != null) {
size++;
}
}
assertTrue(size == 1);
@Test
void overwriteTracingHeader() {
Metadata metadata = new Metadata();
TextMapPropagator propagator = W3CTraceContextPropagator.getInstance();
for (int i = 1; i <= 2; i++) {
Context context =
Context.root()
.with(
Span.wrap(
SpanContext.create(
TraceId.fromLongs(0, i),
SpanId.fromLong(i),
TraceFlags.getDefault(),
TraceState.getDefault())));
propagator.inject(context, metadata, MetadataSetter.INSTANCE);
}

assertThat(metadata.getAll(Metadata.Key.of("traceparent", Metadata.ASCII_STRING_MARSHALLER)))
.hasSize(1);

Context context =
propagator.extract(
Context.root(),
metadata,
new TextMapGetter<Metadata>() {
@Override
public Iterable<String> keys(Metadata metadata) {
return metadata.keys();
}

@Nullable
@Override
public String get(@Nullable Metadata metadata, String key) {
return metadata.get(Metadata.Key.of(key, Metadata.ASCII_STRING_MARSHALLER));
}
});

SpanContext spanContext = Span.fromContext(context).getSpanContext();
assertThat(spanContext.getTraceId()).isEqualTo(TraceId.fromLongs(0, 2));
assertThat(spanContext.getSpanId()).isEqualTo(SpanId.fromLong(2));
}
}

This file was deleted.