<commit_before>package command

import (
	"path/filepath"
	"strings"
	"time"

	"github.com/mackerelio/golib/pluginutil"
	"github.com/mackerelio/mackerel-agent/config"
	"github.com/mackerelio/mackerel-agent/mackerel"
	"github.com/mackerelio/mackerel-agent/metadata"
	mkr "github.com/mackerelio/mackerel-client-go"
)

func metadataGenerators(conf *config.Config) []*metadata.Generator {
	generators := make([]*metadata.Generator, 0, len(conf.MetadataPlugins))

	workdir := filepath.Join(pluginutil.PluginWorkDir(), "mackerel-metadata")
	for name, pluginConfig := range conf.MetadataPlugins {
		workdir := workdir
		if dir := lookupPluginWorkDir(pluginConfig.Command.Env); dir != "" {
			workdir = dir
		}
		generator := &metadata.Generator{
			Name:      name,
			Config:    pluginConfig,
			Cachefile: filepath.Join(workdir, name),
		}
		logger.Debugf("Metadata plugin generator created: %#v %#v", generator, generator.Config)
		generators = append(generators, generator)
	}

	return generators
}

// The directory configuration in the env config of metadata should work as
// same as metric plugins. Since the working directory of metadata plugin is
// handled by mackerel-agent (not the plugin process), we have to lookup here.
func lookupPluginWorkDir(env []string) string {
	workDirPrefix := "MACKEREL_PLUGIN_WORKDIR="
	for _, e := range env {
		if strings.HasPrefix(e, workDirPrefix) {
			return strings.TrimPrefix(e, workDirPrefix)
		}
	}
	return ""
}

type metadataResult struct {
	namespace string
	metadata  interface{}
	createdAt time.Time
}

func runMetadataLoop(app *App, termMetadataCh <-chan struct{}, quit <-chan struct{}) {<commit_msg>prepend mackerel-plugin-metadata- to the cache file of metadata plugin when MACKEREL_PLUGIN_WORKDIR is specified<commit_after>package command

import (
	"path/filepath"
	"strings"
	"time"

	"github.com/mackerelio/golib/pluginutil"
	"github.com/mackerelio/mackerel-agent/config"
	"github.com/mackerelio/mackerel-agent/mackerel"
	"github.com/mackerelio/mackerel-agent/metadata"
	mkr "github.com/mackerelio/mackerel-client-go"
)

func metadataGenerators(conf *config.Config) []*metadata.Generator {
	generators := make([]*metadata.Generator, 0, len(conf.MetadataPlugins))

	workdir := filepath.Join(pluginutil.PluginWorkDir(), "mackerel-metadata")
	for name, pluginConfig := range conf.MetadataPlugins {
		generator := &metadata.Generator{
			Name:      name,
			Config:    pluginConfig,
			Cachefile: getCacheFileName(name, workdir, pluginConfig),
		}
		logger.Debugf("Metadata plugin generator created: %#v %#v", generator, generator.Config)
		generators = append(generators, generator)
	}

	return generators
}

// The directory configuration in the env config of metadata should work as
// same as metric plugins. Since the working directory of metadata plugin is
// handled by mackerel-agent (not the plugin process), we have to lookup here.
func lookupPluginWorkDir(env []string) string {
	workDirPrefix := "MACKEREL_PLUGIN_WORKDIR="
	for _, e := range env {
		if strings.HasPrefix(e, workDirPrefix) {
			return strings.TrimPrefix(e, workDirPrefix)
		}
	}
	return ""
}

func getCacheFileName(name, defaultWorkDir string, plugin *config.MetadataPlugin) string {
	if dir := lookupPluginWorkDir(plugin.Command.Env); dir != "" {
		return filepath.Join(dir, "mackerel-plugin-metadata-"+name)
	}
	return filepath.Join(defaultWorkDir, name)
}

type metadataResult struct {
	namespace string
	metadata  interface{}
	createdAt time.Time
}

func runMetadataLoop(app *App, termMetadataCh <-chan struct{}, quit <-chan struct{}) {
{'fraction_non_alphanumeric': 0.115406162464986, 'fraction_numerical': 0.0005602240896358543, 'mean_word_length': 4.951666666666667, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3262660', 'n_tokens_mistral': 1180, 'n_tokens_neox': 1127, 'n_words': 280}