Skip to content

Commit

Permalink
Ensure that MetadataResource is not nil
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexBasinov committed Oct 27, 2023
1 parent bea25ea commit 7f783b7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
2 changes: 1 addition & 1 deletion cmd/google_cloud_ops_agent_diagnostics/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func getUserAndMergedConfigs(ctx context.Context, userConfPath string) (*confgen
func main() {
defer func() {
if r := recover(); r != nil {
log.Fatal("Recovered in run", r)
log.Fatalf("Recovering from a panic due to %v", r)
}
}()
if err := run(context.Background()); err != nil {
Expand Down
40 changes: 21 additions & 19 deletions confgenerator/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,27 +62,29 @@ func (r PrometheusMetrics) Type() string {

func (r PrometheusMetrics) Pipelines(ctx context.Context) []otel.ReceiverPipeline {
// Get the resource metadata for the instance we're running on.
resourceMetadataMap := MetadataResource.PrometheusStyleMetadata()
if p := platform.FromContext(ctx).ResourceOverride; p != nil {
resourceMetadataMap = p.PrometheusStyleMetadata()
}
if MetadataResource != nil {
resourceMetadataMap := MetadataResource.PrometheusStyleMetadata()
if p := platform.FromContext(ctx).ResourceOverride; p != nil {
resourceMetadataMap = p.PrometheusStyleMetadata()
}

// Add the GCE metadata to the prometheus config.
for i := range r.PromConfig.ScrapeConfigs {
// Iterate over the static configs.
for j := range r.PromConfig.ScrapeConfigs[i].ServiceDiscoveryConfigs {
staticConfigs := r.PromConfig.ScrapeConfigs[i].ServiceDiscoveryConfigs[j].(discovery.StaticConfig)
for k := range staticConfigs {
labels := staticConfigs[k].Labels
if labels == nil {
labels = model.LabelSet{}
// Add the GCE metadata to the prometheus config.
for i := range r.PromConfig.ScrapeConfigs {
// Iterate over the static configs.
for j := range r.PromConfig.ScrapeConfigs[i].ServiceDiscoveryConfigs {
staticConfigs := r.PromConfig.ScrapeConfigs[i].ServiceDiscoveryConfigs[j].(discovery.StaticConfig)
for k := range staticConfigs {
labels := staticConfigs[k].Labels
if labels == nil {
labels = model.LabelSet{}
}
for k, v := range resourceMetadataMap {
// If there are conflicts, the resource metadata should take precedence.
labels[model.LabelName(k)] = model.LabelValue(v)
}

staticConfigs[k].Labels = labels
}
for k, v := range resourceMetadataMap {
// If there are conflicts, the resource metadata should take precedence.
labels[model.LabelName(k)] = model.LabelValue(v)
}

staticConfigs[k].Labels = labels
}
}
}
Expand Down

0 comments on commit 7f783b7

Please sign in to comment.