func()

in pkg/plugin/plugin.go [256:320]


func (d *CloudLoggingDatasource) query(ctx context.Context, pCtx backend.PluginContext, query backend.DataQuery) backend.DataResponse {
	response := backend.DataResponse{}

	var q queryModel
	response.Error = json.Unmarshal(query.JSON, &q)
	if response.Error != nil {
		return response
	}

	var qstr string
	if q.QueryText != "" {
		qstr = q.QueryText
	} else if q.Query != "" {
		qstr = q.Query
	}
	clientRequest := cloudlogging.Query{
		ProjectID: q.ProjectID,
		BucketId:  q.BucketId,
		ViewId:    q.ViewId,
		Filter:    qstr,
		Limit:     query.MaxDataPoints,
		TimeRange: struct {
			From string
			To   string
		}{
			From: query.TimeRange.From.Format(time.RFC3339),
			To:   query.TimeRange.To.Format(time.RFC3339),
		},
	}

	logs, err := d.client.ListLogs(ctx, &clientRequest)
	if err != nil {
		response.Error = fmt.Errorf("query: %w", err)
		return response
	}

	// create data frame response.
	frames := []*data.Frame{}

	for i := 0; i < len(logs); i++ {
		body, err := cloudlogging.GetLogEntryMessage(logs[i])
		if err != nil {
			// some log messages might not have a payload
			// log a warning here but continue
			log.DefaultLogger.Warn("failed getting log message", "warning", err)
		}

		labels := cloudlogging.GetLogLabels(logs[i])
		f := data.NewFrame(logs[i].GetInsertId())
		timestamp := data.NewField("time", nil, []time.Time{logs[i].GetTimestamp().AsTime()})
		content := data.NewField("content", labels, []string{body})

		f.Fields = append(f.Fields, timestamp, content)
		f.Meta = &data.FrameMeta{}
		f.Meta.PreferredVisualization = data.VisTypeLogs
		frames = append(frames, f)
	}

	// add the frames to the response.
	for _, f := range frames {
		response.Frames = append(response.Frames, f)
	}

	return response
}