func()

in PetAdoptions/cdk/pet_stack/resources/microservices/petlistadoptions-go/petlistadoptions/repository.go [56:108]


func (r *repo) GetLatestAdoptions(ctx context.Context, petSearchURL string) ([]Adoption, error) {
	logger := log.With(r.logger, "method", "GetTopTransactions")

	tracer := otel.GetTracerProvider().Tracer("petlistadoptions")
	_, span := tracer.Start(ctx, "PGSQL Query", trace.WithSpanKind(trace.SpanKindClient))

	sql := `SELECT pet_id, transaction_id, adoption_date FROM transactions ORDER BY id DESC LIMIT 25`
	// TODO: implement native sql instrumentation when issue is closed.
	// https://github.com/open-telemetry/opentelemetry-go-contrib/issues/5
	//rows, err := r.db.QueryContext(ctx, sql)

	span.SetAttributes(
		attribute.String("sql", sql),
		attribute.String("url", r.safeConnStr),
	)

	rows, err := r.db.Query(sql)
	if err != nil {
		logger.Log("error", err)
		return nil, err
	}
	span.End()

	var wg sync.WaitGroup
	adoptions := make(chan Adoption)

	for rows.Next() {
		t := transaction{}

		err := rows.Scan(&t.PetID, &t.TransactionID, &t.AdoptionDate)

		if err != nil {
			level.Error(logger).Log("err", err)
			continue
		}
		wg.Add(1)
		go searchForPet(ctx, r.logger, &wg, adoptions, t, petSearchURL)
	}

	go func() {
		wg.Wait()
		close(adoptions)
	}()

	res := []Adoption{}

	for i := range adoptions {
		logger.Log("petid", i.PetID, "pettype", i.PetType, "petcolor", i.PetColor)
		res = append(res, i)
	}

	return res, nil
}