mirror of
https://github.com/grafana/grafana.git
synced 2026-08-14 07:04:57 -05:00
Frontend Logging: Integrate grafana javascript agent (#50801)
Add Grafana Javascript Agent integration to Grafana
This commit is contained in:
@@ -46,3 +46,72 @@ func NewFrontendLogMessageHandler(store *frontendlogging.SourceMapStore) fronten
|
||||
return response.Success("ok")
|
||||
}
|
||||
}
|
||||
|
||||
func GrafanaJavascriptAgentLogMessageHandler(store *frontendlogging.SourceMapStore) frontendLogMessageHandler {
|
||||
return func(c *models.ReqContext) response.Response {
|
||||
event := frontendlogging.FrontendGrafanaJavascriptAgentEvent{}
|
||||
if err := web.Bind(c.Req, &event); err != nil {
|
||||
return response.Error(http.StatusBadRequest, "bad request data", err)
|
||||
}
|
||||
|
||||
// Meta object is standard across event types, adding it globally.
|
||||
|
||||
if event.Logs != nil && len(event.Logs) > 0 {
|
||||
for _, logEntry := range event.Logs {
|
||||
var ctx = frontendlogging.CtxVector{}
|
||||
ctx = event.AddMetaToContext(ctx)
|
||||
ctx = append(ctx, "kind", "log", "original_timestamp", logEntry.Timestamp)
|
||||
|
||||
for k, v := range frontendlogging.KeyValToInterfaceMap(logEntry.KeyValContext()) {
|
||||
ctx = append(ctx, k, v)
|
||||
}
|
||||
switch logEntry.LogLevel {
|
||||
case frontendlogging.LogLevelDebug, frontendlogging.LogLevelTrace:
|
||||
{
|
||||
ctx = append(ctx, "original_log_level", logEntry.LogLevel)
|
||||
frontendLogger.Debug(logEntry.Message, ctx...)
|
||||
}
|
||||
case frontendlogging.LogLevelError:
|
||||
{
|
||||
ctx = append(ctx, "original_log_level", logEntry.LogLevel)
|
||||
frontendLogger.Error(logEntry.Message, ctx...)
|
||||
}
|
||||
case frontendlogging.LogLevelWarning:
|
||||
{
|
||||
ctx = append(ctx, "original_log_level", logEntry.LogLevel)
|
||||
frontendLogger.Warn(logEntry.Message, ctx...)
|
||||
}
|
||||
default:
|
||||
{
|
||||
ctx = append(ctx, "original_log_level", logEntry.LogLevel)
|
||||
frontendLogger.Info(logEntry.Message, ctx...)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if event.Measurements != nil && len(event.Measurements) > 0 {
|
||||
for _, measurementEntry := range event.Measurements {
|
||||
for measurementName, measurementValue := range measurementEntry.Values {
|
||||
var ctx = frontendlogging.CtxVector{}
|
||||
ctx = event.AddMetaToContext(ctx)
|
||||
ctx = append(ctx, measurementName, measurementValue)
|
||||
ctx = append(ctx, "kind", "measurement", "original_timestamp", measurementEntry.Timestamp)
|
||||
frontendLogger.Info("Measurement: "+measurementEntry.Type, ctx...)
|
||||
}
|
||||
}
|
||||
}
|
||||
if event.Exceptions != nil && len(event.Exceptions) > 0 {
|
||||
for _, exception := range event.Exceptions {
|
||||
var ctx = frontendlogging.CtxVector{}
|
||||
ctx = event.AddMetaToContext(ctx)
|
||||
exception := exception
|
||||
transformedException := frontendlogging.TransformException(&exception, store)
|
||||
ctx = append(ctx, "kind", "exception", "type", transformedException.Type, "value", transformedException.Value, "stacktrace", transformedException.String())
|
||||
ctx = append(ctx, "original_timestamp", exception.Timestamp)
|
||||
frontendLogger.Error(exception.Message(), ctx...)
|
||||
}
|
||||
}
|
||||
return response.Success("ok")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user