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

Initial Request Tracing Implementation #1093

Merged
merged 6 commits into from
Apr 12, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
*
* Copyright 2021 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.netflix.genie.common.internal.spring.autoconfigure;

import brave.Tracer;
import com.netflix.genie.common.internal.tracing.TracePropagator;
import com.netflix.genie.common.internal.tracing.brave.BraveTagAdapter;
import com.netflix.genie.common.internal.tracing.brave.BraveTracePropagator;
import com.netflix.genie.common.internal.tracing.brave.BraveTracingCleanup;
import com.netflix.genie.common.internal.tracing.brave.BraveTracingComponents;
import com.netflix.genie.common.internal.tracing.brave.impl.DefaultBraveTagAdapterImpl;
import com.netflix.genie.common.internal.tracing.brave.impl.EnvVarBraveTracePropagatorImpl;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import zipkin2.reporter.AsyncReporter;

import java.util.Set;

/**
* Auto configuration for common tracing components within Genie server and agent.
*
* @author tgianos
* @since 4.0.0
*/
@Configuration
public class CommonTracingAutoConfiguration {

/**
* Provide an implementation of {@link TracePropagator} if no other has been defined.
*
* @return instance of {@link EnvVarBraveTracePropagatorImpl}
*/
@Bean
@ConditionalOnMissingBean(BraveTracePropagator.class)
public EnvVarBraveTracePropagatorImpl braveTracePropagator() {
return new EnvVarBraveTracePropagatorImpl();
}

/**
* Provide a {@link BraveTracingCleanup} based on the context.
*
* @param reporters Any {@link AsyncReporter} instances configured
* @return A {@link BraveTracingCleanup} instance
*/
@Bean
@ConditionalOnMissingBean(BraveTracingCleanup.class)
public BraveTracingCleanup braveTracingCleaner(final Set<AsyncReporter<?>> reporters) {
return new BraveTracingCleanup(reporters);
}

/**
* Provide a {@link BraveTagAdapter} instance if no other has been provided.
*
* @return A {@link DefaultBraveTagAdapterImpl} instance which just directly applies the tags to the span
*/
@Bean
@ConditionalOnMissingBean(BraveTagAdapter.class)
public DefaultBraveTagAdapterImpl braveTagAdapter() {
return new DefaultBraveTagAdapterImpl();
}

/**
* Provide a {@link BraveTracingComponents} instance based on Brave if no other has been provided.
*
* @param tracer The {@link Tracer} instance to use
* @param tracePropagator The {@link BraveTracePropagator} to use
* @param tracingCleanup The {@link BraveTracingCleanup} to use
* @param tagAdapter The {@link BraveTagAdapter} instance to use
* @return A {@link BraveTracingComponents} instance
*/
@Bean
@ConditionalOnMissingBean(BraveTracingComponents.class)
public BraveTracingComponents braveTracingComponents(
final Tracer tracer,
final BraveTracePropagator tracePropagator,
final BraveTracingCleanup tracingCleanup,
final BraveTagAdapter tagAdapter
) {
return new BraveTracingComponents(tracer, tracePropagator, tracingCleanup, tagAdapter);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
*
* Copyright 2021 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

/**
* Auto configuration classes for modules within this package.
*
* @author tgianos
* @since 4.0.0
*/
@ParametersAreNonnullByDefault
package com.netflix.genie.common.internal.spring.autoconfigure;

import javax.annotation.ParametersAreNonnullByDefault;
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
*
* Copyright 2021 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.netflix.genie.common.internal.tracing;

/**
* An interface for implementations to adapt any tags published by default OSS components for internal conventions.
*
* @param <U> The type of object which tags should be applied to specific to the implementation
* @param <K> The key type for the tags
* @param <V> The value type for the tags
* @author tgianos
* @since 4.0.0
*/
public interface TagAdapter<U, K, V> {

/**
* The method that should be implemented in order to provide tagging. Genie OSS components should call this method
* instead of directly tagging.
*
* @param taggable The instance which tags should be applied to
* @param key The original key for the tag
* @param value The original value for the tag
*/
void tag(U taggable, K key, V value);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
*
* Copyright 2021 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.netflix.genie.common.internal.tracing;

import java.util.Map;
import java.util.Optional;

/**
* This interface exists to provide a shared contract for how trace information is shared between the Genie
* server job request handling and the agent process launch. Implementations should be shared across server and agent
* versions for compatibility.
* <p>
* Note: This somewhat copies a pattern from <a href="https://github.com/openzipkin/brave">Brave</a> however it is
* purposely decoupled so as to allow other implementations if necessary.
*
* @param <C> The trace context type that should be used for injection and returned from extraction
* @author tgianos
* @since 4.0.0
*/
public interface TracePropagator<C> {

/**
* Extract the trace context from the supplied set of key value pairs.
* <p>
* Implementations should swallow all exceptions as tracing is not critical to the completion of a job on behalf
* of the user.
*
* @param environment Generally this will be the result of {@link System#getenv()}
* @return A new instance of {@link C} containing the extracted context or {@link Optional#empty()} if no context
* information was found
*/
Optional<C> extract(Map<String, String> environment);

/**
* Inject the trace context from {@literal U} into the returned set of key value pairs for propagation to Agent.
* <p>
* Implementations should swallow all exceptions as tracing is not critical to the completion of a job on behalf
* of the user.
*
* @param traceContext The context for the active unit of work (span in Brave parlance)
* @return A set of key value pairs that should be propagated to the agent in some manner to be extracted in
* {@link #extract(Map)}
*/
Map<String, String> injectForAgent(C traceContext);

/**
* Inject the trace context from {@literal U} into the returned set of key value pairs for propagation to job.
* <p>
* Implementations should swallow all exceptions as tracing is not critical to the completion of a job on behalf
* of the user.
*
* @param traceContext The context for the active unit of work (span in Brave parlance)
* @return A set of key value pairs that should be propagated to the job in some manner which can be extracted
* by downstream systems if they so desire
*/
Map<String, String> injectForJob(C traceContext);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
*
* Copyright 2021 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.netflix.genie.common.internal.tracing;

/**
* Constants used for adding metadata to tracing spans.
*
* @author tgianos
* @since 4.0.0
*/
public final class TracingConstants {

/**
* The root of all tags related to Genie on spans.
*/
public static final String GLOBAL_TAG_BASE = "genie";

/**
* The root for all tags related to spans occurring in the Genie agent.
*/
public static final String AGENT_TAG_BASE = GLOBAL_TAG_BASE + ".agent";

/**
* The root for all tags related to spans occurring in the Genie server.
*/
public static final String SERVER_TAG_BASE = GLOBAL_TAG_BASE + ".server";

/**
* The root of all tags related to spans operating on a genie job.
*/
public static final String JOB_TAG_BASE = GLOBAL_TAG_BASE + ".job";

/**
* The tag for the unique job id.
*/
public static final String JOB_ID_TAG = JOB_TAG_BASE + ".id";

/**
* The tag for the job name.
*/
public static final String JOB_NAME_TAG = JOB_TAG_BASE + ".name";

private TracingConstants() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
*
* Copyright 2021 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.netflix.genie.common.internal.tracing.brave;

import brave.SpanCustomizer;
import com.netflix.genie.common.internal.tracing.TagAdapter;

/**
* Extension of {@link TagAdapter} that specifies that tags should be key value pairs of Strings as they are in
* Brave instrumentation.
*
* @author tgianos
* @since 4.0.0
*/
public interface BraveTagAdapter extends TagAdapter<SpanCustomizer, String, String> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
*
* Copyright 2021 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.netflix.genie.common.internal.tracing.brave;

import brave.propagation.TraceContext;
import com.netflix.genie.common.internal.tracing.TracePropagator;

/**
* Extension of {@link TracePropagator} based on <a href="https://github.com/openzipkin/brave">Brave</a> and
* <a href="https://github.com/openzipkin/b3-propagation">B3 Propagation</a>.
*
* @author tgianos
* @since 4.0.0
*/
public interface BraveTracePropagator extends TracePropagator<TraceContext> {
}