|
| 1 | +// Copyright 2019-2025 The Go Cloud Development Kit Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package awsmysql_test |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + "log/slog" |
| 21 | + "testing" |
| 22 | + "time" |
| 23 | + |
| 24 | + "github.com/google/go-cmp/cmp" |
| 25 | + "go.opentelemetry.io/otel/attribute" |
| 26 | + "gocloud.dev/internal/testing/oteltest" |
| 27 | + "gocloud.dev/internal/testing/terraform" |
| 28 | + "gocloud.dev/mysql" |
| 29 | +) |
| 30 | + |
| 31 | +func TestOpenTelemetry(t *testing.T) { |
| 32 | + // This test will be skipped unless the project is set up with Terraform. |
| 33 | + // Before running go test, run in this directory: |
| 34 | + // |
| 35 | + // terraform init |
| 36 | + // terraform apply |
| 37 | + tfOut, err := terraform.ReadOutput(".") |
| 38 | + if err != nil || len(tfOut) == 0 { |
| 39 | + t.Skipf("Could not obtain harness info: %v", err) |
| 40 | + } |
| 41 | + endpoint, _ := tfOut["endpoint"].Value.(string) |
| 42 | + username, _ := tfOut["iam_db_username"].Value.(string) |
| 43 | + roleARN, _ := tfOut["iam_role_arn"].Value.(string) |
| 44 | + databaseName, _ := tfOut["database"].Value.(string) |
| 45 | + if endpoint == "" || username == "" || databaseName == "" { |
| 46 | + t.Fatalf("Missing one or more required Terraform outputs; got endpoint=%q iam_db_username=%q database=%q", endpoint, username, databaseName) |
| 47 | + } |
| 48 | + ctx := context.Background() |
| 49 | + |
| 50 | + // Setup the test exporter for both trace and metrics. |
| 51 | + te := oteltest.NewTestExporter(t, nil) |
| 52 | + defer te.Shutdown(ctx) |
| 53 | + |
| 54 | + // Open the database with otelsql. |
| 55 | + urlstr := fmt.Sprintf("awsmysql://%s@%s/%s?parseTime=true&aws_role_arn=%s", |
| 56 | + username, endpoint, databaseName, roleARN) |
| 57 | + t.Log("Connecting to:", urlstr) |
| 58 | + db, err := mysql.Open(ctx, urlstr) |
| 59 | + if err != nil { |
| 60 | + t.Fatal(err) |
| 61 | + } |
| 62 | + defer db.Close() |
| 63 | + |
| 64 | + query := func() error { |
| 65 | + rows, err := db.QueryContext(ctx, `SELECT CURRENT_TIMESTAMP`) |
| 66 | + if err != nil { |
| 67 | + return err |
| 68 | + } |
| 69 | + defer func() { _ = rows.Close() }() |
| 70 | + |
| 71 | + var currentTime time.Time |
| 72 | + for rows.Next() { |
| 73 | + err = rows.Scan(¤tTime) |
| 74 | + if err != nil { |
| 75 | + return err |
| 76 | + } |
| 77 | + } |
| 78 | + // Check for errors from iterating over rows |
| 79 | + if err = rows.Err(); err != nil { |
| 80 | + return err |
| 81 | + } |
| 82 | + slog.Info("Current time", "time", currentTime) |
| 83 | + return nil |
| 84 | + } |
| 85 | + if err = query(); err != nil { |
| 86 | + t.Error("QueryContext:", err) |
| 87 | + } |
| 88 | + |
| 89 | + spans := te.GetSpans().Snapshots() |
| 90 | + if !cmp.Equal(3, len(spans)) { |
| 91 | + t.Errorf("expected 3 spans, got %d: %v", len(spans), spans) |
| 92 | + } |
| 93 | + if !cmp.Equal("sql.connector.connect", spans[0].Name()) { |
| 94 | + t.Errorf("expected first span name to be sql.connector.connect, got %q", spans[0].Name()) |
| 95 | + } |
| 96 | + if !cmp.Equal("sql.conn.query", spans[1].Name()) { |
| 97 | + t.Errorf("expected second span name to be sql.conn.query, got %q", spans[1].Name()) |
| 98 | + } else { |
| 99 | + attrs := spans[1].Attributes() |
| 100 | + slog.Info("Span Attributes", "attributes", attrs) |
| 101 | + if !cmp.Equal(1, len(attrs)) { |
| 102 | + t.Errorf("expected 1 attribute, got %d: %v", len(attrs), attrs) |
| 103 | + } |
| 104 | + if !cmp.Equal(attribute.Key("db.statement"), attrs[0].Key) { |
| 105 | + t.Errorf("expected attribute key to be db.statement, got %q", attrs[0].Key) |
| 106 | + } |
| 107 | + if !cmp.Equal("SELECT CURRENT_TIMESTAMP", attrs[0].Value.AsString()) { |
| 108 | + t.Errorf("expected attribute value to be 'SELECT CURRENT_TIMESTAMP', got %q", attrs[0].Value.AsString()) |
| 109 | + } |
| 110 | + } |
| 111 | + if !cmp.Equal("sql.rows", spans[2].Name()) { |
| 112 | + t.Errorf("expected second span name to be sql.rows, got %q", spans[2].Name()) |
| 113 | + } else { |
| 114 | + attrs := spans[2].Attributes() |
| 115 | + slog.Info("Span Attributes", "attributes", attrs) |
| 116 | + if !cmp.Equal(0, len(attrs)) { |
| 117 | + t.Errorf("expected 0 attribute, got %d: %v", len(attrs), attrs) |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments