Skip to content

Commit

Permalink
Phase1 jetty linux (#46)
Browse files Browse the repository at this point in the history
* PoC for setting app server middleware attributes from splunk distro.

* First iteration of jetty test matrix.

* Proper end-to-end test for Jetty.

* Added copyright headers.

* Spotless.

* Temporarily build test images before smoke test.

* Rename shared to bootstrap and other PR fixes.

* Removed middleware submodule.

* Verify middleware attributes also in webapp test.

* spotless

* Add middleware attributes to the server span only.

* Add a TODO reference to an important PR.
  • Loading branch information
vovencij authored Nov 30, 2020
1 parent cfbc13d commit 4d51b2b
Show file tree
Hide file tree
Showing 24 changed files with 948 additions and 124 deletions.
72 changes: 72 additions & 0 deletions agent/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
plugins {
id("com.github.johnrengelman.shadow") version "6.0.0"
}
configurations {
customShadow
}
dependencies {
customShadow project(path: ":custom", configuration: "shadow")
customShadow project(path: ":instrumentation", configuration: "shadow")
implementation "io.opentelemetry.javaagent:opentelemetry-javaagent:${versions.opentelemetryJavaagent}:all"
implementation project(":bootstrap")
}

archivesBaseName = "splunk-otel-javaagent"

compileJava {
options.release.set(8)
}

CopySpec isolateSpec() {
return copySpec {
configurations.customShadow.files.each {
from(zipTree(it)) {
into("inst")
rename("(^.*)\\.class\$", "\$1.classdata")
}
}
}
}

tasks {
shadowJar {
dependsOn ':custom:shadowJar'
dependsOn ':instrumentation:shadowJar'
dependsOn ':bootstrap:jar'
with isolateSpec()

/// TODO - with this set to EXCLUDE, service files will not be merged between instrumentation and custom
duplicatesStrategy = DuplicatesStrategy.INCLUDE

mergeServiceFiles {
include("inst/META-INF/services/*")
}
exclude("**/module-info.class")

// Prevents conflict with other SLF4J instances. Important for premain.
relocate("org.slf4j", "io.opentelemetry.javaagent.slf4j")
// rewrite dependencies calling Logger.getLogger
relocate("java.util.logging.Logger", "io.opentelemetry.javaagent.bootstrap.PatchLogger")

// prevents conflict with library instrumentation
relocate("io.opentelemetry.instrumentation.api", "io.opentelemetry.javaagent.shaded.instrumentation.api")

// relocate OpenTelemetry API
relocate("io.opentelemetry.api", "io.opentelemetry.javaagent.shaded.io.opentelemetry.api")
relocate("io.opentelemetry.context", "io.opentelemetry.javaagent.shaded.io.opentelemetry.context")

manifest {
attributes.put("Main-Class", "io.opentelemetry.javaagent.OpenTelemetryAgent")
attributes.put("Agent-Class", "com.splunk.opentelemetry.SplunkAgent")
attributes.put("Premain-Class", "com.splunk.opentelemetry.SplunkAgent")
attributes.put("Can-Redefine-Classes", "true")
attributes.put("Can-Retransform-Classes", "true")
attributes.put("Implementation-Vendor", "Splunk")
attributes.put("Implementation-Version", "splunk-${project.version}-otel-${versions["opentelemetryJavaagent"]}")
}
}

assemble {
dependsOn(shadowJar)
}
}
49 changes: 0 additions & 49 deletions agent/build.gradle.kts

This file was deleted.

11 changes: 11 additions & 0 deletions bootstrap/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
plugins {
id "java"
}

compileJava {
options.release.set(8)
}

dependencies {
implementation "org.slf4j:slf4j-api:1.7.30"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright Splunk 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.splunk.opentelemetry.javaagent.bootstrap;

import java.util.concurrent.atomic.AtomicReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MiddlewareHolder {

private static final Logger log = LoggerFactory.getLogger(MiddlewareHolder.class);

public static final AtomicReference<String> middlewareName = new AtomicReference<>();
public static final AtomicReference<String> middlewareVersion = new AtomicReference<>();

public static void trySetName(String name) {
if (!middlewareName.compareAndSet(null, name)) {
log.debug("Trying to re-set middleware name from {} to {}", middlewareName.get(), name);
}
}

public static void trySetVersion(String version) {
if (!middlewareVersion.compareAndSet(null, version)) {
log.debug(
"Trying to re-set middleware version from {} to {}", middlewareVersion.get(), version);
}
}
}
5 changes: 5 additions & 0 deletions custom/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@ plugins {
val versions: Map<String, String> by extra

dependencies {
implementation(project(":bootstrap"))
implementation("io.opentelemetry:opentelemetry-sdk:${versions["opentelemetry"]}")
implementation("io.opentelemetry:opentelemetry-exporter-jaeger-thrift:${versions["opentelemetry"]}")
implementation("io.opentelemetry.javaagent:opentelemetry-javaagent-spi:${versions["opentelemetryJavaagent"]}")
annotationProcessor("com.google.auto.service:auto-service:1.0-rc3")
annotationProcessor("com.google.auto:auto-common:0.8")
implementation("com.google.auto.service:auto-service:1.0-rc3")
implementation("com.google.auto:auto-common:0.8")
}

tasks {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright Splunk 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.splunk.opentelemetry.middleware;

import com.splunk.opentelemetry.javaagent.bootstrap.MiddlewareHolder;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.context.Context;
import io.opentelemetry.sdk.common.CompletableResultCode;
import io.opentelemetry.sdk.trace.ReadWriteSpan;
import io.opentelemetry.sdk.trace.ReadableSpan;
import io.opentelemetry.sdk.trace.SpanProcessor;

public class MiddlewareAttributeSpanProcessor implements SpanProcessor {

@Override
public void onStart(Context parentContext, ReadWriteSpan span) {
String middlewareName = MiddlewareHolder.middlewareName.get();
String middlewareVersion = MiddlewareHolder.middlewareVersion.get();
// Getting span kind is not the most straightforward or cheap operation apparently.
// TODO: Once this PR is merged and released, use span.getKind directly:
// https://github.com/open-telemetry/opentelemetry-java/pull/2162
// Let's do quick cheap null checks first and exit quickly.
if ((middlewareName == null && middlewareVersion == null)
|| span.toSpanData().getKind() != Span.Kind.SERVER) {
return;
}
if (middlewareName != null) {
span.setAttribute(MiddlewareAttributes.MIDDLEWARE_NAME.key, middlewareName);
}
if (middlewareVersion != null) {
span.setAttribute(MiddlewareAttributes.MIDDLEWARE_VERSION.key, middlewareVersion);
}
}

@Override
public boolean isStartRequired() {
return true;
}

@Override
public void onEnd(ReadableSpan span) {}

@Override
public boolean isEndRequired() {
return false;
}

@Override
public CompletableResultCode shutdown() {
return CompletableResultCode.ofSuccess();
}

@Override
public CompletableResultCode forceFlush() {
return CompletableResultCode.ofSuccess();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright Splunk 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.splunk.opentelemetry.middleware;

public enum MiddlewareAttributes {
MIDDLEWARE_NAME("middleware.name"),
MIDDLEWARE_VERSION("middleware.version");

public final String key;

MiddlewareAttributes(String key) {
this.key = key;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright Splunk 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.splunk.opentelemetry.middleware;

import com.google.auto.service.AutoService;
import com.splunk.opentelemetry.javaagent.bootstrap.MiddlewareHolder;
import io.opentelemetry.javaagent.spi.BootstrapPackagesProvider;
import java.util.Collections;
import java.util.List;

@AutoService(BootstrapPackagesProvider.class)
public class MiddlewareBootstrapPackagesProvider implements BootstrapPackagesProvider {

@Override
public List<String> getPackagePrefixes() {
return Collections.singletonList(MiddlewareHolder.class.getPackage().getName());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright Splunk 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.splunk.opentelemetry.middleware;

import com.google.auto.service.AutoService;
import io.opentelemetry.javaagent.spi.TracerCustomizer;
import io.opentelemetry.sdk.trace.TracerSdkManagement;

@AutoService(TracerCustomizer.class)
public class MiddlewareTracerCustomizer implements TracerCustomizer {

@Override
public void configure(TracerSdkManagement tracerManagement) {
tracerManagement.addSpanProcessor(new MiddlewareAttributeSpanProcessor());
}
}
Loading

0 comments on commit 4d51b2b

Please sign in to comment.