Skip to content
This repository was archived by the owner on Jul 28, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 5 additions & 11 deletions cmd/agent/agent-local-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,8 @@ server:
metrics:
global:
scrape_interval: 1m
configs:
- name: test
host_filter: false
scrape_configs:
- job_name: local_scrape
static_configs:
- targets: ['127.0.0.1:12345']
labels:
cluster: 'localhost'
remote_write:
- url: http://localhost:9009/api/prom/push
remote_write:
- url: http://localhost:9009/api/prom/push

integrations:
agent: {}
8 changes: 8 additions & 0 deletions pkg/integrations/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
package install

import (
//
// v1 integrations
//

_ "github.com/grafana/agent/pkg/integrations/agent" // register agent
_ "github.com/grafana/agent/pkg/integrations/consul_exporter" // register consul_exporter
_ "github.com/grafana/agent/pkg/integrations/dnsmasq_exporter" // register dnsmasq_exporter
Expand All @@ -18,5 +22,9 @@ import (
_ "github.com/grafana/agent/pkg/integrations/statsd_exporter" // register statsd_exporter
_ "github.com/grafana/agent/pkg/integrations/windows_exporter" // register windows_exporter

//
// v2 integrations
//

_ "github.com/grafana/agent/pkg/integrations/v2/agent" // register agent
)
32 changes: 32 additions & 0 deletions pkg/integrations/install/shims.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package install

import (
v1 "github.com/grafana/agent/pkg/integrations"
v2 "github.com/grafana/agent/pkg/integrations/v2"
metricsutils "github.com/grafana/agent/pkg/integrations/v2/metricsutils"
)

// Perform a migration of v1 integrations which do not yet have a v2
// counterpart. These integrations will be registered as Singletons
// to maintain existing behavior.
//
// To migrate a v1 integration to a v2 migration with support for multiple
// instances, v1 integrations must manually migrate themselves by calling
// v2.RegisterDynamic directly.
func init() {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to log that we were doing this, but by the time a logger is created it's already too late and integrations would've been unmarshaled.

for _, v1Integration := range v1.RegisteredIntegrations() {
// Look to see if there's a v2 integration with the same name.
var found bool
for _, v2Integration := range v2.Registered() {
if v2Integration.Name() == v1Integration.Name() {
found = true
break
}
}
if !found {
v2.RegisterDynamic(v1Integration, v1Integration.Name(), v2.TypeSingleton, func(in interface{}) v2.WrappedConfig {
return metricsutils.CreateShim(in.(v1.Config))
})
}
}
}
45 changes: 35 additions & 10 deletions pkg/integrations/v2/metricsutils/metricshandler_integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,12 @@ func NewMetricsHandlerIntegration(
return &metricsHandlerIntegration{
integrationName: c.Name(),
instanceID: id,
common: c.MetricsConfig(),
globals: globals,
handler: h,

common: c.MetricsConfig(),
globals: globals,
handler: h,

targets: []handlerTarget{{MetricsPath: "metrics"}},
}, nil
}

Expand All @@ -45,6 +48,17 @@ type metricsHandlerIntegration struct {
common CommonConfig
globals integrations.Globals
handler http.Handler
targets []handlerTarget

runFunc func(ctx context.Context) error
}

type handlerTarget struct {
// Path relative to Handler prefix where metrics are available.
MetricsPath string
// Extra labels to inject into the target. Labels here that take precedence
// over labels with the same name from the generated target group.
Labels model.LabelSet
}

// Static typecheck tests
Expand All @@ -56,6 +70,12 @@ var (

// RunIntegration implements Integration.
func (i *metricsHandlerIntegration) RunIntegration(ctx context.Context) error {
// Call our runFunc if defined (used from integrationShim), otherwise
// fallback to no-op.
if i.runFunc != nil {
return i.runFunc(ctx)
}

<-ctx.Done()
return nil
}
Expand All @@ -71,11 +91,7 @@ func (i *metricsHandlerIntegration) Handler(prefix string) (http.Handler, error)
func (i *metricsHandlerIntegration) Targets(ep integrations.Endpoint) []*targetgroup.Group {
integrationNameValue := model.LabelValue("integrations/" + i.integrationName)

return []*targetgroup.Group{{
Targets: []model.LabelSet{{
model.AddressLabel: model.LabelValue(ep.Host),
model.MetricsPathLabel: model.LabelValue(path.Join(ep.Prefix, "metrics")),
}},
group := &targetgroup.Group{
Labels: model.LabelSet{
model.InstanceLabel: model.LabelValue(i.instanceID),
model.JobLabel: integrationNameValue,
Expand All @@ -85,9 +101,18 @@ func (i *metricsHandlerIntegration) Targets(ep integrations.Endpoint) []*targetg
"__meta_agent_integration_name": model.LabelValue(i.integrationName),
"__meta_agent_integration_instance": model.LabelValue(i.instanceID),
"__meta_agent_integration_selfscrape": model.LabelValue(boolToString(i.shouldSelfScrape())),
}.Merge(i.globals.SubsystemOpts.Labels),
},
Source: fmt.Sprintf("%s/%s", i.integrationName, i.instanceID),
}}
}

for _, t := range i.targets {
group.Targets = append(group.Targets, model.LabelSet{
model.AddressLabel: model.LabelValue(ep.Host),
model.MetricsPathLabel: model.LabelValue(path.Join(ep.Prefix, t.MetricsPath)),
}.Merge(t.Labels))
}

return []*targetgroup.Group{group}
}

// shouldSelfScrape returns true if the integration is self-scraping.
Expand Down
119 changes: 119 additions & 0 deletions pkg/integrations/v2/metricsutils/versionshim.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package metricsutils

import (
"context"
"errors"
"fmt"
"net/http"

"github.com/go-kit/log"
"github.com/prometheus/common/model"

v1 "github.com/grafana/agent/pkg/integrations"
v2 "github.com/grafana/agent/pkg/integrations/v2"
)

// CreateShim creates a shim between the v1.Config and v2.Config. The resulting
// config is NOT registered.
func CreateShim(before v1.Config) (after v2.WrappedConfig) {
return &configShim{Orig: before}
}

type configShim struct {
Orig v1.Config
}

var (
_ v2.Config = (*configShim)(nil)
_ v2.WrappedConfig = (*configShim)(nil)
)

// UnwrapConfig implements v2.WrappedConfig.
func (s *configShim) UnwrapConfig() interface{} { return s.Orig }

func (s *configShim) Name() string { return s.Orig.Name() }
func (s *configShim) Identifier(g v2.Globals) (string, error) {
return s.Orig.InstanceKey(g.AgentIdentifier)
}

func (s *configShim) NewIntegration(l log.Logger, g v2.Globals) (v2.Integration, error) {
v1Integration, err := s.Orig.NewIntegration(l)
if err != nil {
return nil, err
}

id, err := s.Identifier(g)
if err != nil {
return nil, err
}

// Map from the original CommonConfig to the new settings. This is a 1:1
// mapping, minus the loss of WALTruncateFrequency.
origCommon := s.Orig.CommonConfig()
newCommon := CommonConfig{
Enabled: origCommon.Enabled,
InstanceKey: origCommon.InstanceKey,
ScrapeIntegration: origCommon.ScrapeIntegration,
ScrapeInterval: origCommon.ScrapeInterval,
ScrapeTimeout: origCommon.ScrapeTimeout,
RelabelConfigs: origCommon.RelabelConfigs,
MetricRelabelConfigs: origCommon.MetricRelabelConfigs,
}

// Generate our handler. Original integrations didn't accept a prefix, and
// just assumed that they would be wired to /metrics somewhere.
handler, err := v1Integration.MetricsHandler()
if err != nil {
return nil, fmt.Errorf("generating http handler: %w", err)
} else if handler == nil {
handler = http.NotFoundHandler()
}

// Generate targets. Original integrations used a static set of targets,
// so this mapping can always be generated just once.
//
// Targets are generated from the result of ScrapeConfigs(), which returns a
// tuple of job name and relative metrics path.
//
// Job names were prefixed at the subsystem level with integrations/, so we
// will retain that behavior here.
v1ScrapeConfigs := v1Integration.ScrapeConfigs()
targets := make([]handlerTarget, 0, len(v1ScrapeConfigs))
for _, sc := range v1ScrapeConfigs {
targets = append(targets, handlerTarget{
MetricsPath: sc.MetricsPath,
Labels: model.LabelSet{
model.JobLabel: model.LabelValue("integrations/" + sc.JobName),
},
})
}

// Convert he run function. Original integrations sometimes returned
// ctx.Err() on exit. This isn't recommended anymore, but we need to hide the
// error if it happens, since the error was previously ignored.
runFunc := func(ctx context.Context) error {
err := v1Integration.Run(ctx)
switch {
case err == nil:
return nil
case errors.Is(err, context.Canceled) && ctx.Err() != nil:
// Hide error that no longer happens in newer integrations.
return nil
default:
return err
}
}

// Aggregate our converted settings into a v2 integration.
return &metricsHandlerIntegration{
integrationName: s.Orig.Name(),
instanceID: id,

common: newCommon,
globals: g,
handler: handler,
targets: targets,

runFunc: runFunc,
}, nil
}
Loading