2022-06-03 05:55:18 -04:00
|
|
|
package azure
|
|
|
|
|
|
|
|
import (
|
2022-06-10 07:18:30 -04:00
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/applicationinsights/armapplicationinsights"
|
|
|
|
"github.com/edgelesssys/constellation/internal/azureshared"
|
2022-06-03 05:55:18 -04:00
|
|
|
"github.com/microsoft/ApplicationInsights-Go/appinsights"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Logger struct {
|
|
|
|
client appinsights.TelemetryClient
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewLogger creates a new client to store information in Azure Application Insights
|
|
|
|
// https://github.com/Microsoft/ApplicationInsights-go
|
2022-06-10 07:18:30 -04:00
|
|
|
func NewLogger(ctx context.Context, metadata *Metadata) (*Logger, error) {
|
|
|
|
providerID, err := metadata.providerID(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, resourceGroup, err := azureshared.BasicsFromProviderID(providerID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
uid, err := azureshared.UIDFromProviderID(providerID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-06-03 05:55:18 -04:00
|
|
|
}
|
2022-06-10 07:18:30 -04:00
|
|
|
|
|
|
|
resourceName := "constellation-insights-" + uid
|
|
|
|
resp, err := metadata.applicationInsightsAPI.Get(ctx, resourceGroup, resourceName, &armapplicationinsights.ComponentsClientGetOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if resp.Properties == nil || resp.Properties.InstrumentationKey == nil {
|
|
|
|
return nil, errors.New("unable to get instrumentation key")
|
|
|
|
}
|
2022-08-01 06:32:28 -04:00
|
|
|
client := appinsights.NewTelemetryClient(*resp.Properties.InstrumentationKey)
|
|
|
|
|
|
|
|
instance, err := metadata.GetInstance(ctx, providerID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
client.Context().CommonProperties["instance-name"] = instance.Name
|
2022-06-10 07:18:30 -04:00
|
|
|
|
|
|
|
return &Logger{
|
2022-08-01 06:32:28 -04:00
|
|
|
client: client,
|
2022-06-10 07:18:30 -04:00
|
|
|
}, nil
|
2022-06-03 05:55:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Disclose stores log information in Azure Application Insights!
|
|
|
|
// Do **NOT** log sensitive information!
|
|
|
|
func (l *Logger) Disclose(msg string) {
|
|
|
|
l.client.Track(appinsights.NewTraceTelemetry(msg, appinsights.Information))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close blocks until all information are written to cloud API.
|
|
|
|
func (l *Logger) Close() error {
|
|
|
|
<-l.client.Channel().Close()
|
|
|
|
return nil
|
|
|
|
}
|