All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.opentelemetry.contrib.staticinstrumenter.plugin.maven.ArtifactProcessor Maven / Gradle / Ivy

There is a newer version: 1.26.0-alpha
Show newest version
/*
 * Copyright The OpenTelemetry Authors
 * SPDX-License-Identifier: Apache-2.0
 */

package io.opentelemetry.contrib.staticinstrumenter.plugin.maven;

import static io.opentelemetry.contrib.staticinstrumenter.plugin.maven.PackagingSupportFactory.packagingSupportFor;

import java.io.IOException;
import java.nio.file.Path;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

class ArtifactProcessor {

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

  private final InstrumentationAgent agent;
  private final Instrumenter instrumenter;
  private final Unpacker unpacker;
  private final Packer packer;

  static ArtifactProcessor createProcessor(
      Path instrumentationFolder,
      Path preparationFolder,
      Path agentFolder,
      Path finalFolder,
      String finalNameSuffix)
      throws IOException {
    Unpacker unpacker = new Unpacker(preparationFolder);
    InstrumentationAgent agent = InstrumentationAgent.createFromClasspathAgent(agentFolder);
    Instrumenter instrumenter = new Instrumenter(agent, instrumentationFolder);
    Packer packer = new Packer(finalFolder, finalNameSuffix);
    return new ArtifactProcessor(unpacker, instrumenter, agent, packer);
  }

  ArtifactProcessor(
      Unpacker unpacker, Instrumenter instrumenter, InstrumentationAgent agent, Packer packer) {
    this.agent = agent;
    this.instrumenter = instrumenter;
    this.unpacker = unpacker;
    this.packer = packer;
  }

  /**
   * Instruments, repackages and enhances an artifacts. Exact steps:
   *
   * 
    *
  • unpacks artifacts into a temp dir *
  • runs instrumentation agent, creating instrumented copy of a JAR and dependencies *
  • packs all dependencies into a single JAR and adds open telemetry classes *
  • move jar to final location, adding a suffix *
*/ void process(Path artifact) throws IOException { PackagingSupport packagingSupport = packagingSupportFor(artifact); List artifactsToInstrument = unpacker.copyAndExtract(artifact, packagingSupport); log.info("Unpacked artifacts: {}", artifactsToInstrument); Path instrumentedArtifact = instrumenter.instrument(artifact.getFileName(), artifactsToInstrument); log.info("Main artifact after instrumentation: {}", instrumentedArtifact); agent.copyAgentClassesTo(instrumentedArtifact, packagingSupport); Path finalArtifact = packer.packAndCopy(instrumentedArtifact, packagingSupport); log.info("Final artifact: {}", finalArtifact); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy