|
| 1 | +package frameworks |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + |
| 8 | + "github.com/cloudfoundry/java-buildpack/src/java/common" |
| 9 | + "github.com/cloudfoundry/libbuildpack" |
| 10 | +) |
| 11 | + |
| 12 | +const cfMetricsExporterDependencyName = "cf-metrics-exporter" |
| 13 | +const cfMetricsExporterDirName = "cf_metrics_exporter" |
| 14 | + |
| 15 | +// Installer interface for dependency installation |
| 16 | +// Allows for mocking in tests |
| 17 | +// Only the InstallDependency method is needed for this framework |
| 18 | +// (matches the signature of libbuildpack.Installer) |
| 19 | +type Installer interface { |
| 20 | + InstallDependency(dep libbuildpack.Dependency, outputDir string) error |
| 21 | +} |
| 22 | + |
| 23 | +type CfMetricsExporterFramework struct { |
| 24 | + context *common.Context |
| 25 | + installer Installer |
| 26 | +} |
| 27 | + |
| 28 | +func NewCfMetricsExporterFramework(ctx *common.Context) *CfMetricsExporterFramework { |
| 29 | + installer := ctx.Installer |
| 30 | + if installer == nil { |
| 31 | + installer = libbuildpack.NewInstaller(ctx.Manifest) |
| 32 | + } |
| 33 | + return &CfMetricsExporterFramework{context: ctx, installer: installer} |
| 34 | +} |
| 35 | + |
| 36 | +func (f *CfMetricsExporterFramework) Detect() (string, error) { |
| 37 | + enabled := os.Getenv("CF_METRICS_EXPORTER_ENABLED") |
| 38 | + if enabled == "true" || enabled == "TRUE" { |
| 39 | + _, err := f.context.Manifest.DefaultVersion(cfMetricsExporterDependencyName) |
| 40 | + if err != nil { |
| 41 | + return "", fmt.Errorf("cf-metrics-exporter version not found in manifest: %w", err) |
| 42 | + } |
| 43 | + return "CF Metrics Exporter", nil |
| 44 | + } |
| 45 | + return "", nil |
| 46 | +} |
| 47 | + |
| 48 | +func (f *CfMetricsExporterFramework) getManifestDependency() (libbuildpack.Dependency, *libbuildpack.ManifestEntry, error) { |
| 49 | + dep, err := f.context.Manifest.DefaultVersion(cfMetricsExporterDependencyName) |
| 50 | + if err != nil { |
| 51 | + return libbuildpack.Dependency{}, nil, fmt.Errorf("cf-metrics-exporter version not found in manifest: %w", err) |
| 52 | + } |
| 53 | + entry, err := f.context.Manifest.GetEntry(dep) |
| 54 | + if err != nil { |
| 55 | + return dep, nil, fmt.Errorf("cf-metrics-exporter manifest entry not found: %w", err) |
| 56 | + } |
| 57 | + return dep, entry, nil |
| 58 | +} |
| 59 | + |
| 60 | +func (f *CfMetricsExporterFramework) Supply() error { |
| 61 | + enabled := os.Getenv("CF_METRICS_EXPORTER_ENABLED") |
| 62 | + if enabled != "true" && enabled != "TRUE" { |
| 63 | + return nil |
| 64 | + } |
| 65 | + |
| 66 | + dep, _, err := f.getManifestDependency() |
| 67 | + if err != nil { |
| 68 | + return err |
| 69 | + } |
| 70 | + |
| 71 | + agentDir := filepath.Join(f.context.Stager.DepDir(), cfMetricsExporterDirName) |
| 72 | + jarName := fmt.Sprintf("cf-metrics-exporter-%s.jar", dep.Version) |
| 73 | + jarPath := filepath.Join(agentDir, jarName) |
| 74 | + |
| 75 | + // Ensure agent directory exists |
| 76 | + if err := os.MkdirAll(agentDir, 0755); err != nil { |
| 77 | + return fmt.Errorf("failed to create agent dir: %w", err) |
| 78 | + } |
| 79 | + |
| 80 | + // Download the JAR if not present |
| 81 | + if _, err := os.Stat(jarPath); os.IsNotExist(err) { |
| 82 | + if err := f.installer.InstallDependency(dep, agentDir); err != nil { |
| 83 | + return fmt.Errorf("failed to download cf-metrics-exporter: %w", err) |
| 84 | + } |
| 85 | + if _, err := os.Stat(jarPath); err != nil { |
| 86 | + return fmt.Errorf("expected jar file not found after download: %w", err) |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + // Log activation, including properties if set |
| 91 | + props := os.Getenv("CF_METRICS_EXPORTER_PROPS") |
| 92 | + if props != "" { |
| 93 | + f.context.Log.Info("CF Metrics Exporter v%s enabled, with properties: %s", dep.Version, props) |
| 94 | + } else { |
| 95 | + f.context.Log.Info("CF Metrics Exporter v%s enabled", dep.Version) |
| 96 | + } |
| 97 | + |
| 98 | + return nil |
| 99 | +} |
| 100 | + |
| 101 | +func (f *CfMetricsExporterFramework) Finalize() error { |
| 102 | + enabled := os.Getenv("CF_METRICS_EXPORTER_ENABLED") |
| 103 | + if enabled != "true" && enabled != "TRUE" { |
| 104 | + return nil |
| 105 | + } |
| 106 | + |
| 107 | + dep, _, err := f.getManifestDependency() |
| 108 | + if err != nil { |
| 109 | + return err |
| 110 | + } |
| 111 | + |
| 112 | + jarName := fmt.Sprintf("cf-metrics-exporter-%s.jar", dep.Version) |
| 113 | + depsIdx := f.context.Stager.DepsIdx() |
| 114 | + agentPath := fmt.Sprintf("$DEPS_DIR/%s/cf_metrics_exporter/%s", depsIdx, jarName) |
| 115 | + |
| 116 | + props := os.Getenv("CF_METRICS_EXPORTER_PROPS") |
| 117 | + var javaOpt string |
| 118 | + if props != "" { |
| 119 | + javaOpt = fmt.Sprintf("-javaagent:%s=%s", agentPath, props) |
| 120 | + } else { |
| 121 | + javaOpt = fmt.Sprintf("-javaagent:%s", agentPath) |
| 122 | + } |
| 123 | + |
| 124 | + // Priority 43: after SkyWalking (41), Splunk OTEL (42) |
| 125 | + return writeJavaOptsFile(f.context, 43, cfMetricsExporterDirName, javaOpt) |
| 126 | +} |
0 commit comments