github.com/newrelic/go-agent@v3.26.0+incompatible/_integrations/nrsqlite3/example/main.go (about)

     1  // Copyright 2020 New Relic Corporation. All rights reserved.
     2  // SPDX-License-Identifier: Apache-2.0
     3  
     4  package main
     5  
     6  import (
     7  	"context"
     8  	"database/sql"
     9  	"fmt"
    10  	"os"
    11  	"time"
    12  
    13  	newrelic "github.com/newrelic/go-agent"
    14  	_ "github.com/newrelic/go-agent/_integrations/nrsqlite3"
    15  )
    16  
    17  func mustGetEnv(key string) string {
    18  	if val := os.Getenv(key); "" != val {
    19  		return val
    20  	}
    21  	panic(fmt.Sprintf("environment variable %s unset", key))
    22  }
    23  
    24  func main() {
    25  	db, err := sql.Open("nrsqlite3", ":memory:")
    26  	if err != nil {
    27  		panic(err)
    28  	}
    29  	defer db.Close()
    30  
    31  	db.Exec("CREATE TABLE zaps ( zap_num INTEGER )")
    32  	db.Exec("INSERT INTO zaps (zap_num) VALUES (22)")
    33  
    34  	cfg := newrelic.NewConfig("SQLite App", mustGetEnv("NEW_RELIC_LICENSE_KEY"))
    35  	cfg.Logger = newrelic.NewDebugLogger(os.Stdout)
    36  	app, err := newrelic.NewApplication(cfg)
    37  	if nil != err {
    38  		panic(err)
    39  	}
    40  	app.WaitForConnection(5 * time.Second)
    41  	txn := app.StartTransaction("sqliteQuery", nil, nil)
    42  
    43  	ctx := newrelic.NewContext(context.Background(), txn)
    44  	row := db.QueryRowContext(ctx, "SELECT count(*) from zaps")
    45  	var count int
    46  	row.Scan(&count)
    47  
    48  	txn.End()
    49  	app.Shutdown(5 * time.Second)
    50  
    51  	fmt.Println("number of entries in table", count)
    52  }