GoogleCloudPlatform / spring-cloud-gcp

New home for Spring Cloud GCP development starting with version 2.0.
Apache License 2.0
410 stars 298 forks source link

Support For W3C Trace Propagation #2921

Open hoduulmu opened 3 months ago

hoduulmu commented 3 months ago

Please understand that my English may be awkward and I may appear rude.

Problem

I use GCP Cloud Run and it only use w3c trace propagation spec only. you can see the documentation about it (link)

and in com.google.cloud.spring.autoconfigure.trace.StackdriverTraceAutoConfiguration it use b3 propagation only

@Bean
    @ConditionalOnMissingBean
    public BaggagePropagation.FactoryBuilder baggagePropagationFactoryBuilder() {
        Propagation.Factory primary = B3Propagation.newFactoryBuilder().injectFormat(Format.MULTI).build();
        return BaggagePropagation.newFactoryBuilder(StackdriverTracePropagation.newFactory(primary));
    }


so the problem is when request pass many server and in log explorer,
GCP Cloud Run's trace id and spring cloud gcp's trace id is different like picture below. (in trace:)
(first one is cloud run's log and second one is spring cloud gcp's log)
gcp generate another trace id because it doesn't understand b3 header i think.

400d1750-acbd-4300-8350-5d9f8c98f92a

My Workaround

so i wrote a code in my common lib like below and it work now, but i think it's not solid code.

@AutoConfiguration(before = {StackdriverTraceAutoConfiguration.class})
@ConditionalOnProperty(prefix = "temp", name = "enabled", havingValue = "true", matchIfMissing = true)
@EnableConfigurationProperties(TracingPropagationProperties.class)
public class TracingPropagationConfig {

    @Bean
    @ConditionalOnMissingBean(BaggagePropagation.FactoryBuilder.class)
    public BaggagePropagation.FactoryBuilder baggagePropagationFactoryBuilder(TracingPropagationProperties properties) {
        Propagation.Factory primary = getPropagation(properties.type());
        return BaggagePropagation.newFactoryBuilder(StackdriverTracePropagation.newFactory(primary));
    }
    private Factory getPropagation(PropagationType type) {
        return switch (type) {
            case W3C -> new W3CPropagation();
            case B3 -> B3Propagation.newFactoryBuilder().injectFormat(Format.MULTI).build();
        };
    }
}

Suggestion

Any plan to support w3c propagation?