Skip to content

Commit

Permalink
Telemetry collect subscription ID (#389)
Browse files Browse the repository at this point in the history
* Collect subscription id in telemetry

* Separating the error trace to 2, so that we only need to redacate the details

* Split aztfexport end error to 2

* Fix a minor bug in the effective cli arg
  • Loading branch information
magodo authored Apr 25, 2023
1 parent 66596c4 commit 852f37d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 18 deletions.
2 changes: 1 addition & 1 deletion flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (flag FlagSet) DescribeCLI(mode string) string {
args = append(args, "--hcl-only=true")
}
if flag.hflagTFClientPluginPath != "" {
args = append(args, "--tfclient-plugin-path=%s", flag.hflagTFClientPluginPath)
args = append(args, "--tfclient-plugin-path="+flag.hflagTFClientPluginPath)
}
if flag.flagModulePath != "" {
args = append(args, "--module-path="+flag.flagModulePath)
Expand Down
9 changes: 6 additions & 3 deletions internal/meta/base_meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,8 @@ func (meta *baseMeta) importItem_tf(ctx context.Context, item *ImportItem, impor
err := tf.Import(ctx, addr, item.TFResourceId)
if err != nil {
log.Printf("[ERROR] Importing %s: %v", item.TFAddr, err)
meta.tc.Trace(telemetry.Error, fmt.Sprintf("Importing %s: %v", item.AzureResourceID.TypeString(), err))
meta.tc.Trace(telemetry.Error, fmt.Sprintf("Importing %s failed", item.AzureResourceID.TypeString()))
meta.tc.Trace(telemetry.Error, fmt.Sprintf("Error detail: %v", err))
} else {
meta.tc.Trace(telemetry.Info, fmt.Sprintf("Importing %s as %s successfully", item.AzureResourceID.TypeString(), addr))
}
Expand All @@ -797,7 +798,8 @@ func (meta *baseMeta) importItem_notf(ctx context.Context, item *ImportItem, imp
})
if diags.HasErrors() {
log.Printf("[ERROR] Importing %s: %v", item.TFAddr, diags)
meta.tc.Trace(telemetry.Error, fmt.Sprintf("Importing %s: %v", item.AzureResourceID.TypeString(), diags))
meta.tc.Trace(telemetry.Error, fmt.Sprintf("Importing %s failed", item.AzureResourceID.TypeString()))
meta.tc.Trace(telemetry.Error, fmt.Sprintf("Error detail: %v", diags.Err()))
item.ImportError = diags.Err()
item.Imported = false
return
Expand All @@ -818,7 +820,8 @@ func (meta *baseMeta) importItem_notf(ctx context.Context, item *ImportItem, imp
})
if diags.HasErrors() {
log.Printf("[ERROR] Reading %s: %v", item.TFAddr, diags)
meta.tc.Trace(telemetry.Error, fmt.Sprintf("Reading %s: %v", item.AzureResourceID.TypeString(), diags))
meta.tc.Trace(telemetry.Error, fmt.Sprintf("Reading %s failed", item.AzureResourceID.TypeString()))
meta.tc.Trace(telemetry.Error, fmt.Sprintf("Error detail: %v", diags.Err()))
item.ImportError = diags.Err()
item.Imported = false
return
Expand Down
23 changes: 12 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ func main() {
Parallelism: flagset.flagParallelism,
HCLOnly: flagset.flagHCLOnly,
ModulePath: flagset.flagModulePath,
TelemetryClient: initTelemetryClient(),
TelemetryClient: initTelemetryClient(flagset.flagSubscriptionId),
},
ResourceId: resId,
TFResourceName: flagset.flagResName,
Expand Down Expand Up @@ -499,7 +499,7 @@ func main() {
Parallelism: flagset.flagParallelism,
HCLOnly: flagset.flagHCLOnly,
ModulePath: flagset.flagModulePath,
TelemetryClient: initTelemetryClient(),
TelemetryClient: initTelemetryClient(flagset.flagSubscriptionId),
},
ResourceGroupName: rg,
ResourceNamePattern: flagset.flagPattern,
Expand Down Expand Up @@ -562,7 +562,7 @@ func main() {
Parallelism: flagset.flagParallelism,
HCLOnly: flagset.flagHCLOnly,
ModulePath: flagset.flagModulePath,
TelemetryClient: initTelemetryClient(),
TelemetryClient: initTelemetryClient(flagset.flagSubscriptionId),
},
ARGPredicate: predicate,
ResourceNamePattern: flagset.flagPattern,
Expand Down Expand Up @@ -626,7 +626,7 @@ func main() {
Parallelism: flagset.flagParallelism,
HCLOnly: flagset.flagHCLOnly,
ModulePath: flagset.flagModulePath,
TelemetryClient: initTelemetryClient(),
TelemetryClient: initTelemetryClient(flagset.flagSubscriptionId),
},
MappingFile: mapFile,
}
Expand Down Expand Up @@ -716,29 +716,29 @@ func initLog(path string, flagLevel string) error {
return nil
}

func initTelemetryClient() telemetry.Client {
func initTelemetryClient(subscriptionId string) telemetry.Client {
cfg, err := cfgfile.GetConfig()
if err != nil {
return telemetry.NewNullClient()
}
enabled, id := cfg.TelemetryEnabled, cfg.InstallationId
enabled, installId := cfg.TelemetryEnabled, cfg.InstallationId
if !enabled {
return telemetry.NewNullClient()
}
if id == "" {
if installId == "" {
uuid, err := uuid.NewV4()
if err == nil {
id = uuid.String()
installId = uuid.String()
} else {
id = "undefined"
installId = "undefined"
}
}

sessionId := "undefined"
if uuid, err := uuid.NewV4(); err == nil {
sessionId = uuid.String()
}
return telemetry.NewAppInsight(id, sessionId)
return telemetry.NewAppInsight(subscriptionId, installId, sessionId)
}

// At most one of below is true
Expand Down Expand Up @@ -895,7 +895,8 @@ func realMain(ctx context.Context, cfg config.Config, batch, mockMeta, plainUI,
tc.Trace(telemetry.Info, "aztfexport ends")
} else {
log.Printf("[ERROR] aztfexport ends with error: %v", result)
tc.Trace(telemetry.Error, fmt.Sprintf("aztfexport ends with error: %v", result))
tc.Trace(telemetry.Error, fmt.Sprintf("aztfexport ends with error"))
tc.Trace(telemetry.Error, fmt.Sprintf("Error detail: %v", result))
}
tc.Close()
}()
Expand Down
10 changes: 7 additions & 3 deletions pkg/telemetry/telemetry.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,28 +33,32 @@ func (NullClient) Close() {}

type AppInsightClient struct {
appinsights.TelemetryClient
installId string
sessionId string
subscriptionId string
installId string
sessionId string
}

func NewAppInsight(installId string, sessionid string) Client {
func NewAppInsight(subscriptionId string, installId string, sessionid string) Client {
// The instrument key of a MS managed application insights
const instrumentKey = "1bfe1d29-b42e-49b5-9d51-77514f85b37b"
return AppInsightClient{
TelemetryClient: appinsights.NewTelemetryClient(instrumentKey),
subscriptionId: subscriptionId,
installId: installId,
sessionId: sessionid,
}
}

type ApplicationInsightMessage struct {
SubscriptionId string `json:"subscription_id"`
InstallationId string `json:"installation_id"`
SessionId string `json:"session_id"`
Payload string `json:"payload"`
}

func (c AppInsightClient) Trace(level Level, payload string) {
msg := ApplicationInsightMessage{
SubscriptionId: c.subscriptionId,
InstallationId: c.installId,
SessionId: c.sessionId,
Payload: payload,
Expand Down

0 comments on commit 852f37d

Please sign in to comment.