-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpart1.py
More file actions
60 lines (47 loc) · 2.16 KB
/
part1.py
File metadata and controls
60 lines (47 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import requests
import logging
from fastapi import FastAPI, HTTPException, Query
from opentelemetry import trace
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
from opentelemetry.instrumentation.requests import RequestsInstrumentor
from opentelemetry.sdk.trace import TracerProvider
# Create FastAPI app
app = FastAPI()
# Set up the TracerProvider
trace.set_tracer_provider(TracerProvider())
# Creates a tracer from the global tracer provider
tracer = trace.get_tracer("open-telemetry.part1")
# Setup the logger and set minimum log level to info, everything below will not be logged
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
# Instrument FastAPI and Requests
FastAPIInstrumentor.instrument_app(app)
RequestsInstrumentor().instrument()
# Example logging from part 1
logger.info("hello from part 1")
# Tracer annotation which will automatically create a span for this function
@tracer.start_as_current_span("fetch_data_from_open_meteo")
def fetch_data_from_open_meteo(latitude, longitude):
# Alternative way of creating a span
# with tracer.start_as_current_span("fetch_data_from_open_meteo") as span:
logger.info('Fetching weather data from Open-Meteo')
# Define the parameters for your request here
params = {
'latitude': latitude, # Example latitude
'longitude': longitude, # Example longitude
'hourly': 'temperature_2m'
}
# Make a GET request to the Open-Meteo API using requests
response = requests.get('https://api.open-meteo.com/v1/forecast', params=params)
logger.info('Received response from Open-Meteo %s', response)
if response.status_code == 200:
return response.json()
else:
# Handle errors
raise HTTPException(status_code=500, detail='Failed to fetch data from Open-Meteo')
# Endpoint for getting weather data, defined via annotation and using Query parameters
@app.get('/weather')
async def get_weather(latitude: float = Query(..., description="Latitude of the location"),
longitude: float = Query(..., description="Longitude of the location")):
data = fetch_data_from_open_meteo(latitude, longitude)
return data