Skip to content

Commit 96fba8f

Browse files
authored
Create opentelemetry.cs (#322)
1 parent dd72b07 commit 96fba8f

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// ODP.NET OpenTelemetry Demo
2+
// This sample demonstrates using managed ODP.NET or ODP.NET Core with OpenTelemetry using the HR schema.
3+
// To setup, add NuGet packages: Oracle.ManagedDataAccess.OpenTelemetry, OpenTelemetry, and an OpenTelemetry exporter.
4+
// This sample is configured to use the Console Exporter (OpenTelemetry.Exporter.Console), but can be modified to another exporter.
5+
// Provide the Oracle database password and data source information for the connection string.
6+
7+
using System.Diagnostics;
8+
using OpenTelemetry; // for Sdk
9+
using OpenTelemetry.Trace; // for TracerProvider and TracerProviderBuilder
10+
using Oracle.ManagedDataAccess.Client; // for ODP.NET
11+
using Oracle.ManagedDataAccess.OpenTelemetry; // for ODP.NET OpenTelemetry
12+
13+
class ODP_OTel_Demo
14+
{
15+
static TracerProvider tracerProvider = Sdk.CreateTracerProviderBuilder()
16+
.AddOracleDataProviderInstrumentation(o => // ODP.NET OpenTelemetry extension method
17+
{
18+
o.EnableConnectionLevelAttributes = true;
19+
o.RecordException = true;
20+
o.InstrumentOracleDataReaderRead = true;
21+
o.SetDbStatementForText = true;
22+
})
23+
.AddSource("ODP.NET App")
24+
.AddConsoleExporter() // OpenTelemetry.Exporter.Console NuGet package extension method
25+
//.AddZipkinExporter() // OpenTelemetry.Exporter.Zipkin NuGet package extension method
26+
.Build()!;
27+
28+
static ActivitySource activitySource = new ActivitySource("ODPNET");
29+
30+
static string conString = @"User Id=hr;Password=<PASSWORD>;Data Source=<NET SERVICE NAME>;";
31+
32+
static void Main()
33+
{
34+
using (OracleConnection con = new OracleConnection(conString))
35+
{
36+
using (OracleCommand cmd = con.CreateCommand())
37+
{
38+
try
39+
{
40+
con.Open();
41+
cmd.CommandText = "select * from employees";
42+
43+
// Start OpenTelemetry activity
44+
using (Activity activity = activitySource.StartActivity("Retrieve data")!)
45+
{
46+
OracleDataReader reader = cmd.ExecuteReader();
47+
while (reader.Read())
48+
{
49+
// Use query results
50+
}
51+
reader.Dispose();
52+
}
53+
}
54+
catch (Exception ex)
55+
{
56+
Console.WriteLine(ex.Message);
57+
}
58+
}
59+
}
60+
}
61+
}
62+
63+
/******************************************************************************
64+
* The MIT License (MIT)
65+
*
66+
* Copyright (c) 2015, 2023 Oracle
67+
*
68+
* Permission is hereby granted, free of charge, to any person obtaining a copy
69+
* of this software and associated documentation files (the "Software"), to deal
70+
* in the Software without restriction, including without limitation the rights
71+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
72+
* copies of the Software, and to permit persons to whom the Software is
73+
* furnished to do so, subject to the following conditions:
74+
75+
* The above copyright notice and this permission notice shall be included in all
76+
* copies or substantial portions of the Software.
77+
78+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
79+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
80+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
81+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
82+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
83+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
84+
* SOFTWARE.
85+
*****************************************************************************/

0 commit comments

Comments
 (0)