|
| 1 | +package logger |
| 2 | + |
| 3 | +import ( |
| 4 | + "runtime" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/Azure/azure-container-networking/zapai" |
| 8 | + "github.com/microsoft/ApplicationInsights-Go/appinsights" |
| 9 | + "github.com/pkg/errors" |
| 10 | + "go.uber.org/zap" |
| 11 | + "go.uber.org/zap/zapcore" |
| 12 | +) |
| 13 | + |
| 14 | +type AIConfig struct { |
| 15 | + GracePeriod time.Duration |
| 16 | + IKey string |
| 17 | + Level zapcore.Level |
| 18 | + MaxBatchInterval time.Duration |
| 19 | + MaxBatchSize int |
| 20 | +} |
| 21 | + |
| 22 | +// ApplicationInsightsCore builds a zapcore.Core that sends logs to Application Insights. |
| 23 | +// The first return is the core, the second is a function to close the sink. |
| 24 | +func ApplicationInsightsCore(cfg *AIConfig) (zapcore.Core, func(), error) { |
| 25 | + // build the AI config |
| 26 | + aicfg := *appinsights.NewTelemetryConfiguration(cfg.IKey) |
| 27 | + aicfg.MaxBatchSize = cfg.MaxBatchSize |
| 28 | + aicfg.MaxBatchInterval = cfg.MaxBatchInterval |
| 29 | + sinkcfg := zapai.SinkConfig{ |
| 30 | + GracePeriod: cfg.GracePeriod, |
| 31 | + TelemetryConfiguration: aicfg, |
| 32 | + } |
| 33 | + // open the AI zap sink |
| 34 | + sink, aiclose, err := zap.Open(sinkcfg.URI()) |
| 35 | + if err != nil { |
| 36 | + return nil, aiclose, errors.Wrap(err, "failed to open AI sink") |
| 37 | + } |
| 38 | + // build the AI core |
| 39 | + core := zapai.NewCore(cfg.Level, sink) |
| 40 | + core = core.WithFieldMappers(zapai.DefaultMappers) |
| 41 | + // add normalized fields for the built-in AI Tags |
| 42 | + // TODO(rbtr): move to the caller |
| 43 | + return core.With([]zapcore.Field{ |
| 44 | + zap.String("user_id", runtime.GOOS), |
| 45 | + zap.String("operation_id", ""), |
| 46 | + zap.String("parent_id", "v0.0.0"), |
| 47 | + zap.String("version", "v0.0.0"), |
| 48 | + zap.String("account", "SubscriptionID"), |
| 49 | + zap.String("anonymous_user_id", "VMName"), |
| 50 | + zap.String("session_id", "VMID"), |
| 51 | + zap.String("AppName", "name"), |
| 52 | + zap.String("Region", "Location"), |
| 53 | + zap.String("ResourceGroup", "ResourceGroupName"), |
| 54 | + zap.String("VMSize", "VMSize"), |
| 55 | + zap.String("OSVersion", "OSVersion"), |
| 56 | + zap.String("VMID", "VMID"), |
| 57 | + }), aiclose, nil |
| 58 | +} |
0 commit comments