2017-04-04 5 views
0

Ich verwende die BurntSushi-Bibliothek, um eine TOML-Konfigurationsdatei in meine GO-Anwendung zu laden. Ich habe die Anweisungen in der Bibliothek befolgt, um die Strukturen und die Konfigurationsdatei selbst zu schreiben. Ich gerate in Schwierigkeiten, und ich kann nicht die Quelle meiner Probleme finden.TOML-Datei kann nicht mit Go mit BurntSushi-Bibliothek gelesen werden

Hier sind die Details:

Structs:

package main 


//ConfigurationParameters provides the struct to hold configuration parameters from config file 
type ConfigurationParameters struct { 
    Title string 
    //serviceDiscovery captures configuration parameters needed for service discovery registration with Consul 
    ServiceDiscovery ConsulConf `toml:"ServiceDiscovery"` 
    //metadataReporting captures which metadata to be registered with service into consul for use during discovery 
    MetadataReporting MetaDataConf `toml:"MetadataReporting"` 
    //awsTagsToLabels captures the aws tags that should be added to reported metrics as Labels 
    AwsTagsToLabels LabelConf `toml:"AwsTagsToLabels"` 
    //collectors captures the list of collectors to use 
    Collectors CollectorConf `toml:"Collectors"` 
    //service captures agent related configurations 
    Service ServiceConf `toml:"Service"` 
} 

//ConsulConf captures configuration parameters needed for service discovery registration with Consul 
type ConsulConf struct { 
    enabled bool 
    endpoint string 
    port  int 
    datacenter string 
    serviceID string 
} 
//MetaDataConf captures which metadata to be registered with service into consul for use during discovery 
    type MetaDataConf struct { 
     enabled bool 
     awsregion string 
    } 
//LabelConf captures the aws tags that should be added to reported metrics as Labels 
type LabelConf struct { 
    enabled  bool 
    refreshPeriod int 
} 

//CollectorConf captures the list of collectors to use 
type CollectorConf struct { 
    goCollectionEnabled  bool 
    exporterCollectionEnabled bool 
    wmiCollectionEnabled  bool 
    agentCollectionEnabled bool 
    enabledCollectors   string 
    metricMap     []MetricMap 
} 

//MetricMap captures a mapping between one or more WMI metrics and the name it should be reported with 
type MetricMap struct { 
    wmiMetricName []string 
    exportName  string 
    dropMetric  bool 
    computedMetric bool 
    computeLogic string 
} 

//ServiceConf captures agent related configurations 
type ServiceConf struct { 
    listenIP   string 
    listenPort   int 
    metricPath   string 
    collectionInterval int 
    serviceName  string 
} 

und die Konfiguration TomL Datei:

Title = "WMI Exporter Configuration" 

[ServiceDiscovery] 
    enabled = true 
    endpoint = "my.consul.server" 
    port = 5500 
    datacenter = "ucm-west" 
    serviceID = "ucm.agent.wmiExporter" 

[MetadataReporting] 
    enabled = true 
    awsregion = "us-west-2" 

[AwsTagsToLabels] 
    enabled = true 
    refreshPeriod = 3600 

[Collectors] 
    goCollectionEnabled = true 
    exporterCollectionEnabled = true 
    wmiCollectionEnabled = true 
    agentCollectionEnabled = false 
    enabledCollectors = "cpu,os" 
    [Collectors.MetricMap.0] 
     wmiMetricName = ["test"] 
     exportName = "export_test" 

[Service] 
    listenPort = 9103 
    metricPath = "/metrics" 
    collectionInterval = 60 
    serviceName = "wmi_exporter" 

Und der Code, der die Konfigurationsdatei lautet:

// InitializeFromConfig reads configuration parameters from configuration file and initializes this service 
func InitializeFromConfig(configfile string) ConfigurationParameters { 
    conf := ConfigurationParameters{} 

    if configfile == "" { 
     return conf 
    } 

    _, err := toml.DecodeFile(configfile, &conf) 
    if err != nil { 
     log.Fatalf("Cannot parse configuration file at %s. Error=%s", configfile, err) 
    } 
    //at this point, conf is a fully loaded configuration now; now initialize everything from conf 
    return conf 
} 

Das Problem, mit dem ich konfrontiert bin, besteht darin, dass nur der Wert für das Title-Attribut in die GO-Strukturmitglieder eingeordnet wird. Alle anderen Konfigurationen bleiben nicht zugeordnet. Betrachtet man alle Beispiele auf GitHub für BurntSushi und (Go) How to use toml files?, sehe ich keinen Unterschied zu dem, was ich gerade im Code mache.

Ich habe auch den Tomlv-Validator aus dem BurntSushi-Paket verwendet, um die Typen aus der TOML-Datei zu betrachten, und ich glaube, sie sehen korrekt aus.

Datentypen:

Title            String 
ServiceDiscovery         Hash 
    ServiceDiscovery.enabled      Bool 
    ServiceDiscovery.endpoint      String 
    ServiceDiscovery.port       Integer 
    ServiceDiscovery.datacenter     String 
    ServiceDiscovery.serviceID     String 
MetadataReporting         Hash 
    MetadataReporting.enabled      Bool 
    MetadataReporting.awsregion     String 
AwsTagsToLabels         Hash 
    AwsTagsToLabels.enabled      Bool 
    AwsTagsToLabels.refreshPeriod     Integer 
Collectors          Hash 
    Collectors.goCollectionEnabled    Bool 
    Collectors.exporterCollectionEnabled   Bool 
    Collectors.wmiCollectionEnabled    Bool 
    Collectors.agentCollectionEnabled    Bool 
    Collectors.enabledCollectors     String 
     Collectors.MetricMap.0     Hash 
      Collectors.MetricMap.0.wmiMetricName Array 
      Collectors.MetricMap.0.exportName  String 
Service           Hash 
    Service.listenPort       Integer 
    Service.metricPath       String 
    Service.collectionInterval     Integer 
    Service.serviceName       String 

Ich habe versucht, das Debuggen in den Code BurntSushi Paket, aber es war nicht sehr hilfreich (der Delve Debugger nicht in der Lage war in diesem Paket einige der Variablen anzuzeigen, und schien zufällig zu springen zwischen den Zeilen in diesem Paket).

Irgendwelche Hilfe oder Hinweise darauf, was ich falsch machen könnte?

Danke.

+0

Alle Felder Ihrer substructs unexported sind. Hier https://play.golang.org/p/OdVDj81KWw das wird es so dekodieren, wie Sie es wollen, fast, es wird auf dem 'MetricMap' Zeug scheitern, Sie müssen dort ein wenig debuggen. – mkopriva

+0

Btw, um das 'MetricMap' Problem zu beheben, sollten Sie' Collectors.MetricMap' in doppelte Klammern wie dieses '[[Collectors.MetricMap]]' einbinden, da Sie das Feld als '[] MetricMap definieren. – mkopriva

+0

@mkopriva - danke! das hat funktioniert. Wenn Sie das als Antwort hinzufügen können, kann ich es als Antwort markieren. – sujitv

Antwort

1

Sie müssen alle Felder exportieren, die Sie BurntSushi/toml in entpacken wollen, einschließlich Subfelder:

//ConfigurationParameters provides the struct to hold configuration parameters from config file 
type ConfigurationParameters struct { 
    Title string 
    //serviceDiscovery captures configuration parameters needed for service discovery registration with Consul 
    ServiceDiscovery ConsulConf `toml:"ServiceDiscovery"` 
    //metadataReporting captures which metadata to be registered with service into consul for use during discovery 
    MetadataReporting MetaDataConf `toml:"MetadataReporting"` 
    //awsTagsToLabels captures the aws tags that should be added to reported metrics as Labels 
    AwsTagsToLabels LabelConf `toml:"AwsTagsToLabels"` 
    //collectors captures the list of collectors to use 
    Collectors CollectorConf `toml:"Collectors"` 
    //service captures agent related configurations 
    Service ServiceConf `toml:"Service"` 
} 

//ConsulConf captures configuration parameters needed for service discovery registration with Consul 
type ConsulConf struct { 
    Enabled bool 
    Endpoint string 
    Port  int 
    Datacenter string 
    ServiceID string 
} 

//MetaDataConf captures which metadata to be registered with service into consul for use during discovery 
type MetaDataConf struct { 
    Enabled bool 
    Awsregion string 
} 

//LabelConf captures the aws tags that should be added to reported metrics as Labels 
type LabelConf struct { 
    Enabled  bool 
    RefreshPeriod int 
} 

//CollectorConf captures the list of collectors to use 
type CollectorConf struct { 
    GoCollectionEnabled  bool 
    ExporterCollectionEnabled bool 
    WmiCollectionEnabled  bool 
    AgentCollectionEnabled bool 
    EnabledCollectors   string 
    MetricMap     []MetricMap 
} 

//MetricMap captures a mapping between one or more WMI metrics and the name it should be reported with 
type MetricMap struct { 
    WmiMetricName []string 
    ExportName  string 
    DropMetric  bool 
    ComputedMetric bool 
    ComputeLogic string 
} 

//ServiceConf captures agent related configurations 
type ServiceConf struct { 
    ListenIP   string 
    ListenPort   int 
    MetricPath   string 
    CollectionInterval int 
    ServiceName  string 
} 

Auch ich bin nicht sicher, was diese Collectors.MetricMap.0 Syntax aber darstellen soll Ihre TomL Werte in die entpacken []MetricMap Feld , was Sie tun möchten, ist so etwas wie dieses stattdessen:

[[Collectors.MetricMap]] 
    wmiMetricName = ["test"] 
    exportName = "export_test" 
Verwandte Themen